Skip to content

Commit

Permalink
Reuse SelectorTokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Mar 19, 2016
1 parent f74ff80 commit 2ed9be0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/com/inet/lib/less/LessExtendMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
package com.inet.lib.less;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -42,6 +43,15 @@ class LessExtendMap {
// use a LinkedHashSet as cache to remove duplicates and hold the original order
private final LinkedHashSet<String> selectorList = new LinkedHashSet<>();

private final ArrayDeque<SelectorTokenizer> tokenizers = new ArrayDeque<SelectorTokenizer>() {
public SelectorTokenizer pollLast() {
if( size() == 0 ) {
return new SelectorTokenizer();
}
return super.pollLast();
}
};

/**
* Default constructor
*/
Expand Down Expand Up @@ -75,14 +85,15 @@ void add( LessExtend lessExtend, String[] mainSelector ) {
String extendingSelector = lessExtend.getExtendingSelector();
if( lessExtend.isAll() ) {
LessExtendResult extend = new LessExtendResult( mainSelector, extendingSelector );
SelectorTokenizer tokenizer = new SelectorTokenizer( extendingSelector );
SelectorTokenizer tokenizer = tokenizers.pollLast().init( extendingSelector );
do {
String token = tokenizer.next();
if( token == null ) {
break;
}
all.add( token, extend );
} while( true );
tokenizers.addLast( tokenizer );
} else {
exact.add( extendingSelector, mainSelector );
}
Expand Down Expand Up @@ -145,7 +156,7 @@ private void concatenateExtendsRecursive( String selector, boolean isReference,
}
}
}
SelectorTokenizer tokenizer = new SelectorTokenizer( allSelector );
SelectorTokenizer tokenizer = tokenizers.pollLast().init( allSelector );
do {
String token = tokenizer.next();
if( token == null ) {
Expand All @@ -167,5 +178,6 @@ private void concatenateExtendsRecursive( String selector, boolean isReference,
}
}
} while( true );
tokenizers.addLast( tokenizer );
}
}
8 changes: 5 additions & 3 deletions src/com/inet/lib/less/SelectorTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@
*/
class SelectorTokenizer {

private final String selector;
private String selector;

private int idx, lastIdx;
private int idx, lastIdx;

/**
* Create a new tokenizer.
* @param selector all selectors
*/
SelectorTokenizer( String selector ) {
SelectorTokenizer init( String selector ) {
this.selector = selector;
idx = lastIdx = 0;
return this;
}

/**
Expand Down

0 comments on commit 2ed9be0

Please sign in to comment.