1 16 package org.apache.commons.lang; 17 18 import java.io.Serializable ; 19 20 32 public final class CharRange implements Serializable { 33 34 35 private static final long serialVersionUID = 8270183163158333422L; 36 37 38 private final char start; 39 40 private final char end; 41 42 private final boolean negated; 43 44 45 private transient String iToString; 46 47 53 public CharRange(char ch) { 54 this(ch, ch, false); 55 } 56 57 66 public CharRange(char ch, boolean negated) { 67 this(ch, ch, negated); 68 } 69 70 76 public CharRange(char start, char end) { 77 this(start, end, false); 78 } 79 80 94 public CharRange(char start, char end, boolean negated) { 95 super(); 96 if (start > end) { 97 char temp = start; 98 start = end; 99 end = temp; 100 } 101 102 this.start = start; 103 this.end = end; 104 this.negated = negated; 105 } 106 107 114 public char getStart() { 115 return this.start; 116 } 117 118 123 public char getEnd() { 124 return this.end; 125 } 126 127 135 public boolean isNegated() { 136 return negated; 137 } 138 139 147 public boolean contains(char ch) { 148 return (ch >= start && ch <= end) != negated; 149 } 150 151 159 public boolean contains(CharRange range) { 160 if (range == null) { 161 throw new IllegalArgumentException ("The Range must not be null"); 162 } 163 if (negated) { 164 if (range.negated) { 165 return start >= range.start && end <= range.end; 166 } else { 167 return range.end < start || range.start > end; 168 } 169 } else { 170 if (range.negated) { 171 return start == 0 && end == Character.MAX_VALUE; 172 } else { 173 return start <= range.start && end >= range.end; 174 } 175 } 176 } 177 178 187 public boolean equals(Object obj) { 188 if (obj == this) { 189 return true; 190 } 191 if (obj instanceof CharRange == false) { 192 return false; 193 } 194 CharRange other = (CharRange) obj; 195 return start == other.start && end == other.end && negated == other.negated; 196 } 197 198 203 public int hashCode() { 204 return 83 + start + 7 * end + (negated ? 1 : 0); 205 } 206 207 212 public String toString() { 213 if (iToString == null) { 214 StringBuffer buf = new StringBuffer (4); 215 if (isNegated()) { 216 buf.append('^'); 217 } 218 buf.append(start); 219 if (start != end) { 220 buf.append('-'); 221 buf.append(end); 222 } 223 iToString = buf.toString(); 224 } 225 return iToString; 226 } 227 228 } 229 | Popular Tags |