| 1 |
/* |
| 2 |
* @(#)CombinedNumericRange.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 |
import java.util.ArrayList; |
| 11 |
import java.util.List; |
| 12 |
|
| 13 |
/** |
| 14 |
* A little convenience class to compute the maximum and minimum values of multiple ranges. |
| 15 |
* |
| 16 |
* @author [email protected] |
| 17 |
*/ |
| 18 |
public class CombinedNumericRange extends AbstractNumericRange<Double> { |
| 19 |
private final Object monitor = new Object(); |
| 20 |
private List<Range<Double>> _ranges = new ArrayList<Range<Double>>(); |
| 21 |
private Double _max = null; |
| 22 |
private Double _min = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Using this constructor relies on the user subsequently calling add() to add a range |
| 26 |
*/ |
| 27 |
public CombinedNumericRange() { |
| 28 |
super(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Add a new range to this combined range. Notice the method returns this instance, so method calls can be chained |
| 33 |
* together. If you pass null to this method the CombinedNumericRange remains unchanged; an Exception is NOT |
| 34 |
* thrown. |
| 35 |
* |
| 36 |
* @param range the new range to add |
| 37 |
* @return this instance |
| 38 |
*/ |
| 39 |
public CombinedNumericRange add(Range<Double> range) { |
| 40 |
if (range == null) { |
| 41 |
return this; |
| 42 |
} |
| 43 |
synchronized (monitor) { |
| 44 |
_ranges.add(range); |
| 45 |
_min = null; |
| 46 |
_max = null; |
| 47 |
} |
| 48 |
return this; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* The lower value in the range; here, the same as <code>minimum</code> |
| 53 |
*/ |
| 54 |
public Double lower() { |
| 55 |
return minimum(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* The upper value in the range; here, the same as <code>maximum()</code> |
| 60 |
*/ |
| 61 |
public Double upper() { |
| 62 |
return maximum(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the number of sub-ranges contained by this range |
| 67 |
* |
| 68 |
* @return the number of sub-ranges in this range |
| 69 |
*/ |
| 70 |
public int rangeCount() { |
| 71 |
return _ranges == null ? 0 : _ranges.size(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Calls to this method throw an UnsupportedOprationException. The idea is that in the case of this class we don't |
| 76 |
* want to be able to mess with the lower and upper bounds as they are computed from the supplied range values. If |
| 77 |
* the class needed to recompute the lower and upper bounds any previous adjustment that had been made through this |
| 78 |
* method would have been lost. |
| 79 |
* |
| 80 |
* @throws UnsupportedOperationException |
| 81 |
*/ |
| 82 |
public void adjust(Double lower, Double upper) { |
| 83 |
throw new UnsupportedOperationException(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Lazily calculates the maximum value in the range |
| 88 |
*/ |
| 89 |
public double maximum() { |
| 90 |
synchronized (monitor) { |
| 91 |
if (_max != null) { |
| 92 |
return _max; |
| 93 |
} |
| 94 |
if (_ranges == null || _ranges.size() == 0) { |
| 95 |
return Double.MAX_VALUE; |
| 96 |
} |
| 97 |
_max = -Double.MAX_VALUE; |
| 98 |
for (Range<Double> range : _ranges) { |
| 99 |
if (range != null && range.maximum() > _max) { |
| 100 |
_max = range.maximum(); |
| 101 |
} |
| 102 |
} |
| 103 |
return _max; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Lazily calculates the minimum value in the range |
| 109 |
*/ |
| 110 |
public double minimum() { |
| 111 |
synchronized (monitor) { |
| 112 |
if (_min != null) { |
| 113 |
return _min; |
| 114 |
} |
| 115 |
if (_ranges == null || _ranges.size() == 0) { |
| 116 |
return -Double.MAX_VALUE; |
| 117 |
} |
| 118 |
_min = Double.MAX_VALUE; |
| 119 |
for (Range<Double> range : _ranges) { |
| 120 |
if (range != null && range.minimum() < _min) { |
| 121 |
_min = range.minimum(); |
| 122 |
} |
| 123 |
} |
| 124 |
return _min; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* This range contains some point iff one or more of its sub-ranges contain that point |
| 131 |
*/ |
| 132 |
public boolean contains(Double x) { |
| 133 |
synchronized (monitor) { |
| 134 |
if (x == null || _ranges.size() == 0) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
else { |
| 138 |
for (Range<Double> range : _ranges) { |
| 139 |
if (range.contains(x)) { |
| 140 |
return true; |
| 141 |
} |
| 142 |
} |
| 143 |
return false; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* The size of the range is computed as the maximum minus the minimum value. |
| 150 |
*/ |
| 151 |
public double size() { |
| 152 |
synchronized (monitor) { |
| 153 |
return maximum() - minimum(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Returns a new numeric range that is based on this range, but with a margin introduced at each end. The margin |
| 159 |
* proportion is a value between 0 and 1. For example to add a 20% margin to each end use parameters of 0.2 for both |
| 160 |
* the leading and trailing margin proportion. |
| 161 |
* |
| 162 |
* @param leadingMarginProportion how much margin to add at the low end of the range |
| 163 |
* @param trailingMarginProportion how much margin to add at the top end of the range |
| 164 |
* @return a new NumericRange object with margins added |
| 165 |
*/ |
| 166 |
public NumericRange getRange(double leadingMarginProportion, double trailingMarginProportion) { |
| 167 |
double maximum = maximum(); |
| 168 |
double minimum = minimum(); |
| 169 |
double difference = Math.abs(maximum - minimum); |
| 170 |
double leadingMargin = leadingMarginProportion * difference; |
| 171 |
double trailingMargin = trailingMarginProportion * difference; |
| 172 |
return new NumericRange(minimum - leadingMargin, maximum + trailingMargin); |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public Range<Double> createIntermediate(Range<Double> targetRange, double position) { |
| 177 |
double sourceMin = this.minimum(); |
| 178 |
double sourceMax = this.maximum(); |
| 179 |
double targetMin = targetRange.minimum(); |
| 180 |
double targetMax = targetRange.maximum(); |
| 181 |
double min = sourceMin + position * (targetMin - sourceMin); |
| 182 |
double max= sourceMax + position * (targetMax - sourceMax); |
| 183 |
return new NumericRange(min, max); |
| 184 |
} |
| 185 |
|
| 186 |
public String toString() { |
| 187 |
return String.format("#<CombinedNumericRange min=%s max=%s>", minimum(), maximum()); |
| 188 |
} |
| 189 |
|
| 190 |
} |