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. /**
  56. * <p>Represents a range of {@link Number} objects.</p>
  57. *
  58. * <p>This class uses <code>double</code> comparisons. This means that it
  59. * is unsuitable for dealing with large <code>Long</code>, <code>BigDecimal</code>
  60. * or <code>BigInteger</code> numbers.</p>
  61. *
  62. * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
  63. * @author Stephen Colebourne
  64. * @since 1.0
  65. * @version $Revision: 1.10 $ $Date: 2003/08/18 02:22:23 $
  66. *
  67. * @deprecated Use one of the Range classes in org.apache.commons.lang.math.
  68. * Class will be removed in Commons Lang 3.0.
  69. *
  70. */
  71. public final class NumberRange {
  72. /* The minimum number in this range. */
  73. private final Number min;
  74. /* The maximum number in this range. */
  75. private final Number max;
  76. /**
  77. * <p>Constructs a new <code>NumberRange</code> using
  78. * <code>number</code> as both the minimum and maximum in
  79. * this range.</p>
  80. *
  81. * @param num the number to use for this range
  82. * @throws NullPointerException if the number is <code>null</code>
  83. */
  84. public NumberRange(Number num) {
  85. if (num == null) {
  86. throw new NullPointerException("The number must not be null");
  87. }
  88. this.min = num;
  89. this.max = num;
  90. }
  91. /**
  92. * <p>Constructs a new <code>NumberRange</code> with the specified
  93. * minimum and maximum numbers.</p>
  94. *
  95. * <p><em>If the maximum is less than the minimum, the range will be constructed
  96. * from the minimum value to the minimum value, not what you would expect!.</em></p>
  97. *
  98. * @param min the minimum number in this range
  99. * @param max the maximum number in this range
  100. * @throws NullPointerException if either the minimum or maximum number is
  101. * <code>null</code>
  102. */
  103. public NumberRange(Number min, Number max) {
  104. if (min == null) {
  105. throw new NullPointerException("The minimum value must not be null");
  106. } else if (max == null) {
  107. throw new NullPointerException("The maximum value must not be null");
  108. }
  109. if (max.doubleValue() < min.doubleValue()) {
  110. this.min = this.max = min;
  111. } else {
  112. this.min = min;
  113. this.max = max;
  114. }
  115. }
  116. /**
  117. * <p>Returns the minimum number in this range.</p>
  118. *
  119. * @return the minimum number in this range
  120. */
  121. public Number getMinimum() {
  122. return min;
  123. }
  124. /**
  125. * <p>Returns the maximum number in this range.</p>
  126. *
  127. * @return the maximum number in this range
  128. */
  129. public Number getMaximum() {
  130. return max;
  131. }
  132. /**
  133. * <p>Tests whether the specified <code>number</code> occurs within
  134. * this range using <code>double</code> comparison.</p>
  135. *
  136. * @param number the number to test
  137. * @return <code>true</code> if the specified number occurs within this
  138. * range; otherwise, <code>false</code>
  139. */
  140. public boolean includesNumber(Number number) {
  141. if (number == null) {
  142. return false;
  143. } else {
  144. return !(min.doubleValue() > number.doubleValue()) &&
  145. !(max.doubleValue() < number.doubleValue());
  146. }
  147. }
  148. /**
  149. * <p>Tests whether the specified range occurs entirely within this
  150. * range using <code>double</code> comparison.</p>
  151. *
  152. * @param range the range to test
  153. * @return <code>true</code> if the specified range occurs entirely within
  154. * this range; otherwise, <code>false</code>
  155. */
  156. public boolean includesRange(NumberRange range) {
  157. if (range == null) {
  158. return false;
  159. } else {
  160. return includesNumber(range.min) && includesNumber(range.max);
  161. }
  162. }
  163. /**
  164. * <p>Tests whether the specified range overlaps with this range
  165. * using <code>double</code> comparison.</p>
  166. *
  167. * @param range the range to test
  168. * @return <code>true</code> if the specified range overlaps with this
  169. * range; otherwise, <code>false</code>
  170. */
  171. public boolean overlaps(NumberRange range) {
  172. if (range == null) {
  173. return false;
  174. } else {
  175. return range.includesNumber(min) || range.includesNumber(max) ||
  176. includesRange(range);
  177. }
  178. }
  179. /**
  180. * <p>Indicates whether some other <code>Object</code> is
  181. * "equal" to this one.</p>
  182. *
  183. * @param obj the reference object with which to compare
  184. * @return <code>true</code> if this object is the same as the obj
  185. * argument; <code>false</code> otherwise
  186. */
  187. public boolean equals(Object obj) {
  188. if (obj == this) {
  189. return true;
  190. } else if (!(obj instanceof NumberRange)) {
  191. return false;
  192. } else {
  193. NumberRange range = (NumberRange)obj;
  194. return min.equals(range.min) && max.equals(range.max);
  195. }
  196. }
  197. /**
  198. * <p>Returns a hash code value for this object.</p>
  199. *
  200. * @return a hash code value for this object
  201. */
  202. public int hashCode() {
  203. int result = 17;
  204. result = 37 * result + min.hashCode();
  205. result = 37 * result + max.hashCode();
  206. return result;
  207. }
  208. /**
  209. * <p>Returns the string representation of this range.</p>
  210. *
  211. * <p>This string is the string representation of the minimum and
  212. * maximum numbers in the range, separated by a hyphen. If a number
  213. * is negative, then it is enclosed in parentheses.</p>
  214. *
  215. * @return the string representation of this range
  216. */
  217. public String toString() {
  218. StringBuffer sb = new StringBuffer();
  219. if (min.doubleValue() < 0) {
  220. sb.append('(')
  221. .append(min)
  222. .append(')');
  223. } else {
  224. sb.append(min);
  225. }
  226. sb.append('-');
  227. if (max.doubleValue() < 0) {
  228. sb.append('(')
  229. .append(max)
  230. .append(')');
  231. } else {
  232. sb.append(max);
  233. }
  234. return sb.toString();
  235. }
  236. }