Source Code Cross Referenced for NumberRange.java in  » Library » Apache-common-lang » org » apache » commons » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common lang » org.apache.commons.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.lang;
018:
019:        /**
020:         * <p>Represents a range of {@link Number} objects.</p>
021:         * 
022:         * <p>This class uses <code>double</code> comparisons. This means that it
023:         * is unsuitable for dealing with large <code>Long</code>, <code>BigDecimal</code>
024:         * or <code>BigInteger</code> numbers.</p>
025:         *
026:         * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
027:         * @author Stephen Colebourne
028:         * @since 1.0
029:         * @version $Revision: 437554 $ $Date: 2006-08-27 23:21:41 -0700 (Sun, 27 Aug 2006) $
030:         * 
031:         * @deprecated Use one of the Range classes in org.apache.commons.lang.math.
032:         *             Class will be removed in Commons Lang 3.0.
033:         * 
034:         */
035:        public final class NumberRange {
036:
037:            /* The minimum number in this range. */
038:            private final Number min;
039:
040:            /* The maximum number in this range. */
041:            private final Number max;
042:
043:            /**
044:             * <p>Constructs a new <code>NumberRange</code> using
045:             * <code>number</code> as both the minimum and maximum in
046:             * this range.</p>
047:             *
048:             * @param num the number to use for this range
049:             * @throws NullPointerException if the number is <code>null</code>
050:             */
051:            public NumberRange(Number num) {
052:                if (num == null) {
053:                    throw new NullPointerException(
054:                            "The number must not be null");
055:                }
056:
057:                this .min = num;
058:                this .max = num;
059:            }
060:
061:            /**
062:             * <p>Constructs a new <code>NumberRange</code> with the specified
063:             * minimum and maximum numbers.</p>
064:             * 
065:             * <p><em>If the maximum is less than the minimum, the range will be constructed
066:             * from the minimum value to the minimum value, not what you would expect!.</em></p>
067:             *
068:             * @param min the minimum number in this range
069:             * @param max the maximum number in this range
070:             * @throws NullPointerException if either the minimum or maximum number is
071:             *  <code>null</code>
072:             */
073:            public NumberRange(Number min, Number max) {
074:                if (min == null) {
075:                    throw new NullPointerException(
076:                            "The minimum value must not be null");
077:                } else if (max == null) {
078:                    throw new NullPointerException(
079:                            "The maximum value must not be null");
080:                }
081:
082:                if (max.doubleValue() < min.doubleValue()) {
083:                    this .min = this .max = min;
084:                } else {
085:                    this .min = min;
086:                    this .max = max;
087:                }
088:            }
089:
090:            /**
091:             * <p>Returns the minimum number in this range.</p>
092:             *
093:             * @return the minimum number in this range
094:             */
095:            public Number getMinimum() {
096:                return min;
097:            }
098:
099:            /**
100:             * <p>Returns the maximum number in this range.</p>
101:             *
102:             * @return the maximum number in this range
103:             */
104:            public Number getMaximum() {
105:                return max;
106:            }
107:
108:            /**
109:             * <p>Tests whether the specified <code>number</code> occurs within
110:             * this range using <code>double</code> comparison.</p>
111:             *
112:             * @param number the number to test
113:             * @return <code>true</code> if the specified number occurs within this
114:             *  range; otherwise, <code>false</code>
115:             */
116:            public boolean includesNumber(Number number) {
117:                if (number == null) {
118:                    return false;
119:                } else {
120:                    return !(min.doubleValue() > number.doubleValue())
121:                            && !(max.doubleValue() < number.doubleValue());
122:                }
123:            }
124:
125:            /**
126:             * <p>Tests whether the specified range occurs entirely within this
127:             * range using <code>double</code> comparison.</p>
128:             *
129:             * @param range the range to test
130:             * @return <code>true</code> if the specified range occurs entirely within
131:             *  this range; otherwise, <code>false</code>
132:             */
133:            public boolean includesRange(NumberRange range) {
134:                if (range == null) {
135:                    return false;
136:                } else {
137:                    return includesNumber(range.min)
138:                            && includesNumber(range.max);
139:                }
140:            }
141:
142:            /**
143:             * <p>Tests whether the specified range overlaps with this range
144:             * using <code>double</code> comparison.</p>
145:             *
146:             * @param range the range to test
147:             * @return <code>true</code> if the specified range overlaps with this
148:             *  range; otherwise, <code>false</code>
149:             */
150:            public boolean overlaps(NumberRange range) {
151:                if (range == null) {
152:                    return false;
153:                } else {
154:                    return range.includesNumber(min)
155:                            || range.includesNumber(max)
156:                            || includesRange(range);
157:                }
158:            }
159:
160:            /**
161:             * <p>Indicates whether some other <code>Object</code> is
162:             * &quot;equal&quot; to this one.</p>
163:             *
164:             * @param obj the reference object with which to compare
165:             * @return <code>true</code> if this object is the same as the obj
166:             *  argument; <code>false</code> otherwise
167:             */
168:            public boolean equals(Object obj) {
169:                if (obj == this ) {
170:                    return true;
171:                } else if (!(obj instanceof  NumberRange)) {
172:                    return false;
173:                } else {
174:                    NumberRange range = (NumberRange) obj;
175:                    return min.equals(range.min) && max.equals(range.max);
176:                }
177:            }
178:
179:            /**
180:             * <p>Returns a hash code value for this object.</p>
181:             *
182:             * @return a hash code value for this object
183:             */
184:            public int hashCode() {
185:                int result = 17;
186:                result = 37 * result + min.hashCode();
187:                result = 37 * result + max.hashCode();
188:                return result;
189:            }
190:
191:            /**
192:             * <p>Returns the string representation of this range.</p>
193:             *
194:             * <p>This string is the string representation of the minimum and
195:             * maximum numbers in the range, separated by a hyphen. If a number
196:             * is negative, then it is enclosed in parentheses.</p>
197:             *
198:             * @return the string representation of this range
199:             */
200:            public String toString() {
201:                StringBuffer sb = new StringBuffer();
202:
203:                if (min.doubleValue() < 0) {
204:                    sb.append('(').append(min).append(')');
205:                } else {
206:                    sb.append(min);
207:                }
208:
209:                sb.append('-');
210:
211:                if (max.doubleValue() < 0) {
212:                    sb.append('(').append(max).append(')');
213:                } else {
214:                    sb.append(max);
215:                }
216:
217:                return sb.toString();
218:            }
219:
220:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.