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