Debian Codegraph @

src/com/jidesoft/range/BigDecimalRange.java

raw
1 /*
2 * @(#)BigDecimalRange.java 5/22/2014
3 *
4 * Copyright 2002 - 2014 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.range;
8
9 import java.math.BigDecimal;
10
11 /**
12 * Specifies upper and lower bounds for a range of values
13 */
14 public class BigDecimalRange extends AbstractRange<BigDecimal> {
15 protected BigDecimal _min;
16 protected BigDecimal _max;
17 private static final BigDecimal TWO = new BigDecimal(2);
18
19 /**
20 * Creates a numeric range with minimum 0.0 and maximum of 1.0
21 */
22 public BigDecimalRange() {
23 this(BigDecimal.ZERO, BigDecimal.ONE);
24 }
25
26 /**
27 * Create a numeric range by supplying minimum and maximum values
28 *
29 * @param min the minimum
30 * @param max the maximum
31 */
32 public BigDecimalRange(BigDecimal min, BigDecimal max) {
33 super();
34 boolean inOrder = min.compareTo(max) < 0;
35 _min = inOrder ? min : max;
36 _max = inOrder ? max : min;
37 }
38
39 /**
40 * Creates a copy of the supplied BigDecimalRange
41 *
42 * @param BigDecimalRange the BigDecimalRange instance to copy
43 */
44 public BigDecimalRange(BigDecimalRange BigDecimalRange) {
45 this(BigDecimalRange.getMin(), BigDecimalRange.getMax());
46 }
47
48 @Override
49 public Range<BigDecimal> copy() {
50 return new BigDecimalRange(this);
51 }
52
53 /**
54 * @return the minimum value
55 */
56 public double minimum() {
57 return _min.doubleValue();
58 }
59
60 /**
61 * @return the maximum value
62 */
63 public double maximum() {
64 return _max.doubleValue();
65 }
66
67 /**
68 * @return the minimum value
69 */
70 public BigDecimal getMin() {
71 return _min;
72 }
73
74 /**
75 * Sets the minimum value
76 *
77 * @param min the new minimum value.
78 */
79 public void setMin(BigDecimal min) {
80 BigDecimal old = _min;
81 if (old != null && old.equals(min)) {
82 return;
83 }
84 assert min.compareTo(_max) <= 0;
85 _min = min;
86 firePropertyChange(PROPERTY_MIN, old, min);
87 }
88
89 /**
90 * @return the maximum value
91 */
92 public BigDecimal getMax() {
93 return _max;
94 }
95
96 /**
97 * Sets the maximum value
98 *
99 * @param max the new maximum value.
100 */
101 public void setMax(BigDecimal max) {
102 BigDecimal old = _max;
103 if (old != null && old.equals(max)) {
104 return;
105 }
106 assert max.compareTo(_min) >= 0;
107 _max = max;
108 firePropertyChange(PROPERTY_MAX, old, max);
109 }
110
111 /**
112 * @return the size of the range
113 */
114 public double size() {
115 return _max.subtract(_min).doubleValue();
116 }
117
118 /**
119 * @return the minimum() value for the range
120 */
121 public BigDecimal lower() {
122 return getMin();
123 }
124
125 @Override
126 public void adjust(BigDecimal lower, BigDecimal upper) {
127 double size = size(); // save it
128 if (lower != null) {
129 setMin(lower);
130 }
131 else if (upper != null) {
132 setMin(upper.add(new BigDecimal(size)));
133 }
134 if (upper != null) {
135 setMax(upper);
136 }
137 else if (lower != null) {
138 setMin(lower.add(new BigDecimal(size)));
139 }
140 }
141
142 /**
143 * @return the maximum() value for the range
144 */
145 public BigDecimal upper() {
146 return getMax();
147 }
148
149 @Override
150 public boolean contains(BigDecimal x) {
151 return x != null && x.compareTo(_min) >= 0 && x.compareTo(_max) <= 0;
152 }
153
154 /**
155 * Creates a new BigDecimalRange by enlarging this numeric range about its mid-point. For example to make it 10%
156 * bigger, use a stretch factor of 1.1. Note that this method can also be used to shrink a BigDecimalRange.
157 *
158 * @param stretchFactor the multiplication factor for the enlargement
159 * @return a new BigDecimalRange
160 */
161 public BigDecimalRange stretch(double stretchFactor) {
162 return stretch(stretchFactor, stretchFactor);
163 }
164
165 /**
166 * Creates a new BigDecimalRange by enlarging this numeric range about its mid-point. For example to make it 10%
167 * bigger, use a stretch factor of 1.1. Note that this method can also be used to shrink a BigDecimalRange.
168 *
169 * @param stretchFactorForLower the multiplication factor for the enlargement for the lower range
170 * @param stretchFactorForUpper the multiplication factor for the enlargement for the upper range
171 * @return a new BigDecimalRange
172 */
173 public BigDecimalRange stretch(double stretchFactorForLower, double stretchFactorForUpper) {
174 BigDecimal mid = _max.add(_min).divide(TWO);
175 BigDecimal halfSize = _max.subtract(_min).divide(TWO);
176 return new BigDecimalRange(mid.subtract(halfSize.multiply(new BigDecimal(stretchFactorForLower))), mid.add(halfSize.multiply(new BigDecimal(stretchFactorForUpper))));
177 }
178
179
180 @Override
181 public Range<BigDecimal> createIntermediate(Range<BigDecimal> target, double position) {
182 throw new UnsupportedOperationException("createIntermediate method is not currently support in BigDecimalRange");
183 }
184
185 /**
186 * Test for equality based on the values of min and max
187 */
188 @Override
189 public boolean equals(Object other) {
190 if (other instanceof BigDecimalRange) {
191 BigDecimalRange otherRange = (BigDecimalRange) other;
192 return _min.equals(otherRange._min) && _max.equals(otherRange._max);
193 }
194 else {
195 return false;
196 }
197 }
198
199 @Override
200 public int hashCode() {
201 return _max.multiply(new BigDecimal(29)).add(_min).intValue();
202 }
203
204 @Override
205 public String toString() {
206 return String.format("#<BigDecimalRange min=%f max=%f>", _min, _max);
207 }
208 }