Source Code Cross Referenced for NumberRange.java in  » Library » Apache-common-lang » org » apache » commons » lang » math » 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.math 
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.math;
018:
019:        import java.io.Serializable;
020:
021:        /**
022:         * <p><code>NumberRange</code> represents an inclusive range of 
023:         * {@link java.lang.Number} objects of the same type.</p>
024:         *
025:         * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
026:         * @author Stephen Colebourne
027:         * @since 2.0 (previously in org.apache.commons.lang)
028:         * @version $Id: NumberRange.java 437554 2006-08-28 06:21:41Z bayard $
029:         */
030:        public final class NumberRange extends Range implements  Serializable {
031:
032:            /**
033:             * Required for serialization support.
034:             * 
035:             * @see java.io.Serializable
036:             */
037:            private static final long serialVersionUID = 71849363892710L;
038:
039:            /**
040:             * The minimum number in this range.
041:             */
042:            private final Number min;
043:            /**
044:             * The maximum number in this range.
045:             */
046:            private final Number max;
047:
048:            /**
049:             * Cached output hashCode (class is immutable).
050:             */
051:            private transient int hashCode = 0;
052:            /**
053:             * Cached output toString (class is immutable).
054:             */
055:            private transient String toString = null;
056:
057:            /**
058:             * <p>Constructs a new <code>NumberRange</code> using the specified
059:             * number as both the minimum and maximum in this range.</p>
060:             *
061:             * @param num the number to use for this range
062:             * @throws IllegalArgumentException if the number is <code>null</code>
063:             * @throws IllegalArgumentException if the number doesn't implement <code>Comparable</code>
064:             * @throws IllegalArgumentException if the number is <code>Double.NaN</code> or <code>Float.NaN</code>
065:             */
066:            public NumberRange(Number num) {
067:                if (num == null) {
068:                    throw new IllegalArgumentException(
069:                            "The number must not be null");
070:                }
071:                if (num instanceof  Comparable == false) {
072:                    throw new IllegalArgumentException(
073:                            "The number must implement Comparable");
074:                }
075:                if (num instanceof  Double && ((Double) num).isNaN()) {
076:                    throw new IllegalArgumentException(
077:                            "The number must not be NaN");
078:                }
079:                if (num instanceof  Float && ((Float) num).isNaN()) {
080:                    throw new IllegalArgumentException(
081:                            "The number must not be NaN");
082:                }
083:
084:                this .min = num;
085:                this .max = num;
086:            }
087:
088:            /**
089:             * <p>Constructs a new <code>NumberRange</code> with the specified
090:             * minimum and maximum numbers (both inclusive).</p>
091:             * 
092:             * <p>The arguments may be passed in the order (min,max) or (max,min). The
093:             * {@link #getMinimumNumber()} and {@link #getMaximumNumber()} methods will return the
094:             * correct value.</p>
095:             * 
096:             * <p>This constructor is designed to be used with two <code>Number</code>
097:             * objects of the same type. If two objects of different types are passed in,
098:             * an exception is thrown.</p>
099:             *
100:             * @param num1  first number that defines the edge of the range, inclusive
101:             * @param num2  second number that defines the edge of the range, inclusive
102:             * @throws IllegalArgumentException if either number is <code>null</code>
103:             * @throws IllegalArgumentException if the numbers are of different types
104:             * @throws IllegalArgumentException if the numbers don't implement <code>Comparable</code>
105:             */
106:            public NumberRange(Number num1, Number num2) {
107:                if (num1 == null || num2 == null) {
108:                    throw new IllegalArgumentException(
109:                            "The numbers must not be null");
110:                }
111:                if (num1.getClass() != num2.getClass()) {
112:                    throw new IllegalArgumentException(
113:                            "The numbers must be of the same type");
114:                }
115:                if (num1 instanceof  Comparable == false) {
116:                    throw new IllegalArgumentException(
117:                            "The numbers must implement Comparable");
118:                }
119:                if (num1 instanceof  Double) {
120:                    if (((Double) num1).isNaN() || ((Double) num2).isNaN()) {
121:                        throw new IllegalArgumentException(
122:                                "The number must not be NaN");
123:                    }
124:                } else if (num1 instanceof  Float) {
125:                    if (((Float) num1).isNaN() || ((Float) num2).isNaN()) {
126:                        throw new IllegalArgumentException(
127:                                "The number must not be NaN");
128:                    }
129:                }
130:
131:                int compare = ((Comparable) num1).compareTo(num2);
132:                if (compare == 0) {
133:                    this .min = num1;
134:                    this .max = num1;
135:                } else if (compare > 0) {
136:                    this .min = num2;
137:                    this .max = num1;
138:                } else {
139:                    this .min = num1;
140:                    this .max = num2;
141:                }
142:            }
143:
144:            // Accessors
145:            //--------------------------------------------------------------------
146:
147:            /**
148:             * <p>Returns the minimum number in this range.</p>
149:             *
150:             * @return the minimum number in this range
151:             */
152:            public Number getMinimumNumber() {
153:                return min;
154:            }
155:
156:            /**
157:             * <p>Returns the maximum number in this range.</p>
158:             *
159:             * @return the maximum number in this range
160:             */
161:            public Number getMaximumNumber() {
162:                return max;
163:            }
164:
165:            // Tests
166:            //--------------------------------------------------------------------
167:
168:            /**
169:             * <p>Tests whether the specified <code>number</code> occurs within
170:             * this range.</p>
171:             * 
172:             * <p><code>null</code> is handled and returns <code>false</code>.</p>
173:             *
174:             * @param number  the number to test, may be <code>null</code>
175:             * @return <code>true</code> if the specified number occurs within this range
176:             * @throws IllegalArgumentException if the number is of a different type to the range
177:             */
178:            public boolean containsNumber(Number number) {
179:                if (number == null) {
180:                    return false;
181:                }
182:                if (number.getClass() != min.getClass()) {
183:                    throw new IllegalArgumentException(
184:                            "The number must be of the same type as the range numbers");
185:                }
186:                int compareMin = ((Comparable) min).compareTo(number);
187:                int compareMax = ((Comparable) max).compareTo(number);
188:                return compareMin <= 0 && compareMax >= 0;
189:            }
190:
191:            // Range tests
192:            //--------------------------------------------------------------------
193:            // use Range implementations
194:
195:            // Basics
196:            //--------------------------------------------------------------------
197:
198:            /**
199:             * <p>Compares this range to another object to test if they are equal.</p>.
200:             * 
201:             * <p>To be equal, the class, minimum and maximum must be equal.</p>
202:             *
203:             * @param obj the reference object with which to compare
204:             * @return <code>true</code> if this object is equal
205:             */
206:            public boolean equals(Object obj) {
207:                if (obj == this ) {
208:                    return true;
209:                }
210:                if (obj instanceof  NumberRange == false) {
211:                    return false;
212:                }
213:                NumberRange range = (NumberRange) obj;
214:                return min.equals(range.min) && max.equals(range.max);
215:            }
216:
217:            /**
218:             * <p>Gets a hashCode for the range.</p>
219:             *
220:             * @return a hash code value for this object
221:             */
222:            public int hashCode() {
223:                if (hashCode == 0) {
224:                    hashCode = 17;
225:                    hashCode = 37 * hashCode + getClass().hashCode();
226:                    hashCode = 37 * hashCode + min.hashCode();
227:                    hashCode = 37 * hashCode + max.hashCode();
228:                }
229:                return hashCode;
230:            }
231:
232:            /**
233:             * <p>Gets the range as a <code>String</code>.</p>
234:             *
235:             * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
236:             *
237:             * @return the <code>String</code> representation of this range
238:             */
239:            public String toString() {
240:                if (toString == null) {
241:                    StringBuffer buf = new StringBuffer(32);
242:                    buf.append("Range[");
243:                    buf.append(min);
244:                    buf.append(',');
245:                    buf.append(max);
246:                    buf.append(']');
247:                    toString = buf.toString();
248:                }
249:                return toString;
250:            }
251:
252:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.