ma.glasnost.orika.util
Class SortedCollection<V>

java.lang.Object
  extended by ma.glasnost.orika.util.SortedCollection<V>
Type Parameters:
V - the element type contained in this list
All Implemented Interfaces:
Iterable<V>, Collection<V>
Direct Known Subclasses:
SortedSet

public class SortedCollection<V>
extends Object
implements Collection<V>

A simple sorted collection implementation that allows for duplicates; new items are inserted based on their comparison to existing items; if a new item is found to be less than any item in the list, it is inserted before that item, else it is inserted at the end.

The collection is backed by a ConcurrentSkipListMap, with the keys defined as Double, allowing for inserts of items at a specific location with respect to the other items in the map.

This class is thread-safe, with the same caveats for ConcurrentSkipListMap.

Author:
matt.deboer@gmail.com

Field Summary
protected  Comparator<V> comparator
           
protected  ConcurrentSkipListMap<Double,V> sortedItems
           
 
Constructor Summary
SortedCollection()
           
SortedCollection(Collection<? extends V> c)
           
SortedCollection(Collection<? extends V> c, Comparator<V> comparator)
           
SortedCollection(Comparator<V> comparator)
           
 
Method Summary
 boolean add(V value)
           
 boolean addAll(Collection<? extends V> c)
           
 void clear()
           
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> c)
           
 V first()
           
protected  boolean insertBetween(Double min, Double max, V value, boolean allowDuplicates)
          Inserts the provided value with a key between the provided min and max values.
 boolean isEmpty()
           
 Iterator<V> iterator()
           
 V last()
           
 boolean remove(Object o)
           
 boolean removeAll(Collection<?> c)
           
 boolean retainAll(Collection<?> c)
           
 int size()
           
 SortedCollection<V> threadsafeCopy()
           
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
protected static
<V> Comparable<V>
toComparable(V item)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

comparator

protected final Comparator<V> comparator

sortedItems

protected final ConcurrentSkipListMap<Double,V> sortedItems
Constructor Detail

SortedCollection

public SortedCollection()

SortedCollection

public SortedCollection(Comparator<V> comparator)
Parameters:
comparator -

SortedCollection

public SortedCollection(Collection<? extends V> c)
Parameters:
c - the collection used to initialize this SortedCollection

SortedCollection

public SortedCollection(Collection<? extends V> c,
                        Comparator<V> comparator)
Parameters:
c - the collection from which to initialize this SortedCollection
comparator - the comparator used for sorting the elements
Method Detail

threadsafeCopy

public SortedCollection<V> threadsafeCopy()
Returns:
a copy of the current sorted collection which is safe for concurrent access

add

public boolean add(V value)
Specified by:
add in interface Collection<V>

insertBetween

protected boolean insertBetween(Double min,
                                Double max,
                                V value,
                                boolean allowDuplicates)
Inserts the provided value with a key between the provided min and max values. If an item is already found at that key, an additional comparison is performed, and the new value is inserted at half the distance before or after the existing key.

Returns true if the item is inserted.

Parameters:
min -
max -
value -
allowDuplicates -
Returns:
true if the item was inserted

size

public int size()
Specified by:
size in interface Collection<V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<V>

toComparable

protected static <V> Comparable<V> toComparable(V item)
Parameters:
item -
Returns:
the provided item, cast as a Comparable

iterator

public Iterator<V> iterator()
Specified by:
iterator in interface Iterable<V>
Specified by:
iterator in interface Collection<V>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<V>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<V>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface Collection<V>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<V>

containsAll

public boolean containsAll(Collection<?> c)
Specified by:
containsAll in interface Collection<V>

addAll

public boolean addAll(Collection<? extends V> c)
Specified by:
addAll in interface Collection<V>

removeAll

public boolean removeAll(Collection<?> c)
Specified by:
removeAll in interface Collection<V>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<V>

clear

public void clear()
Specified by:
clear in interface Collection<V>

first

public V first()
Returns:
the first item in this collection

last

public V last()
Returns:
the last item in this collection


Copyright © 2013 Glasnost. All Rights Reserved.