1 16 package org.apache.commons.lang; 17 18 import java.io.Serializable ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 38 public class CharSet implements Serializable { 39 40 41 private static final long serialVersionUID = 5947847346149275958L; 42 43 47 public static final CharSet EMPTY = new CharSet((String ) null); 48 49 53 public static final CharSet ASCII_ALPHA = new CharSet("a-zA-Z"); 54 55 59 public static final CharSet ASCII_ALPHA_LOWER = new CharSet("a-z"); 60 61 65 public static final CharSet ASCII_ALPHA_UPPER = new CharSet("A-Z"); 66 67 71 public static final CharSet ASCII_NUMERIC = new CharSet("0-9"); 72 73 78 protected static final Map COMMON = new HashMap (); 79 80 static { 81 COMMON.put(null, EMPTY); 82 COMMON.put("", EMPTY); 83 COMMON.put("a-zA-Z", ASCII_ALPHA); 84 COMMON.put("A-Za-z", ASCII_ALPHA); 85 COMMON.put("a-z", ASCII_ALPHA_LOWER); 86 COMMON.put("A-Z", ASCII_ALPHA_UPPER); 87 COMMON.put("0-9", ASCII_NUMERIC); 88 } 89 90 91 private Set set = new HashSet (); 92 93 137 public static CharSet getInstance(String setStr) { 138 Object set = COMMON.get(setStr); 139 if (set != null) { 140 return (CharSet) set; 141 } 142 return new CharSet(setStr); 143 } 144 145 152 protected CharSet(String setStr) { 153 super(); 154 add(setStr); 155 } 156 157 164 protected CharSet(String [] set) { 165 super(); 166 int sz = set.length; 167 for (int i = 0; i < sz; i++) { 168 add(set[i]); 169 } 170 } 171 172 178 protected void add(String str) { 179 if (str == null) { 180 return; 181 } 182 183 int len = str.length(); 184 int pos = 0; 185 while (pos < len) { 186 int remainder = (len - pos); 187 if (remainder >= 4 && str.charAt(pos) == '^' && str.charAt(pos + 2) == '-') { 188 set.add(new CharRange(str.charAt(pos + 1), str.charAt(pos + 3), true)); 190 pos += 4; 191 } else if (remainder >= 3 && str.charAt(pos + 1) == '-') { 192 set.add(new CharRange(str.charAt(pos), str.charAt(pos + 2))); 194 pos += 3; 195 } else if (remainder >= 2 && str.charAt(pos) == '^') { 196 set.add(new CharRange(str.charAt(pos + 1), true)); 198 pos += 2; 199 } else { 200 set.add(new CharRange(str.charAt(pos))); 202 pos += 1; 203 } 204 } 205 } 206 207 214 public CharRange[] getCharRanges() { 215 return (CharRange[]) set.toArray(new CharRange[set.size()]); 216 } 217 218 226 public boolean contains(char ch) { 227 for (Iterator it = set.iterator(); it.hasNext();) { 228 CharRange range = (CharRange) it.next(); 229 if (range.contains(ch)) { 230 return true; 231 } 232 } 233 return false; 234 } 235 236 249 public boolean equals(Object obj) { 250 if (obj == this) { 251 return true; 252 } 253 if (obj instanceof CharSet == false) { 254 return false; 255 } 256 CharSet other = (CharSet) obj; 257 return set.equals(other.set); 258 } 259 260 266 public int hashCode() { 267 return 89 + set.hashCode(); 268 } 269 270 275 public String toString() { 276 return set.toString(); 277 } 278 279 } 280 | Popular Tags |