KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > pattern > CharPredicates


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Dec 11, 2004
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec.pattern;
14
15 /**
16  * This class provides some common CharPredicate implementations.
17  * @author Ben Yu
18  *
19  * Dec 11, 2004
20  */

21 public final class CharPredicates {
22   /**
23    * == a.
24    */

25   public static CharPredicate isChar(final char a){
26     return new CharPredicate(){
27       public boolean isChar(final char c){
28         return c==a;
29       }
30     };
31   }
32   /**
33    * != a.
34    */

35   public static CharPredicate notChar(final char a){
36     return new CharPredicate(){
37       public boolean isChar(final char c){
38         return c!=a;
39       }
40     };
41   }
42   /**
43    * between a and b inclusive.
44    */

45   public static CharPredicate range(final char a, final char b){
46     return new CharPredicate(){
47       public boolean isChar(final char c){
48         return c>=a && c <= b;
49       }
50     };
51   }
52   /**
53    * between 0 and 9.
54    */

55   public static CharPredicate isDigit(){
56     return range('0','9');
57   }
58   /**
59    * not between a and b inclusive.
60    */

61   public static CharPredicate notRange(final char a, final char b){
62     return new CharPredicate(){
63       public boolean isChar(final char c){
64         return !(c>=a && c <= b);
65       }
66     };
67   }
68   /**
69    * among chars.
70    */

71   public static CharPredicate among(final char[] chars){
72     return new CharPredicate(){
73       public boolean isChar(final char c){
74         return charAmong(c, chars);
75       }
76     };
77   }
78   /**
79    * not among chars.
80    */

81   public static CharPredicate notAmong(final char[] chars){
82     return new CharPredicate(){
83       public boolean isChar(final char c){
84         return !charAmong(c, chars);
85       }
86     };
87   }
88   static boolean charAmong(final char c, final char[] chars){
89     for(int i=0; i<chars.length; i++){
90       if(c==chars[i]) return true;
91     }
92     return false;
93   }
94   /**
95    * is hex digit.
96    */

97   public static CharPredicate isHexDigit(){
98     return new CharPredicate(){
99       public boolean isChar(final char c){
100         return c>='0' && c <= '9' || c >='a' && c <='f' || c>='A' && c <= 'F';
101       }
102     };
103   }
104   /**
105    * [A-Z].
106    */

107   public static CharPredicate isUppercase(){
108     return new CharPredicate(){
109       public boolean isChar(char c){return Character.isUpperCase(c);}
110     };
111   }
112   /**
113    * [a-z].
114    */

115   public static CharPredicate isLowercase(){
116     return new CharPredicate(){
117       public boolean isChar(char c){return Character.isLowerCase(c);}
118     };
119   }
120   /**
121    * is white space.
122    */

123   public static CharPredicate isWhitespace(){
124     return new CharPredicate(){
125       public boolean isChar(char c){return Character.isWhitespace(c);}
126     };
127   }
128   /**
129    * [a-zA-Z].
130    */

131   public static CharPredicate isAlpha(){
132     return new CharPredicate(){
133       public boolean isChar(final char c){
134         return c <='z' && c>='a' || c <='Z' && c >= 'A';
135       }
136     };
137   }
138   /**
139    * [a-zA-Z_].
140    */

141   public static CharPredicate isAlpha_(){
142     return new CharPredicate(){
143       public boolean isChar(final char c){
144         return c=='_' || c <='z' && c>='a' || c <='Z' && c >= 'A';
145       }
146     };
147   }
148   /**
149    * is letter.
150    */

151   public static CharPredicate isLetter(){
152     return new CharPredicate(){
153       public boolean isChar(final char c){
154         return Character.isLetter(c);
155       }
156     };
157   }
158   /**
159    * [a-zA-Z0-9_]
160    */

161   public static CharPredicate isAlphaNumeric(){
162     return new CharPredicate(){
163       public boolean isChar(final char c){
164         return c=='_' || c>='A' && c <= 'Z' || c>='a' && c<='z' || c>='0' && c<='9';
165       }
166     };
167   }
168   /**
169    * Negate a CharPredicate object.
170    */

171   public static CharPredicate not(final CharPredicate cp){
172     return new CharPredicate(){
173       public boolean isChar(final char c){
174         return !cp.isChar(c);
175       }
176     };
177   }
178   /**
179    * Logical and of two CharPredicate objects.
180    */

181   public static CharPredicate and(final CharPredicate cp1, final CharPredicate cp2){
182     return new CharPredicate(){
183       public boolean isChar(final char c){
184         return cp1.isChar(c) && cp2.isChar(c);
185       }
186     };
187   }
188   /**
189    * Logical or of two CharPredicate objects.
190    */

191   public static CharPredicate or(final CharPredicate cp1, final CharPredicate cp2){
192     return new CharPredicate(){
193       public boolean isChar(final char c){
194         return cp1.isChar(c) || cp2.isChar(c);
195       }
196     };
197   }
198
199   /**
200    * Logical and of an array of CharPredicate objects.
201    */

202   public static CharPredicate and(final CharPredicate... cps){
203     if(cps.length==0) return always();
204     else if(cps.length==1) return cps[0];
205     return new CharPredicate(){
206       public boolean isChar(final char c){
207         for(int i=0;i<cps.length;i++){
208           if(!cps[i].isChar(c)) return false;
209         }
210         return true;
211       }
212     };
213   }
214   /**
215    * Logical or of an array of CharPredicate objects.
216    */

217   public static CharPredicate or(final CharPredicate... cps){
218     if(cps.length==0) return never();
219     else if(cps.length==1) return cps[0];
220     return new CharPredicate(){
221       public boolean isChar(final char c){
222         for(int i=0;i<cps.length;i++){
223           if(cps[i].isChar(c)) return true;
224         }
225         return false;
226       }
227     };
228   }
229   /**
230    * A predicate that always returns false.
231    */

232   public static CharPredicate never(){
233     return _never;
234   }
235   /**
236    * A predicate that always returns true.
237    */

238   public static CharPredicate always(){
239     return _always;
240   }
241   private static final CharPredicate _never = new CharPredicate(){
242     public boolean isChar(final char c){return false;}
243   };
244   private static final CharPredicate _always = new CharPredicate(){
245     public boolean isChar(final char c){return true;}
246   };
247 }
248
Popular Tags