1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang;
  55. import java.io.Serializable;
  56. /**
  57. * <p>A contiguous range of characters, optionally negated.</p>
  58. *
  59. * <p>Instances are immutable.</p>
  60. *
  61. * @author Henri Yandell
  62. * @author Stephen Colebourne
  63. * @author Chris Feldhacker
  64. * @author Gary Gregory
  65. * @since 1.0
  66. * @version $Id: CharRange.java,v 1.15 2003/08/22 17:25:33 ggregory Exp $
  67. */
  68. public final class CharRange implements Serializable {
  69. /** Serialization lock, Lang version 2.0. */
  70. private static final long serialVersionUID = 8270183163158333422L;
  71. /** The first character, inclusive, in the range. */
  72. private final char start;
  73. /** The last character, inclusive, in the range. */
  74. private final char end;
  75. /** True if the range is everything except the characters specified. */
  76. private final boolean negated;
  77. /** Cached toString. */
  78. private transient String iToString;
  79. //-----------------------------------------------------------------------
  80. /**
  81. * <p>Constructs a <code>CharRange</code> over a single character.</p>
  82. *
  83. * @param ch only character in this range
  84. */
  85. public CharRange(char ch) {
  86. this(ch, ch, false);
  87. }
  88. /**
  89. * <p>Constructs a <code>CharRange</code> over a single character,
  90. * optionally negating the range.</p>
  91. *
  92. * <p>A negated range includes everything except the specified char.</p>
  93. *
  94. * @param ch only character in this range
  95. * @param negated true to express everything except the range
  96. */
  97. public CharRange(char ch, boolean negated) {
  98. this(ch, ch, negated);
  99. }
  100. /**
  101. * <p>Constructs a <code>CharRange</code> over a set of characters.</p>
  102. *
  103. * @param start first character, inclusive, in this range
  104. * @param end last character, inclusive, in this range
  105. */
  106. public CharRange(char start, char end) {
  107. this(start, end, false);
  108. }
  109. /**
  110. * <p>Constructs a <code>CharRange</code> over a set of characters,
  111. * optionally negating the range.</p>
  112. *
  113. * <p>A negated range includes everything except that defined by the
  114. * start and end characters.</p>
  115. *
  116. * <p>If start and end are in the wrong order, they are reversed.
  117. * Thus <code>a-e</code> is the same as <code>e-a</code>.</p>
  118. *
  119. * @param start first character, inclusive, in this range
  120. * @param end last character, inclusive, in this range
  121. * @param negated true to express everything except the range
  122. */
  123. public CharRange(char start, char end, boolean negated) {
  124. super();
  125. if (start > end) {
  126. char temp = start;
  127. start = end;
  128. end = temp;
  129. }
  130. this.start = start;
  131. this.end = end;
  132. this.negated = negated;
  133. }
  134. // Accessors
  135. //-----------------------------------------------------------------------
  136. /**
  137. * <p>Gets the start character for this character range.</p>
  138. *
  139. * @return the start char (inclusive)
  140. */
  141. public char getStart() {
  142. return this.start;
  143. }
  144. /**
  145. * <p>Gets the end character for this character range.</p>
  146. *
  147. * @return the end char (inclusive)
  148. */
  149. public char getEnd() {
  150. return this.end;
  151. }
  152. /**
  153. * <p>Is this <code>CharRange</code> negated.</p>
  154. *
  155. * <p>A negated range includes everything except that defined by the
  156. * start and end characters.</p>
  157. *
  158. * @return <code>true</code> is negated
  159. */
  160. public boolean isNegated() {
  161. return negated;
  162. }
  163. // Contains
  164. //-----------------------------------------------------------------------
  165. /**
  166. * <p>Is the character specified contained in this range.</p>
  167. *
  168. * @param ch the character to check
  169. * @return <code>true</code> if this range contains the input character
  170. */
  171. public boolean contains(char ch) {
  172. return ((ch >= start && ch <= end) != negated);
  173. }
  174. /**
  175. * <p>Are all the characters of the passed in range contained in
  176. * this range.</p>
  177. *
  178. * @param range the range to check against
  179. * @return <code>true</code> if this range entirely contains the input range
  180. * @throws IllegalArgumentException if <code>null</code> input
  181. */
  182. public boolean contains(CharRange range) {
  183. if (range == null) {
  184. throw new IllegalArgumentException("The Range must not be null");
  185. }
  186. if (negated) {
  187. if (range.negated) {
  188. return (start >= range.start && end <= range.end);
  189. } else {
  190. return (range.end < start || range.start > end);
  191. }
  192. } else {
  193. if (range.negated) {
  194. return (start == 0 && end == Character.MAX_VALUE);
  195. } else {
  196. return (start <= range.start && end >= range.end);
  197. }
  198. }
  199. }
  200. // Basics
  201. //-----------------------------------------------------------------------
  202. /**
  203. * <p>Compares two CharRange objects, returning true if they represent
  204. * exactly the same range of characters defined in the same way.</p>
  205. *
  206. * @param obj the object to compare to
  207. * @return true if equal
  208. */
  209. public boolean equals(Object obj) {
  210. if (obj == this) {
  211. return true;
  212. }
  213. if (obj instanceof CharRange == false) {
  214. return false;
  215. }
  216. CharRange other = (CharRange) obj;
  217. return (start == other.start && end == other.end && negated == other.negated);
  218. }
  219. /**
  220. * <p>Gets a hashCode compatable with the equals method.</p>
  221. *
  222. * @return a suitable hashCode
  223. */
  224. public int hashCode() {
  225. return 83 + start + 7 * end + (negated ? 1 : 0);
  226. }
  227. /**
  228. * <p>Gets a string representation of the character range.</p>
  229. *
  230. * @return string representation of this range
  231. */
  232. public String toString() {
  233. if (iToString == null) {
  234. StringBuffer buf = new StringBuffer(4);
  235. if (isNegated()) {
  236. buf.append('^');
  237. }
  238. buf.append(start);
  239. if (start != end) {
  240. buf.append('-');
  241. buf.append(end);
  242. }
  243. iToString = buf.toString();
  244. }
  245. return iToString;
  246. }
  247. }