Skip to content

Commit 2ed9be0

Browse files
committedMar 19, 2016
Reuse SelectorTokenizer
1 parent f74ff80 commit 2ed9be0

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed
 

‎src/com/inet/lib/less/LessExtendMap.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
package com.inet.lib.less;
2828

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

46+
private final ArrayDeque<SelectorTokenizer> tokenizers = new ArrayDeque<SelectorTokenizer>() {
47+
public SelectorTokenizer pollLast() {
48+
if( size() == 0 ) {
49+
return new SelectorTokenizer();
50+
}
51+
return super.pollLast();
52+
}
53+
};
54+
4555
/**
4656
* Default constructor
4757
*/
@@ -75,14 +85,15 @@ void add( LessExtend lessExtend, String[] mainSelector ) {
7585
String extendingSelector = lessExtend.getExtendingSelector();
7686
if( lessExtend.isAll() ) {
7787
LessExtendResult extend = new LessExtendResult( mainSelector, extendingSelector );
78-
SelectorTokenizer tokenizer = new SelectorTokenizer( extendingSelector );
88+
SelectorTokenizer tokenizer = tokenizers.pollLast().init( extendingSelector );
7989
do {
8090
String token = tokenizer.next();
8191
if( token == null ) {
8292
break;
8393
}
8494
all.add( token, extend );
8595
} while( true );
96+
tokenizers.addLast( tokenizer );
8697
} else {
8798
exact.add( extendingSelector, mainSelector );
8899
}
@@ -145,7 +156,7 @@ private void concatenateExtendsRecursive( String selector, boolean isReference,
145156
}
146157
}
147158
}
148-
SelectorTokenizer tokenizer = new SelectorTokenizer( allSelector );
159+
SelectorTokenizer tokenizer = tokenizers.pollLast().init( allSelector );
149160
do {
150161
String token = tokenizer.next();
151162
if( token == null ) {
@@ -167,5 +178,6 @@ private void concatenateExtendsRecursive( String selector, boolean isReference,
167178
}
168179
}
169180
} while( true );
181+
tokenizers.addLast( tokenizer );
170182
}
171183
}

‎src/com/inet/lib/less/SelectorTokenizer.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@
3333
*/
3434
class SelectorTokenizer {
3535

36-
private final String selector;
36+
private String selector;
3737

38-
private int idx, lastIdx;
38+
private int idx, lastIdx;
3939

4040
/**
4141
* Create a new tokenizer.
4242
* @param selector all selectors
4343
*/
44-
SelectorTokenizer( String selector ) {
44+
SelectorTokenizer init( String selector ) {
4545
this.selector = selector;
46+
idx = lastIdx = 0;
47+
return this;
4648
}
4749

4850
/**

0 commit comments

Comments
 (0)