| 1 |
/* |
| 2 |
* @(#)AggregatedRange.java 6/9/2011 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2011 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.range; |
| 8 |
|
| 9 |
import com.jidesoft.swing.JideSwingUtilities; |
| 10 |
|
| 11 |
import java.beans.PropertyChangeListener; |
| 12 |
import java.util.ArrayList; |
| 13 |
import java.util.Collection; |
| 14 |
import java.util.Collections; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
/** |
| 18 |
* A range class formed from a collection of Positionable instances. The class can be used to derive the minimum and |
| 19 |
* maximum values for the collection of Positionables, as well as providing other useful information such as the sum of |
| 20 |
* all the positive values and the sum of all the negative values. (These are used in the preparation of a stacked bar |
| 21 |
* chart.) |
| 22 |
*/ |
| 23 |
public class AggregatedRange implements Range<Double> { |
| 24 |
private Double positiveSum; |
| 25 |
private int positiveCount; |
| 26 |
private Double negativeSum; |
| 27 |
private int negativeCount; |
| 28 |
private List<Positionable> positions; |
| 29 |
|
| 30 |
/** |
| 31 |
* Create an empty range |
| 32 |
*/ |
| 33 |
public AggregatedRange() { |
| 34 |
this(null); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Create a range from the supplied Positionable instances |
| 39 |
* |
| 40 |
* @param positions the instances of the Positionable interface |
| 41 |
*/ |
| 42 |
public AggregatedRange(Collection<Positionable> positions) { |
| 43 |
this.positions = positions == null ? new ArrayList<Positionable>() : new ArrayList<Positionable>(positions); |
| 44 |
Collections.sort(this.positions); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* The lower value of the range; for this class it is the same as minimum() |
| 49 |
* |
| 50 |
* @return the lower value of the range |
| 51 |
*/ |
| 52 |
public Double lower() { |
| 53 |
return minimum(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* The upper value of the range; for this class it is the same as maximum() |
| 58 |
* |
| 59 |
* @return the upper value of the range |
| 60 |
*/ |
| 61 |
public Double upper() { |
| 62 |
return maximum(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* The number of points being combined in this range |
| 67 |
* |
| 68 |
* @return the number of points contributing to the range |
| 69 |
*/ |
| 70 |
public int getCount() { |
| 71 |
return positions.size(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Computes the sum of all the positive Positionables |
| 76 |
* |
| 77 |
* @return the sum of all the positive Positionables |
| 78 |
*/ |
| 79 |
public double getPositiveSum() { |
| 80 |
if (positiveSum == null) { |
| 81 |
updatePositives(); |
| 82 |
} |
| 83 |
return positiveSum; |
| 84 |
} |
| 85 |
|
| 86 |
public int getPositiveCount() { |
| 87 |
if (positiveSum == null) { |
| 88 |
updatePositives(); |
| 89 |
} |
| 90 |
return positiveCount; |
| 91 |
} |
| 92 |
|
| 93 |
private void updatePositives() { |
| 94 |
// Use a double to avoid autoboxing |
| 95 |
double sum = 0.0; |
| 96 |
positiveCount = 0; |
| 97 |
if (positions != null) { |
| 98 |
for (Positionable pos : positions) { |
| 99 |
if (pos.position() >= 0) { |
| 100 |
sum += pos.position(); |
| 101 |
positiveCount++; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
positiveSum = sum; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Computes the sum of all the negative Positionables |
| 110 |
* |
| 111 |
* @return the sum of all the negative Positionables |
| 112 |
*/ |
| 113 |
public double getNegativeSum() { |
| 114 |
if (negativeSum == null) { |
| 115 |
updateNegatives(); |
| 116 |
} |
| 117 |
return negativeSum; |
| 118 |
} |
| 119 |
|
| 120 |
public int getNegativeCount() { |
| 121 |
if (negativeSum == null) { |
| 122 |
updateNegatives(); |
| 123 |
} |
| 124 |
return negativeCount; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
private void updateNegatives() { |
| 129 |
// Use a double to avoid autoboxing |
| 130 |
double sum = 0.0; |
| 131 |
negativeCount = 0; |
| 132 |
if (positions != null) { |
| 133 |
for (Positionable pos : positions) { |
| 134 |
if (pos.position() < 0) { |
| 135 |
sum += pos.position(); |
| 136 |
negativeCount++; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
negativeSum = sum; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns the minimum (numeric) value in the range |
| 145 |
* |
| 146 |
* @return the minimum value in the range |
| 147 |
*/ |
| 148 |
public double minimum() { |
| 149 |
if (positions == null || positions.size() == 0) { |
| 150 |
return Double.NEGATIVE_INFINITY; |
| 151 |
} |
| 152 |
Positionable first = positions.get(0); |
| 153 |
return first.position(); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns the maximum (numeric) value in the range |
| 158 |
* |
| 159 |
* @return the maximum value in the range |
| 160 |
*/ |
| 161 |
public double maximum() { |
| 162 |
if (positions == null || positions.size() == 0) { |
| 163 |
return Double.POSITIVE_INFINITY; |
| 164 |
} |
| 165 |
Positionable last = positions.get(positions.size() - 1); |
| 166 |
return last.position(); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* The size of the range, computed as the difference between the maximum and the minimum |
| 171 |
* |
| 172 |
* @return the size of the range (i.e., max - min) |
| 173 |
*/ |
| 174 |
public double size() { |
| 175 |
return maximum() - minimum(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns a boolean to indicate whether the supplied Double lies within this range |
| 180 |
*/ |
| 181 |
public boolean contains(Double item) { |
| 182 |
return item != null && item >= minimum() && item <= maximum(); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Not supported in this class |
| 187 |
*/ |
| 188 |
public void adjust(Double lower, Double upper) { |
| 189 |
throw new UnsupportedOperationException("An aggregated range is immutable"); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Not supported in this class |
| 194 |
*/ |
| 195 |
public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) { |
| 196 |
throw new UnsupportedOperationException("An aggregated range is immutable"); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Not supported in this class |
| 201 |
*/ |
| 202 |
public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) { |
| 203 |
throw new UnsupportedOperationException("An aggregated range is immutable"); |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public PropertyChangeListener[] getPropertyChangeListeners() { |
| 208 |
return new PropertyChangeListener[0]; |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) { |
| 213 |
return new PropertyChangeListener[0]; |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public boolean equals(Object o) { |
| 218 |
if (this == o) return true; |
| 219 |
if (!(o instanceof AggregatedRange)) return false; |
| 220 |
|
| 221 |
AggregatedRange that = (AggregatedRange) o; |
| 222 |
|
| 223 |
if (negativeCount != that.negativeCount) return false; |
| 224 |
if (positiveCount != that.positiveCount) return false; |
| 225 |
if (negativeSum != null ? !negativeSum.equals(that.negativeSum) : that.negativeSum != null) return false; |
| 226 |
if (!JideSwingUtilities.equals(positions, that.positions, true)) return false; |
| 227 |
if (positiveSum != null ? !positiveSum.equals(that.positiveSum) : that.positiveSum != null) return false; |
| 228 |
|
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public int hashCode() { |
| 234 |
int result = positiveSum != null ? positiveSum.hashCode() : 0; |
| 235 |
result = 31 * result + positiveCount; |
| 236 |
result = 31 * result + (negativeSum != null ? negativeSum.hashCode() : 0); |
| 237 |
result = 31 * result + negativeCount; |
| 238 |
result = 31 * result + (positions != null ? positions.hashCode() : 0); |
| 239 |
return result; |
| 240 |
} |
| 241 |
} |