1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.text.matcher;
19
20 import org.apache.commons.lang3.ArrayUtils;
21 import org.apache.commons.lang3.StringUtils;
22
23 /**
24 * Provides access to matchers defined in this package.
25 *
26 * @since 1.3
27 */
28 public final class StringMatcherFactory {
29
30 /**
31 * Matches the comma character.
32 */
33 private static final AbstractStringMatcher.CharMatcher COMMA_MATCHER = new AbstractStringMatcher.CharMatcher(',');
34
35 /**
36 * Matches the double quote character.
37 */
38 private static final AbstractStringMatcher.CharMatcher DOUBLE_QUOTE_MATCHER = new AbstractStringMatcher.CharMatcher(
39 '"');
40
41 /**
42 * Defines the singleton for this class.
43 */
44 public static final StringMatcherFactory INSTANCE = new StringMatcherFactory();
45
46 /**
47 * Matches no characters.
48 */
49 private static final AbstractStringMatcher.NoneMatcher NONE_MATCHER = new AbstractStringMatcher.NoneMatcher();
50
51 /**
52 * Matches the single or double quote character.
53 */
54 private static final AbstractStringMatcher.CharSetMatcher QUOTE_MATCHER = new AbstractStringMatcher.CharSetMatcher(
55 "'\"".toCharArray());
56
57 /**
58 * Matches the double quote character.
59 */
60 private static final AbstractStringMatcher.CharMatcher SINGLE_QUOTE_MATCHER = new AbstractStringMatcher.CharMatcher(
61 '\'');
62
63 /**
64 * Matches the space character.
65 */
66 private static final AbstractStringMatcher.CharMatcher SPACE_MATCHER = new AbstractStringMatcher.CharMatcher(' ');
67
68 /**
69 * Matches the same characters as StringTokenizer, namely space, tab, newline, form feed.
70 */
71 private static final AbstractStringMatcher.CharSetMatcher SPLIT_MATCHER = new AbstractStringMatcher.CharSetMatcher(
72 " \t\n\r\f".toCharArray());
73
74 /**
75 * Matches the tab character.
76 */
77 private static final AbstractStringMatcher.CharMatcher TAB_MATCHER = new AbstractStringMatcher.CharMatcher('\t');
78
79 /**
80 * Matches the String trim() whitespace characters.
81 */
82 private static final AbstractStringMatcher.TrimMatcher TRIM_MATCHER = new AbstractStringMatcher.TrimMatcher();
83
84 /**
85 * No need to build instances for now.
86 */
87 private StringMatcherFactory() {
88 // empty
89 }
90
91 /**
92 * Creates a matcher that matches all of the given matchers in order.
93 *
94 * @param stringMatchers the matcher
95 * @return a matcher that matches all of the given matchers in order.
96 * @since 1.9
97 */
98 public StringMatcher andMatcher(final StringMatcher... stringMatchers) {
99 final int len = ArrayUtils.getLength(stringMatchers);
100 if (len == 0) {
101 return NONE_MATCHER;
102 }
103 if (len == 1) {
104 return stringMatchers[0];
105 }
106 return new AbstractStringMatcher.AndStringMatcher(stringMatchers);
107 }
108
109 /**
110 * Constructor that creates a matcher from a character.
111 *
112 * @param ch the character to match, must not be null
113 * @return a new Matcher for the given char
114 */
115 public StringMatcher charMatcher(final char ch) {
116 return new AbstractStringMatcher.CharMatcher(ch);
117 }
118
119 /**
120 * Constructor that creates a matcher from a set of characters.
121 *
122 * @param chars the characters to match, null or empty matches nothing
123 * @return a new matcher for the given char[]
124 */
125 public StringMatcher charSetMatcher(final char... chars) {
126 final int len = ArrayUtils.getLength(chars);
127 if (len == 0) {
128 return NONE_MATCHER;
129 }
130 if (len == 1) {
131 return new AbstractStringMatcher.CharMatcher(chars[0]);
132 }
133 return new AbstractStringMatcher.CharSetMatcher(chars);
134 }
135
136 /**
137 * Creates a matcher from a string representing a set of characters.
138 *
139 * @param chars the characters to match, null or empty matches nothing
140 * @return a new Matcher for the given characters
141 */
142 public StringMatcher charSetMatcher(final String chars) {
143 final int len = StringUtils.length(chars);
144 if (len == 0) {
145 return NONE_MATCHER;
146 }
147 if (len == 1) {
148 return new AbstractStringMatcher.CharMatcher(chars.charAt(0));
149 }
150 return new AbstractStringMatcher.CharSetMatcher(chars.toCharArray());
151 }
152
153 /**
154 * Returns a matcher which matches the comma character.
155 *
156 * @return a matcher for a comma
157 */
158 public StringMatcher commaMatcher() {
159 return COMMA_MATCHER;
160 }
161
162 /**
163 * Returns a matcher which matches the double quote character.
164 *
165 * @return a matcher for a double quote
166 */
167 public StringMatcher doubleQuoteMatcher() {
168 return DOUBLE_QUOTE_MATCHER;
169 }
170
171 /**
172 * Matches no characters.
173 *
174 * @return a matcher that matches nothing
175 */
176 public StringMatcher noneMatcher() {
177 return NONE_MATCHER;
178 }
179
180 /**
181 * Returns a matcher which matches the single or double quote character.
182 *
183 * @return a matcher for a single or double quote
184 */
185 public StringMatcher quoteMatcher() {
186 return QUOTE_MATCHER;
187 }
188
189 /**
190 * Returns a matcher which matches the single quote character.
191 *
192 * @return a matcher for a single quote
193 */
194 public StringMatcher singleQuoteMatcher() {
195 return SINGLE_QUOTE_MATCHER;
196 }
197
198 /**
199 * Returns a matcher which matches the space character.
200 *
201 * @return a matcher for a space
202 */
203 public StringMatcher spaceMatcher() {
204 return SPACE_MATCHER;
205 }
206
207 /**
208 * Matches the same characters as StringTokenizer, namely space, tab, newline and form feed.
209 *
210 * @return The split matcher
211 */
212 public StringMatcher splitMatcher() {
213 return SPLIT_MATCHER;
214 }
215
216 /**
217 * Creates a matcher from a string.
218 *
219 * @param chars the string to match, null or empty matches nothing
220 * @return a new Matcher for the given String
221 * @since 1.9
222 */
223 public StringMatcher stringMatcher(final char... chars) {
224 final int length = ArrayUtils.getLength(chars);
225 return length == 0 ? NONE_MATCHER
226 : length == 1 ? new AbstractStringMatcher.CharMatcher(chars[0])
227 : new AbstractStringMatcher.CharArrayMatcher(chars);
228 }
229
230 /**
231 * Creates a matcher from a string.
232 *
233 * @param str the string to match, null or empty matches nothing
234 * @return a new Matcher for the given String
235 */
236 public StringMatcher stringMatcher(final String str) {
237 return StringUtils.isEmpty(str) ? NONE_MATCHER : stringMatcher(str.toCharArray());
238 }
239
240 /**
241 * Returns a matcher which matches the tab character.
242 *
243 * @return a matcher for a tab
244 */
245 public StringMatcher tabMatcher() {
246 return TAB_MATCHER;
247 }
248
249 /**
250 * Matches the String trim() whitespace characters.
251 *
252 * @return The trim matcher
253 */
254 public StringMatcher trimMatcher() {
255 return TRIM_MATCHER;
256 }
257
258 }
259