1 package com.github.mygreen.cellformatter.tokenizer;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.github.mygreen.cellformatter.lang.ArgUtils;
7 import com.github.mygreen.cellformatter.lang.Utils;
8
9
10
11
12
13
14
15
16 public class TokenStore {
17
18 private List<Token> tokens = new ArrayList<>();
19
20 public TokenStore() {
21
22 }
23
24
25
26
27
28 public void add(final Token token) {
29 getTokens().add(token);
30 }
31
32
33
34
35
36 public List<Token> getTokens() {
37 return tokens;
38 }
39
40
41
42
43
44
45
46 public List<TokenStore> split(final Token.Symbol symbol) {
47
48 ArgUtils.notNull(symbol, "symbol");
49
50 final List<TokenStore> list = new ArrayList<>();
51
52 TokenStoretter/tokenizer/TokenStore.html#TokenStore">TokenStore store = new TokenStore();
53 list.add(store);
54
55 for(Token token : tokens) {
56
57 if(token.equals(symbol)) {
58 store = new TokenStore();
59 list.add(store);
60
61 } else {
62 store.add(token);
63 }
64
65 }
66
67 return list;
68 }
69
70
71
72
73
74 public String getConcatenatedToken() {
75
76 StringBuilder sb = new StringBuilder();
77
78 for(Token token : tokens) {
79 sb.append(token.getValue());
80 }
81
82 return sb.toString();
83
84 }
85
86
87
88
89
90
91 public boolean containsInFactor(final String search) {
92 return containsInFactor(search, false);
93 }
94
95
96
97
98
99
100 public boolean containsInFactorIgnoreCase(final String search) {
101 return containsInFactor(search, true);
102 }
103
104 public boolean containsInFactor(final String search, final boolean ignoreCase) {
105
106 for(Token token : tokens) {
107 if(!(token instanceof Token.Factor)) {
108 continue;
109 }
110
111 final Token.Factor factor = token.asFactor();
112 if(ignoreCase) {
113 if(Utils.containsIgnoreCase(factor.getValue(), search)) {
114 return true;
115 }
116 } else {
117 if(factor.getValue().contains(search)) {
118 return true;
119 }
120 }
121
122 }
123
124 return false;
125 }
126
127
128
129
130
131
132 public boolean containsAnyInFactor(final String[] searchChars) {
133 return containsAnyInFactor(searchChars, false);
134 }
135
136
137
138
139
140
141 public boolean containsAnyInFactorIgnoreCase(final String[] searchChars) {
142 return containsAnyInFactor(searchChars, true);
143 }
144
145
146
147
148
149
150 private boolean containsAnyInFactor(final String[] searchChars, final boolean ignoreCase) {
151
152 for(Token token : tokens) {
153 if(!(token instanceof Token.Factor)) {
154 continue;
155 }
156
157 final Token.Factor factor = token.asFactor();
158 if(Utils.containsAny(factor.getValue(), searchChars, ignoreCase)) {
159 return true;
160 }
161
162 }
163
164 return false;
165 }
166
167 @Override
168 public String toString() {
169
170 StringBuilder sb = new StringBuilder();
171
172 final int size = tokens.size();
173 for(int i=0; i < size; i++) {
174 final Token token = tokens.get(i);
175 sb.append(String.format("[%d]%s(%s)",
176 i,
177 token.getClass().getSimpleName(),
178 token.getValue()));
179
180 if(i < size-1) {
181 sb.append(", ");
182 }
183
184 }
185
186 return sb.toString();
187
188 }
189
190 }