KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > util > Range


1 /*
2  * Range.java, a simple data structure to express min and max.
3  * Copyright (C) Achim Westermann, created on 09.09.2004, 12:38:21
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23 package info.monitorenter.util;
24
25 import java.io.Serializable JavaDoc;
26
27 /**
28  * <p>
29  * A simple data structure that defines a minimum and a maximum and knows, what
30  * lies within it and what not.
31  * </p>
32  *
33  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
34  *
35  */

36 public class Range implements Serializable JavaDoc {
37
38   /**
39    * Generated for <code>serialVersionUID</code>.
40    */

41   private static final long serialVersionUID = 3760565278089754419L;
42
43   /** The unbounded range. */
44   public static final Range RANGE_UNBOUNDED = new Range(-Double.MAX_VALUE, +Double.MAX_VALUE);
45
46   /** The lower bound of this range. */
47   protected double m_min;
48
49   /** The upper bound of this range. */
50   protected double m_max;
51
52   /**
53    * Constructs a new Range that covers the given bounds.
54    * <p>
55    *
56    * @param min
57    * the lower bound for the range.
58    *
59    * @param max
60    * the upper bound for the range.
61    */

62   public Range(final double min, final double max) {
63     if (min == Double.NaN) {
64       throw new IllegalArgumentException JavaDoc("Cannot work on Double.NaN for min.");
65     }
66     MathUtil.assertDouble(min);
67     MathUtil.assertDouble(max);
68     if (min < max) {
69       this.m_min = min;
70       this.m_max = max;
71     } else {
72       this.m_min = max;
73       this.m_max = min;
74     }
75   }
76
77   /**
78    * Returns the lower bound of this range.
79    * <p>
80    *
81    * @return the lower bound of this range.
82    */

83   public double getMin() {
84     return this.m_min;
85   }
86
87   /**
88    * Returns the upper bound of this range.
89    * <p>
90    *
91    * @return the upper bound of this range.
92    */

93   public double getMax() {
94     return this.m_max;
95   }
96
97   /**
98    * Returns the extent of this range.
99    * <p>
100    *
101    * @return the extent of this range.
102    */

103   public double getExtent() {
104     return this.m_max - this.m_min;
105   }
106
107   /**
108    * Force this Range to cover the given value.
109    * <p>
110    *
111    * @param contain
112    * the value that has to be contained within this range.
113    *
114    * @return true, if an internal modification of one bound took place, false
115    * else.
116    *
117    */

118   public boolean ensureContained(final double contain) {
119     boolean ret = false;
120     if (contain < this.m_min) {
121       ret = true;
122       this.m_min = contain;
123     } else if (contain > this.m_max) {
124       ret = true;
125       this.m_max = contain;
126     }
127     return ret;
128   }
129
130   /**
131    * Returns true if the given value is covered by this range.
132    * <p>
133    *
134    * @param contained
135    * the value to test wether it is contained within this range.
136    *
137    * @return true if the given value is covered by this range.
138    */

139   public boolean isContained(final double contained) {
140     return ((this.m_min <= contained) && (this.m_max >= contained));
141   }
142
143   /**
144    * @see java.lang.Object#toString()
145    */

146   public String JavaDoc toString() {
147     StringBuffer JavaDoc ret = new StringBuffer JavaDoc("Range[");
148     ret.append(this.m_min).append(',');
149     ret.append(this.m_max).append(']');
150     return ret.toString();
151   }
152 }
153
Popular Tags