1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
package ca.odell.glazedlists.impl.adt;
import java.util.*;
/**
* A Collection that stores keys in a map, with positions as the values.
*
* @author jplemieux
* @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
*/
public final class KeyedCollection<P, V> {
private final Map<V, Object> values;
private final Comparator<P> positionComparator;
private P first, last;
public KeyedCollection(Comparator<P> positionComparator, Map<V, Object> mapWithValuesAsKeys) {
if (!mapWithValuesAsKeys.isEmpty())
throw new IllegalArgumentException("mapWithValuesAsKeys must be empty");
this.positionComparator = positionComparator;
this.values = mapWithValuesAsKeys;
}
/**
* Inserts the specified value at the specified position.
*/
public void insert(P position, V value) {
Object previousPositions = values.get(value);
if (previousPositions == null) {
values.put(value, position);
} else if (previousPositions instanceof SortedSet) {
SortedSet<P> allPositions = (SortedSet)previousPositions;
allPositions.add(position);
} else {
SortedSet<P> allPositions = new TreeSet<P>(positionComparator);
allPositions.add((P)previousPositions);
allPositions.add(position);
values.put(value, allPositions);
}
if (first == null || lessThan(position, first)) {
first = position;
}
if (last == null || greaterThan(position, last)) {
last = position;
}
}
/**
* Returns the first position of the specified value.
*
* @param min the returned value will be greater than or equal to this.
* @param max the returned position will be less than this.
* @return the position, or <code>null</code> if no such value exists in
* the requested range.
*/
public P find(P min, P max, V value) {
if (positionComparator.compare(min, max) > 0)
throw new IllegalArgumentException("min " + min + " > max " + max);
Object positionsAsSingleOrSet = values.get(value);
if (positionsAsSingleOrSet == null) {
return null;
} else if (positionsAsSingleOrSet instanceof SortedSet) {
SortedSet<P> positions = (SortedSet<P>)positionsAsSingleOrSet;
SortedSet<P> positionsInRange = positions.subSet(min, max);
return positionsInRange.isEmpty() ? null : positionsInRange.iterator().next();
} else {
P position = (P)positionsAsSingleOrSet;
if (!lessThan(position, min) && lessThan(position, max)) {
return position;
}
}
return null;
}
/**
* Returns the largest position that has been inserted into this
* collection.
*/
public P last() { return last; }
/**
* Returns the smallest position that has been inserted into this
* collection.
*/
public P first() { return first; }
private boolean lessThan(P a, P b) {
return positionComparator.compare(a, b) < 0;
}
private boolean greaterThan(P a, P b) {
return positionComparator.compare(a, b) > 0;
}
}
|