Debian Codegraph @

src/com/jidesoft/range/LongRange.java

raw
1 /*
2 * @(#)LongRange.java 5/27/2014
3 *
4 * Copyright 2002 - 2014 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.range;
8
9 /**
10 * Specifies upper and lower bounds for a range of values
11 *
12 * @author Simon
13 */
14 public class LongRange extends AbstractNumericRange<Long> {
15 protected long _min;
16 protected long _max;
17
18 /**
19 * Creates a numeric range with minimum 0.0 and maximum of 1.0
20 */
21 public LongRange() {
22 this(0, 1);
23 }
24
25 /**
26 * Create a numeric range by supplying minimum and maximum values
27 *
28 * @param min the minimum
29 * @param max the maximum
30 */
31 public LongRange(long min, long max) {
32 super();
33 _min = Math.min(min, max);
34 _max = Math.max(min, max);
35 }
36
37 /**
38 * Constructs a copy of the supplied IntegerRange object
39 *
40 * @param integerRange the long range object to copy
41 */
42 public LongRange(LongRange integerRange) {
43 this((long) integerRange.minimum(), (long) integerRange.maximum());
44 }
45
46 @Override
47 public Range<Long> copy() {
48 return new LongRange(this);
49 }
50
51 /**
52 * @return the minimum value
53 */
54 public double minimum() {
55 return _min;
56 }
57
58 /**
59 * @return the maximum value
60 */
61 public double maximum() {
62 return _max;
63 }
64
65 /**
66 * @return the minimum value
67 */
68 public long getMin() {
69 return _min;
70 }
71
72 /**
73 * Sets the minimum value
74 *
75 * @param min the new minimum value.
76 */
77 public void setMin(long min) {
78 long old = _min;
79 if (old == min) {
80 return;
81 }
82 assert min <= _max : "minimum " + min + " not <= " + _max;
83 _min = min;
84 firePropertyChange(PROPERTY_MIN, old, min);
85 }
86
87 /**
88 * @return the maximum value
89 */
90 public long getMax() {
91 return _max;
92 }
93
94 /**
95 * Sets the maximum value
96 *
97 * @param max the new maximum value.
98 */
99 public void setMax(long max) {
100 long old = _max;
101 if (old == max) {
102 return;
103 }
104 assert max >= _min : "maximum " + max + " not >= " + _min;
105 _max = max;
106 firePropertyChange(PROPERTY_MAX, old, max);
107 }
108
109 public void adjust(Long lower, Long upper) {
110 double size = size(); // save it
111 if (lower != null) {
112 setMin(lower);
113 }
114 else if (upper != null) {
115 setMin(upper - (long) size);
116 }
117 if (upper != null) {
118 setMax(upper);
119 }
120 else if (lower != null) {
121 setMin(lower + (long) size);
122 }
123 }
124
125 /**
126 * @return the size of the range
127 */
128 public double size() {
129 return _max - _min;
130 }
131
132 /**
133 * @return the minimum() value for the range
134 */
135 public Long lower() {
136 return (long) minimum();
137 }
138
139 /**
140 * @return the maximum() value for the range
141 */
142 public Long upper() {
143 return (long) maximum();
144 }
145
146 /**
147 * Determines whether the range contains the supplied value
148 */
149 public boolean contains(Long x) {
150 return x >= _min && x <= _max;
151 }
152
153 @Override
154 public Range<Long> createIntermediate(Range<Long> targetRange, double position) {
155 double sourceMin = this.minimum();
156 double sourceMax = this.maximum();
157 double targetMin = targetRange.minimum();
158 double targetMax = targetRange.maximum();
159 double min = sourceMin + position * (targetMin - sourceMin);
160 double max = sourceMax + position * (targetMax - sourceMax);
161 return new LongRange((int) Math.round(min), (int) Math.round(max));
162 }
163
164 /**
165 * Test for equality based on the values of min and max
166 */
167 @Override
168 public boolean equals(Object other) {
169 if (other instanceof LongRange) {
170 LongRange otherRange = (LongRange) other;
171 return _min == otherRange._min && _max == otherRange._max;
172 }
173 else {
174 return false;
175 }
176 }
177
178 @Override
179 public int hashCode() {
180 return (int) (_max * 29 + _min);
181 }
182
183 @Override
184 public String toString() {
185 return String.format("#<LongRange min=%d max=%d>", _min, _max);
186 }
187 }