Debian Codegraph @

src/com/jidesoft/range/NumericRange.java

raw
1 /*
2 * @(#)NumericRange.java
3 *
4 * 2002 - 2012 JIDE Software Incorporated. All rights reserved.
5 * Copyright (c) 2005 - 2012 Catalysoft Limited. All rights reserved.
6 */
7
8 package com.jidesoft.range;
9
10 /**
11 * Specifies upper and lower bounds for a range of values
12 *
13 * @author Simon
14 */
15 public class NumericRange extends AbstractNumericRange<Double> {
16 protected double _min;
17 protected double _max;
18
19 /**
20 * Creates a numeric range with minimum 0.0 and maximum of 1.0
21 */
22 public NumericRange() {
23 this(0.0, 1.0);
24 }
25
26 /**
27 * Create a numeric range by supplying minimum and maximum values
28 *
29 * @param min the minumum
30 * @param max the maximum
31 */
32 public NumericRange(double min, double max) {
33 super();
34 _min = Math.min(min, max);
35 _max = Math.max(min, max);
36 }
37
38 /**
39 * Creates a copy of the supplied NumericRange
40 *
41 * @param numericRange the NumericRange instance to copy
42 */
43 public NumericRange(NumericRange numericRange) {
44 this(numericRange.minimum(), numericRange.maximum());
45 }
46
47 @Override
48 public Range<Double> copy() {
49 return new NumericRange(this);
50 }
51
52 /**
53 * @return the minimum value
54 */
55 public double minimum() {
56 return _min;
57 }
58
59 /**
60 * @return the maximum value
61 */
62 public double maximum() {
63 return _max;
64 }
65
66 /**
67 * @return the minimum value
68 */
69 public double getMin() {
70 return _min;
71 }
72
73 /**
74 * Sets the minimum value
75 *
76 * @param min the new minimum value.
77 */
78 public void setMin(double min) {
79 double old = _min;
80 if (old == min) {
81 return;
82 }
83 assert min <= _max;
84 _min = min;
85 firePropertyChange(PROPERTY_MIN, old, min);
86 }
87
88 /**
89 * @return the maximum value
90 */
91 public double getMax() {
92 return _max;
93 }
94
95 /**
96 * Sets the maximum value
97 *
98 * @param max the new maximum value.
99 */
100 public void setMax(double max) {
101 double old = _max;
102 if (old == max) {
103 return;
104 }
105 assert max >= _min;
106 _max = max;
107 firePropertyChange(PROPERTY_MAX, old, max);
108 }
109
110 /**
111 * @return the size of the range
112 */
113 public double size() {
114 return _max - _min;
115 }
116
117 /**
118 * @return the minimum() value for the range
119 */
120 public Double lower() {
121 return minimum();
122 }
123
124 /**
125 * Sets the minimum and maximum values of the range in a single call
126 */
127 public void adjust(Double lower, Double upper) {
128 double size = size(); // save it
129 if (lower != null) {
130 setMin(lower);
131 }
132 else if (upper != null) {
133 setMin(upper - size);
134 }
135 if (upper != null) {
136 setMax(upper);
137 }
138 else if (lower != null) {
139 setMin(lower + size);
140 }
141 }
142
143 /**
144 * @return the maximum() value for the range
145 */
146 public Double upper() {
147 return maximum();
148 }
149
150 /**
151 * Determines whether the range contains the supplied value
152 */
153 public boolean contains(Double x) {
154 return x != null && x >= _min && x <= _max;
155 }
156
157 /**
158 * Creates a new NumericRange by enlarging this numeric range about its mid-point. For example to make it 10%
159 * bigger, use a stretch factor of 1.1. Note that this method can also be used to shrink a NumericRange.
160 *
161 * @param stretchFactor the multiplication factor for the enlargement
162 * @return a new NumericRange
163 */
164 public NumericRange stretch(double stretchFactor) {
165 return stretch(stretchFactor, stretchFactor);
166 }
167
168 /**
169 * Creates a new NumericRange by enlarging this numeric range about its mid-point. For example to make it 10%
170 * bigger, use a stretch factor of 1.1. Note that this method can also be used to shrink a NumericRange.
171 *
172 * @param stretchFactorForLower the multiplication factor for the enlargement for the lower range
173 * @param stretchFactorForUpper the multiplication factor for the enlargement for the upper range
174 * @return a new NumericRange
175 */
176 public NumericRange stretch(double stretchFactorForLower, double stretchFactorForUpper) {
177 double mid = (_max + _min) / 2.0;
178 double halfSize = size() / 2.0;
179 return new NumericRange(mid - halfSize * stretchFactorForLower, mid + halfSize * stretchFactorForUpper);
180 }
181
182
183 public Range<Double> createIntermediate(Range<Double> target, double position) {
184 double sourceMin = this.minimum();
185 double sourceMax = this.maximum();
186 double targetMin = target.minimum();
187 double targetMax = target.maximum();
188 double min = sourceMin + position * (targetMin - sourceMin);
189 double max= sourceMax + position * (targetMax - sourceMax);
190 return new NumericRange(min, max);
191 }
192
193 /**
194 * Test for equality based on the values of min and max
195 */
196 @Override
197 public boolean equals(Object other) {
198 if (other instanceof NumericRange) {
199 NumericRange otherRange = (NumericRange) other;
200 return _min == otherRange._min && _max == otherRange._max;
201 }
202 else {
203 return false;
204 }
205 }
206
207 @Override
208 public int hashCode() {
209 return (int)(_max * 29 + _min);
210 }
211
212 @Override
213 public String toString() {
214 return String.format("#<NumericRange min=%f max=%f>", _min, _max);
215 }
216
217 /**
218 * Creates a new NumericRange as the union of two existing NumericRanges. The date format of the first time range is
219 * retained in the returned result.
220 *
221 * @param r1 the first NumericRange
222 * @param r2 the second NumericRange
223 * @return the union of the supplied NumericRanges
224 */
225 public static NumericRange union(NumericRange r1, NumericRange r2) {
226 if (r1 == null) {
227 return r2;
228 }
229 else if (r2 == null) {
230 return r1;
231 }
232 double r1Min = r1._min;
233 double r2Min = r2._min;
234 double r1Max = r1._max;
235 double r2Max = r2._max;
236
237 double min = Math.min(r1Min, r2Min);
238 double max = Math.max(r1Max, r2Max);
239 return new NumericRange(min, max);
240 }
241 }