Debian Codegraph @

src/com/jidesoft/range/CategoryRange.java

raw
1 /*
2 * @(#)CategoryRange.java
3 *
4 * 2002 - 2015 JIDE Software Incorporated. All rights reserved.
5 * Copyright (c) 2005 - 2015 Catalysoft Limited. All rights reserved.
6 */
7
8 package com.jidesoft.range;
9
10 import java.util.*;
11
12
13 /**
14 * Note that this class is iterable so you can use it in an advanced for.. loop
15 *
16 * @author Simon White ([email protected])
17 */
18 public class CategoryRange<T> extends AbstractRange<T> implements Iterable<Category<T>> {
19 public static final String PROPERTY_VALUES = "values";
20 public static final String PROPERTY_COMPARATOR = "comparator";
21 public static final String PROPERTY_SORTED = "sorted";
22 private List<T> _possibleValues = null;
23 private List<Category<T>> _categoryValues = null;
24 private Double minimum;
25 private Double maximum;
26 private Comparator<T> comparator = null;
27 private boolean sorted = false;
28 // Private member variable to flag whether the possible values have been sorted or are in need of a sort
29 private boolean alreadySorted = false;
30 private Map<T, Integer> positionIndices = new HashMap<T, Integer>();
31
32 public CategoryRange() {
33 _possibleValues = new ArrayList<T>();
34 _categoryValues = new ArrayList<Category<T>>();
35 alreadySorted = false;
36 }
37
38 /**
39 * Create a CategoryRange from the supplied values
40 *
41 * @param values the values.
42 */
43 public CategoryRange(T... values) {
44 _possibleValues = new ArrayList<T>();
45 _possibleValues.addAll(Arrays.asList(values));
46 alreadySorted = false;
47 }
48
49 /**
50 * Create a CategoryRange from a set of values. Note that internally, a list is created out of the set so that the
51 * class can reliably determine an <code>upper()</code> and a <code>lower()</code> value.
52 *
53 * @param values - the set of possible values
54 */
55 public CategoryRange(Set<T> values) {
56 _possibleValues = new ArrayList<T>(values);
57 alreadySorted = false;
58 }
59
60 /**
61 * Create a new CategoryRange by copying an existing one. This would allow you subsequently to tweak the values in
62 * the copy without affecting the original.
63 *
64 * @param categoryRange the category range instance to copy
65 */
66 public CategoryRange(CategoryRange<T> categoryRange) {
67 _categoryValues = new ArrayList<Category<T>>(categoryRange.getCategoryValues());
68 _possibleValues = new ArrayList<T>(categoryRange.getPossibleValues());
69 comparator = categoryRange.getComparator();
70 setMinimum(categoryRange.minimum());
71 setMaximum(categoryRange.maximum());
72 alreadySorted = false;
73 }
74
75 // When you call getCategoryValues() it will call this method and retrieve a list of possible values
76 // which is sorted if necessary
77 public List<T> getPossibleValues() {
78 if (sorted && !alreadySorted) {
79 if (comparator == null) {
80 Comparator<T> defaultComparator = new Comparator<T>() {
81 public int compare(T o1, T o2) {
82 if (o1 == null && o2 == null) {
83 return 0;
84 } else if (o1 == null) {
85 return -1;
86 } else if (o2 == null) {
87 return 1;
88 } else {
89 // Use natural sort order if available
90 if (o1 instanceof Comparable) {
91 Comparable t1 = (Comparable) o1;
92 return t1.compareTo(o2);
93 } else {
94 // otherwise use the toString method to derive a string comparator
95 String s1 = o1.toString();
96 String s2 = o2.toString();
97 return s1.compareTo(s2);
98 }
99 }
100 }
101 };
102 Collections.sort(_possibleValues, defaultComparator);
103 positionIndices.clear();
104 } else {
105 Collections.sort(_possibleValues, comparator);
106 positionIndices.clear();
107 }
108 }
109 return _possibleValues;
110 }
111
112 /**
113 * Returns the category with the supplied position value. (Note that the first position is 1, not 0.)
114 *
115 * @param position the position of a category along an axis
116 * @return the category with the supplied position value.
117 */
118 public Category<T> getCategory(int position) {
119 if (position < 1) {
120 throw new IllegalArgumentException("Supplied category index was " + position + " but it should be >= 1");
121 }
122 return getCategoryValues().get(position - 1);
123 }
124
125 /**
126 * <p>Adds a category to the range. Note that after adding categories, you will need to call reset() if you want the
127 * minimum and maximum numeric values of the range to be recomputed.</p> <p>This method fires a property change
128 * event, but to avoid cloning a list for efficiency, the old value is always null</p>
129 *
130 * @param c the category to add
131 * @return this range
132 */
133 public CategoryRange<T> add(Category<T> c) {
134 if (!contains(c)) {
135 if (comparator == null) {
136 _possibleValues.add(c.getValue());
137 _categoryValues.add(c);
138 } else {
139 _possibleValues.add(c.getValue());
140 alreadySorted = false;
141 // Force the category values to be recomputed
142 _categoryValues = null;
143 }
144 c.setRange(this);
145 firePropertyChange(PROPERTY_VALUES, null, _possibleValues);
146 }
147 return this;
148 }
149
150 /**
151 * Specify whether the categories of the range should be sorted.
152 * If you call this method with <code>true</code> but do not explicitly
153 * set a comparator for the sort ordering, then the natural ordering of the
154 * objects (using java.util.Comparable) will be used. If the objects do not
155 * implement Comparable, then a string comparator is constructed based on the
156 * toString() method of the object.
157 * @param sorted whether the categories of the range should be sorted
158 */
159 public void setSorted(boolean sorted) {
160 boolean oldValue = this.sorted;
161 this.sorted = sorted;
162 // Force the category values to be recomputed
163 if (sorted) {
164 _categoryValues = null;
165 }
166 positionIndices.clear();
167 firePropertyChange(PROPERTY_SORTED, oldValue, sorted);
168 }
169
170 /**
171 * Returns a value to indicate whether the categories of the range are sorted
172 * @return a value to indicate whether the categories of the range are sorted
173 */
174 public boolean isSorted() {
175 return this.sorted;
176 }
177
178 /**
179 * Returns the comparator that, if set, will be used to sort the values in the range
180 * @return the comparator that, if set, will be used to sort the values in the range
181 */
182 public Comparator<T> getComparator() {
183 return comparator;
184 }
185
186 /**
187 * Specify the comparator that will be used to sort the values in the range.
188 * Calling this method implicitly calls setSorted(): the sorted property will be set
189 * to true if the comparator is non-null and will be set to false if the comparator is null
190 * @param comparator the comparator to be used to sort the values in the range
191 */
192 public void setComparator(Comparator<T> comparator) {
193 Comparator<T> oldValue = this.comparator;
194 this.comparator = comparator;
195 // This call will also force the category values to be recomputed
196 setSorted(comparator != null);
197 firePropertyChange(PROPERTY_COMPARATOR, oldValue, comparator);
198 }
199
200 @Override
201 public Range<T> copy() {
202 return new CategoryRange<T>(this);
203 }
204
205 // TODO: This assumes the possible values are sorted
206 public T lower() {
207 if (_possibleValues == null || _possibleValues.size() == 0) {
208 return null;
209 }
210 return _possibleValues.get(0);
211 }
212
213 // TODO: This assumes the possible values are sorted
214 public T upper() {
215 if (_possibleValues == null || _possibleValues.size() == 0) {
216 return null;
217 }
218 int numElements = _possibleValues.size();
219 return _possibleValues.get(numElements - 1); // get the last element
220 }
221
222 /**
223 * Not supported for Category Ranges
224 */
225 public void adjust(T lower, T upper) {
226 throw new UnsupportedOperationException();
227 }
228
229 /**
230 * @return the maximum value for the axis in the range
231 *
232 * @see com.jidesoft.range.Range#maximum()
233 */
234 public double maximum() {
235 if (maximum == null) {
236 T upper = upper();
237 if (upper == null) {
238 return 1.0;
239 }
240 else {
241 maximum = position(upper) + 1.0;
242 }
243 }
244 return maximum;
245 }
246
247 /**
248 * @return the minimum value for the axis in the range
249 *
250 * @see com.jidesoft.range.Range#minimum()
251 */
252 public double minimum() {
253 if (minimum == null) {
254 T lower = lower();
255 if (lower == null) {
256 return 0.0;
257 }
258 else {
259 minimum = position(lower) - 1.0;
260 }
261 }
262 return minimum;
263 }
264
265 /**
266 * Reset the maximum and minimum. They will be recomputed on the next call to minimum() or maximum() respectively
267 */
268 public void reset() {
269 maximum = null;
270 minimum = null;
271 positionIndices.clear();
272 }
273
274 public void setMinimum(double value) {
275 Double oldValue = this.minimum;
276 this.minimum = value;
277 firePropertyChange(PROPERTY_MIN, oldValue, value);
278 }
279
280 public void setMaximum(double value) {
281 Double oldValue = this.maximum;
282 this.maximum = value;
283 firePropertyChange(PROPERTY_MAX, oldValue, value);
284 }
285
286 /**
287 * Returns the size of the range, as given by the maximum minus the minimum. To compute the size of the range in
288 * terms of the number of members in the category, use getPossibleValue().size()
289 *
290 * @return the size of the range
291 *
292 * @see com.jidesoft.range.Range#size()
293 */
294 public double size() {
295 if (_possibleValues == null) {
296 return 0;
297 }
298
299 int numElements = _possibleValues.size();
300 if (numElements == 0) {
301 return 0;
302 }
303 // The previous definition of size() prevented us from being able to zoom
304 // in on a categorical bar chart
305 return maximum() - minimum();
306 }
307
308
309 public int position(T value) {
310 Integer index = positionIndices.get(value);
311 if (index == null) {
312 List<T> possibleValues = getPossibleValues();
313 index = possibleValues.indexOf(value);
314 if (index < 0) {
315 throw new IllegalArgumentException("Value " + value + " not known");
316 }
317 positionIndices.put(value, index);
318 }
319 return 1 + index;
320 }
321
322 /**
323 * Determines whether the category range contains the supplied possible value
324 */
325 public boolean contains(T x) {
326 if (x == null) {
327 return false;
328 }
329 else {
330 for (T category : _possibleValues) {
331 if (x.equals(category)) {
332 return true;
333 }
334 }
335 return false;
336 }
337 }
338
339 /**
340 * Determines whether the category range contains the specified category value
341 *
342 * @param value the category value.
343 * @return true if the range contains the specified value. Otherwise false.
344 */
345 public boolean contains(Category<T> value) {
346 if (value == null) {
347 return false;
348 }
349 else {
350 for (Category<T> category : getCategoryValues()) {
351 if (value.equals(category)) {
352 return true;
353 }
354 }
355 return false;
356 }
357 }
358
359 /**
360 * Returns an iterator for the category values
361 * @return an iterator for the category values
362 */
363 public Iterator<Category<T>> iterator() {
364 return getCategoryValues().iterator();
365 }
366
367 /**
368 * Returns a list of the category values in this range
369 * @return a list of category values
370 */
371 public List<Category<T>> getCategoryValues() {
372 if (_categoryValues == null) {
373 _categoryValues = new ArrayList<Category<T>>();
374 for (T value : getPossibleValues()) {
375 _categoryValues.add(new Category<T>(value, this));
376 }
377 }
378 return _categoryValues;
379 }
380
381 @Override
382 public int hashCode() {
383 final int prime = 31;
384 int result = 1;
385 result = prime * result + ((_categoryValues == null) ? 0 : _categoryValues.hashCode());
386 result = prime * result + ((_possibleValues == null) ? 0 : _possibleValues.hashCode());
387 result = prime * result + ((maximum == null) ? 0 : maximum.hashCode());
388 result = prime * result + ((minimum == null) ? 0 : minimum.hashCode());
389 return result;
390 }
391
392 @Override
393 public boolean equals(Object obj) {
394 if (this == obj)
395 return true;
396 if (obj == null)
397 return false;
398 if (getClass() != obj.getClass())
399 return false;
400 CategoryRange other = (CategoryRange) obj;
401 if (_categoryValues == null) {
402 if (other._categoryValues != null)
403 return false;
404 }
405 else if (!_categoryValues.equals(other._categoryValues))
406 return false;
407 if (_possibleValues == null) {
408 if (other._possibleValues != null)
409 return false;
410 }
411 else if (!_possibleValues.equals(other._possibleValues))
412 return false;
413 if (maximum == null) {
414 if (other.maximum != null)
415 return false;
416 }
417 else if (!maximum.equals(other.maximum))
418 return false;
419 if (minimum == null) {
420 if (other.minimum != null)
421 return false;
422 }
423 else if (!minimum.equals(other.minimum))
424 return false;
425 return true;
426 }
427
428 /**
429 * Creates an intermediate range between this range and a target range. Used for range morphing.
430 * @param target the target range of the morph
431 * @param position a value between 0 and 1 indicating the position of the morph
432 * @return a CategoryRange
433 */
434 public Range<T> createIntermediate(Range<T> target, double position) {
435 double sourceMin = this.minimum();
436 double sourceMax = this.maximum();
437 double targetMin = target.minimum();
438 double targetMax = target.maximum();
439 double min = sourceMin + position * (targetMin - sourceMin);
440 double max= sourceMax + position * (targetMax - sourceMax);
441 CategoryRange r;
442 if (position < 0.5) {
443 r = new CategoryRange(this);
444 } else {
445 if (target instanceof CategoryRange) {
446 r = new CategoryRange((CategoryRange) target);
447 } else {
448 throw new IllegalArgumentException("Cannot create intermediate range from "+target.getClass());
449 }
450 }
451 r.setMinimum(min);
452 r.setMaximum(max);
453 return r;
454 }
455
456 public String toString() {
457 StringBuilder builder = new StringBuilder("#<CategoryRange ");
458 builder.append("minimum=");
459 builder.append(minimum);
460 builder.append(" maximum=");
461 builder.append(maximum);
462 builder.append(">");
463 return builder.toString();
464 }
465 }