1 8 13 package jfun.parsec.pattern; 14 15 21 public final class CharPredicates { 22 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 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 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 55 public static CharPredicate isDigit(){ 56 return range('0','9'); 57 } 58 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 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 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 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 107 public static CharPredicate isUppercase(){ 108 return new CharPredicate(){ 109 public boolean isChar(char c){return Character.isUpperCase(c);} 110 }; 111 } 112 115 public static CharPredicate isLowercase(){ 116 return new CharPredicate(){ 117 public boolean isChar(char c){return Character.isLowerCase(c);} 118 }; 119 } 120 123 public static CharPredicate isWhitespace(){ 124 return new CharPredicate(){ 125 public boolean isChar(char c){return Character.isWhitespace(c);} 126 }; 127 } 128 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 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 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 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 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 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 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 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 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 232 public static CharPredicate never(){ 233 return _never; 234 } 235 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 |