1 /*
2 * Characters.java
3 * Created on September 17, 2003
4 *
5 * The Blues Framework - A lightweight application framework
6 * Copyright (C) 2003 Lonnie Pryor
7 * http://blues.lonniepryor.com
8 *
9 * This library is free software; you can redistribute it and/or modify it under the
10 * terms of the GNU Lesser General Public License as published by the Free Software
11 * Foundation; either version 2.1 of the License, or (at your option) any later
12 * version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along
19 * with this library; if not, write to:
20 *
21 * The Free Software Foundation, Inc.
22 * 59 Temple Place, Suite 330
23 * Boston, MA 02111-1307 USA
24 *
25 */
26 package com.lonniepryor.blues.util;
27
28 /***
29 * Specification interface for identifying Characters. Instances of this class may
30 * be logically combined using the AND, OR, and NOT operations. Instances may also
31 * be sequentially constructed using the Builder class. Many common use-cases are
32 * supplied via static accessor methods.
33 *
34 * @author Lonnie Pryor
35 * @version $Revision: 1.1 $
36 */
37 public abstract class Characters {
38 /*** A specification satisfied by digit characters. */
39 private static final Characters digits = new Characters() {
40 protected boolean isSatisfiedBy (char canidate) {
41 return Character.isDigit(canidate);
42 }
43 };
44 /*** A specification satisfied by letter characters. */
45 private static final Characters letters = new Characters() {
46 protected boolean isSatisfiedBy (char canidate) {
47 return Character.isLetter(canidate);
48 }
49 };
50 /*** A specification satisfied by upper case characters. */
51 private static final Characters lowerCase = new Characters() {
52 protected boolean isSatisfiedBy (char canidate) {
53 return Character.isLowerCase(canidate);
54 }
55 };
56 /*** A specification satisfied by upper case characters. */
57 private static final Characters upperCase = new Characters() {
58 protected boolean isSatisfiedBy (char canidate) {
59 return Character.isUpperCase(canidate);
60 }
61 };
62 /*** A specification satisfied by whitespace characters. */
63 private static final Characters whitespace = new Characters() {
64 protected boolean isSatisfiedBy (char canidate) {
65 return Character.isWhitespace(canidate);
66 }
67 };
68
69 /***
70 * Creates a new Characters object.
71 */
72 protected Characters () {
73 }
74
75 /***
76 * Returns a specification satisfied by digit characters.
77 *
78 * @return A specification satisfied by digit characters.
79 */
80 public static Characters digits () {
81 return digits;
82 }
83
84 /***
85 * Returns a specification satisfied by letter characters.
86 *
87 * @return A specification satisfied by letter characters.
88 */
89 public static Characters letters () {
90 return letters;
91 }
92
93 /***
94 * Returns a specification satisfied by upper case characters.
95 *
96 * @return A specification satisfied by upper case characters.
97 */
98 public static Characters lowerCase () {
99 return lowerCase;
100 }
101
102 /***
103 * Returns a specification satisfied by upper case characters.
104 *
105 * @return A specification satisfied by upper case characters.
106 */
107 public static Characters upperCase () {
108 return upperCase;
109 }
110
111 /***
112 * Returns a specification satisfied by whitespace characters.
113 *
114 * @return A specification satisfied by whitespace characters.
115 */
116 public static Characters whitespace () {
117 return whitespace;
118 }
119
120 /***
121 * Returns a specification satisfied by the specified character.
122 *
123 * @param c The character to match to.
124 *
125 * @return A specification satisfied by the specified character.
126 */
127 public static Characters equalTo (final char c) {
128 return new Characters() {
129 protected boolean isSatisfiedBy (char canidate) {
130 return canidate == c;
131 }
132 };
133 }
134
135 /***
136 * Returns true if the specified char satisfies this specification.
137 *
138 * @param canidate The character to test.
139 *
140 * @return True if the specified char satisfies this specification.
141 */
142 protected abstract boolean isSatisfiedBy (char canidate);
143
144 /***
145 * Returns true if all of the supplied Characters satisfy this specification.
146 *
147 * @param all The array of Characters to test.
148 *
149 * @return True if all of the supplied Characters satisfy this specification.
150 */
151 public final boolean isSatisfiedByAll (char[] all) {
152 if (all == null)
153 throw new NullPointerException("all");
154 for (int i = 0; i < all.length; ++i)
155 if (!isSatisfiedBy(all[i]))
156 return false;
157 return true;
158 }
159
160 /***
161 * Returns true if any of the supplied Characters satisfy this specification.
162 *
163 * @param any The array of Characters to test.
164 *
165 * @return True if any of the supplied Characters satisfy this specification.
166 */
167 public final boolean isSatisfiedByAny (char[] any) {
168 if (any == null)
169 throw new NullPointerException("any");
170 for (int i = 0; i < any.length; ++i)
171 if (isSatisfiedBy(any[i]))
172 return true;
173 return false;
174 }
175
176 /***
177 * Selects the first Characters that satisfies this specification from the
178 * supplied array.
179 *
180 * @param from The array to select from.
181 *
182 * @return The first Characters that satisfies this specification from the
183 * supplied array.
184 */
185 public final char selectFirst (char[] from) {
186 if (from == null)
187 throw new NullPointerException("from");
188 for (int i = 0; i < from.length; ++i)
189 if (isSatisfiedBy(from[i]))
190 return from[i];
191 return 0;
192 }
193
194 /***
195 * Selects all the Characters that satisfy this specification from the supplied
196 * array.
197 *
198 * @param from The array to select from.
199 *
200 * @return All the Characters that satisfy this specification from the supplied
201 * array.
202 */
203 public final char[] selectAll (char[] from) {
204 if (from == null)
205 throw new NullPointerException("from");
206 int count = 0;
207 char[] matches = new char[from.length];
208 for (int i = 0; i < from.length; ++i)
209 if (isSatisfiedBy(from[i]))
210 matches[count++] = from[i];
211 char[] results = new char[count];
212 System.arraycopy(matches, 0, results, 0, count);
213 return results;
214 }
215
216 /***
217 * Returns a specification representing a logical AND of this specification on
218 * the left and the supplied specification on the right.
219 *
220 * @param specification The specification to AND with.
221 *
222 * @return A specification representing a logical AND of this specification on
223 * the left and the supplied specification on the right.
224 */
225 public final Characters and (final Characters specification) {
226 if (specification == null)
227 throw new NullPointerException("specification");
228 return new Characters() {
229 public boolean isSatisfiedBy (char canidate) {
230 return Characters.this.isSatisfiedBy(canidate)
231 && specification.isSatisfiedBy(canidate);
232 }
233 };
234 }
235
236 /***
237 * Returns a specification representing a logical OR of this specification on the
238 * left and the supplied specification on the right.
239 *
240 * @param specification The specification to OR with.
241 *
242 * @return A specification representing a logical OR of this specification on the
243 * left and the supplied specification on the right.
244 */
245 public final Characters or (final Characters specification) {
246 if (specification == null)
247 throw new NullPointerException("specification");
248 return new Characters() {
249 public boolean isSatisfiedBy (char canidate) {
250 return Characters.this.isSatisfiedBy(canidate)
251 || specification.isSatisfiedBy(canidate);
252 }
253 };
254 }
255
256 /***
257 * Returns a specification representing a logical NOT of this specification.
258 *
259 * @return A specification representing a logical NOT of this specification.
260 */
261 public final Characters not () {
262 return new Characters() {
263 public boolean isSatisfiedBy (char canidate) {
264 return !Characters.this.isSatisfiedBy(canidate);
265 }
266 };
267 }
268 }
This page was automatically generated by Maven