public class UpdateableTreeSet<E extends UpdateableTreeSet.Updateable> extends TreeSet<E>
As you might know you should never add or remove elements of Java collections while
iterating over them in a loop. There is one exception: If you use an Iterator
you may safely use its remove method. Many people
do not know that or are even unaware of that method's existence because they iterate
using for loops.
Another problem specifically with sorted collections is that even if they are
Comparable or use an explicit Comparator,
their elements will be sorted at insertion time only and the sort order not
updated even if their sorting keys change. If you want to achieve a refresh, you have to
Usage example:
import de.scrum_master.util.UpdateableTreeSet;
import de.scrum_master.util.UpdateableTreeSet.Updateable;
class MyType implements Updateable {
void update(Object newValue) {
// Change the receiver's value
}
}
SortedSet mySortedSet = new UpdateableTreeSet();
// Add elements to mySortedSet...
for (MyType element : mySortedSet) {
if (removeCondition)
markForRemoval(element);
if (updateCondition)
markForUpdate(element, newValue);
}
mySortedSet.updateMarked();
Special thanks go to user tucuxi at stackoverflow.com
because his answer in one of the discussion threads
inspired me to implement this class based on his idea. My version is just a bit more sophisticated because
it supports deferred updates and is thus useful within for loops.
| Modifier and Type | Class and Description |
|---|---|
static interface |
UpdateableTreeSet.Updateable
UpdateableTreeSet elements must implement this interface in order to provide a structured way
of updating themselves at the right moment during an update operation, i.e.
|
| Constructor and Description |
|---|
UpdateableTreeSet() |
UpdateableTreeSet(Collection<? extends E> c) |
UpdateableTreeSet(Comparator<? super E> comparator) |
UpdateableTreeSet(SortedSet<E> s) |
| Modifier and Type | Method and Description |
|---|---|
void |
markForRemoval(E element)
Mark an element for subsequent removal by
updateMarked(). |
void |
markForUpdate(E element)
Convenience method passing a null value to
markForUpdate(Updateable, Object) |
void |
markForUpdate(E element,
Object newValue)
Mark an element for subsequent update by
updateMarked(). |
boolean |
update(E element)
Convenience method passing a null value to
update(Updateable, Object) |
boolean |
update(E element,
Object newValue)
Performs an immediate update on a single element so as to trigger its re-ordering
within the collection.
|
void |
updateAll()
Updates the total sort order of the set by removing and re-adding all elements.
|
void |
updateMarked()
Performs
a bulk removal on all elements previously marked for removal,
a bulk update on all elements previously marked for update so as to trigger
their re-sorting within the collection.
|
add, addAll, ceiling, clear, clone, comparator, contains, descendingIterator, descendingSet, first, floor, headSet, headSet, higher, isEmpty, iterator, last, lower, pollFirst, pollLast, remove, size, subSet, subSet, tailSet, tailSetequals, hashCode, removeAllcontainsAll, retainAll, toArray, toArray, toStringpublic UpdateableTreeSet()
public UpdateableTreeSet(Collection<? extends E> c)
public UpdateableTreeSet(Comparator<? super E> comparator)
public void markForUpdate(E element, Object newValue)
updateMarked().
Attention: Beware of manually modifying a marked element before its scheduled update. It might not be found anymore (and thus not removed) because of its changed key, which later could lead to strange double entries in the collection.
public void markForUpdate(E element)
markForUpdate(Updateable, Object)public void markForRemoval(E element)
updateMarked().
Attention: Beware of manually modifying a marked element before its scheduled removal. It might not be found anymore (and thus not removed) because of its changed key, which later could lead to strange double entries in the collection.
public void updateMarked()
Please note that if any remove or update action fails, this will be silently ignored as long as there are no exceptions.
Attention: Do not call this method while looping over the collection and beware of manually modifying any marked elements before their scheduled update/removal. They might not be found anymore (and thus not removed) because of their changed keys, which later could lead to strange double entries in the collection.
public void updateAll()
You may use this method in situations when properties of elements within the set have been mofified,
which usually should be avoided. This method refreshes and in a way "repairs" the set, because if
you modify elements within the set, their fixed sort order might contradict their actual state,
which might disturb iterators and operations like TreeSet.contains(java.lang.Object) or TreeSet.remove(java.lang.Object).
Attention: All previously recorded update/removal marks will be reset before the update, so do not expect any elements to be removed by this operation.
public boolean update(E element, Object newValue)
element - the element to be updatednewValue - element's new value (if any)public boolean update(E element)
update(Updateable, Object)Copyright © 2013. All rights reserved.