T
- the type of objects stored in this collectionpublic class SortedCollection<T> extends SortedArrayData<T,T> implements java.util.Queue<T>, java.io.Serializable, java.lang.Cloneable
Modifier and Type | Field and Description |
---|---|
protected static int |
DEFAULT_INITIAL_CAPACITY
Default initial capacity of the internal array
|
protected static int |
UNLIMITED_CAPACITY
Default initial capacity of the internal array
|
Constructor and Description |
---|
SortedCollection()
Constructs an empty collection.
|
SortedCollection(java.util.Comparator<? super T> comparator)
Constructs an empty collection.
|
SortedCollection(int initialCapacity)
Constructs an empty collection with the specified initial capacity.
|
SortedCollection(int initialCapacity,
java.util.Comparator<? super T> comparator)
Constructs an empty collection with the specified initial capacity.
|
SortedCollection(int initialCapacity,
int maximalCapacity,
java.util.Comparator<? super T> comparator)
Constructs an empty collection with the specified initial and maximal capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T e)
Adds the specified element to this list.
|
protected boolean |
add(T e,
int index)
Adds the specified element to this list given an index on which the object should be stored.
|
boolean |
addAll(java.util.Collection<? extends T> c)
Add all of the elements in the specified collection to this list.
|
protected boolean |
addAllSortedArray(java.lang.Object[] array,
int size)
Add all of the elements in the specified array to this list assuming that the
items of the
array are ordered according to the same comparator as
this collection. |
void |
clear()
Removes all of the elements from this list.
|
SortedCollection<T> |
clone()
Returns a shallow copy of this SortedCollection instance.
|
SortedCollection<T> |
clone(boolean copyData)
Returns a shell or shallow copy of this SortedCollection instance.
|
protected int |
compare(T key,
T object)
Compares its two arguments for order.
|
boolean |
contains(java.lang.Object o)
Returns true if this list contains the specified element.
|
boolean |
containsAll(java.util.Collection<?> c)
Returns true if this collection contains all of the elements
in the specified collection.
|
T |
element() |
protected T |
get(int index)
Returns the element at the specified position in this collection.
|
java.util.Comparator<? super T> |
getComparator()
Returns the comparator used by this collection.
|
int |
getMaximalCapacity()
Returns the maximal capacity of this collection.
|
boolean |
isEmpty()
Returns true if this collection contains no elements.
|
boolean |
isFull()
Returns true if this collection contains the maximal number of elements.
|
java.util.Iterator<T> |
iterator()
Returns an iterator over the elements in this collection.
|
java.util.Iterator<T> |
iterator(int skip,
int count)
Returns an iterator over the elements in this collection skipping the first
skip items and returning only count elements. |
boolean |
offer(T e) |
T |
peek() |
T |
poll() |
T |
popLast()
Deprecated.
Use
removeLast() method instead |
T |
remove() |
protected boolean |
remove(int index)
Removes the element at the specified position in this collection.
|
boolean |
remove(java.lang.Object o)
Removes the first occurrence of the specified element from this list,
if it is present.
|
boolean |
removeAll(java.util.Collection<?> c)
Removes all of this collection's elements that are also contained in the
specified collection.
|
protected boolean |
removeAll(int indexFrom,
int number)
Removes from this collection the specified number of elements starting at the specified position.
|
T |
removeFirst()
Removes and returns the first element of this collection according to the order
specified by the comparator.
|
void |
removeFirstN(int count)
Removes the specified number of top elements of this collection according to the order
specified by the comparator.
|
T |
removeLast()
Removes and returns the last element of this collection according to the order
specified by the comparator.
|
boolean |
retainAll(java.util.Collection<?> c)
Retains only the elements in this collection that are contained in the
specified collection.
|
protected void |
setMaximalCapacity(int capacity)
Set the maximal capacity of this collection.
|
int |
size()
Returns the number of elements in this collection.
|
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this list
in proper sequence (from first to last element).
|
<E> E[] |
toArray(E[] array)
Returns an array containing all of the elements in this list in proper
sequence (from first to last element); the runtime type of the returned
array is that of the specified array.
|
java.lang.String |
toString()
Returns a string representation of this collection.
|
binarySearch, first, fullSearch, indexOf, last, mergeSort
protected static final int DEFAULT_INITIAL_CAPACITY
protected static final int UNLIMITED_CAPACITY
public SortedCollection(int initialCapacity, int maximalCapacity, java.util.Comparator<? super T> comparator) throws java.lang.IllegalArgumentException
initialCapacity
- the initial capacity of the collection (if zero, the DEFAULT_INITIAL_CAPACITY
is used)maximalCapacity
- the maximal capacity of the collectioncomparator
- the comparator that defines orderingjava.lang.IllegalArgumentException
- if the specified initial or maximal capacity is invalidpublic SortedCollection(int initialCapacity, java.util.Comparator<? super T> comparator) throws java.lang.IllegalArgumentException
initialCapacity
- the initial capacity of the collectioncomparator
- the comparator that defines orderingjava.lang.IllegalArgumentException
- if the specified initial or maximal capacity is invalidpublic SortedCollection(java.util.Comparator<? super T> comparator)
comparator
- the comparator that defines orderingpublic SortedCollection(int initialCapacity) throws java.lang.IllegalArgumentException
initialCapacity
- the initial capacity of the collectionjava.lang.IllegalArgumentException
- if the specified initial or maximal capacity is invalidpublic SortedCollection()
DEFAULT_INITIAL_CAPACITY
and maximal capacity is not limited.protected int compare(T key, T object) throws java.lang.ClassCastException
SortedArrayData
compare
in class SortedArrayData<T,T>
key
- the key to indexCompareobject
- the object to be comparedjava.lang.ClassCastException
- if the arguments' types prevent them from
being compared by this comparator.public java.util.Comparator<? super T> getComparator()
public int size()
size
in interface java.util.Collection<T>
size
in class SortedArrayData<T,T>
public boolean isEmpty()
isEmpty
in interface java.util.Collection<T>
public boolean isFull()
public int getMaximalCapacity()
protected void setMaximalCapacity(int capacity)
size
.capacity
- the new collection maximal capacity (zero or negative value means unlimited capacity)public boolean contains(java.lang.Object o)
contains
in interface java.util.Collection<T>
o
- element whose presence in this list is to be testedpublic boolean containsAll(java.util.Collection<?> c)
containsAll
in interface java.util.Collection<T>
c
- collection to be checked for containment in this collectionjava.lang.NullPointerException
- if the specified collection is nullcontains(Object)
protected final T get(int index)
get
in class SortedArrayData<T,T>
index
- index of the element to returnjava.lang.IndexOutOfBoundsException
- if the index is out of range
(index < 0 || index >= size())public T removeLast() throws java.util.NoSuchElementException
java.util.NoSuchElementException
- if the collection is empty@Deprecated public T popLast() throws java.util.NoSuchElementException
removeLast()
method insteadjava.util.NoSuchElementException
- if the collection is emptypublic T removeFirst() throws java.util.NoSuchElementException
java.util.NoSuchElementException
- if the collection is emptypublic void removeFirstN(int count) throws java.util.NoSuchElementException
count
- number of objects to be removedjava.util.NoSuchElementException
- if less then specified number of objects is storedpublic final SortedCollection<T> clone() throws java.lang.CloneNotSupportedException
clone
in class java.lang.Object
java.lang.CloneNotSupportedException
- if there was a problem cloning this collectionpublic SortedCollection<T> clone(boolean copyData) throws java.lang.CloneNotSupportedException
copyData
parameter is false, only the collection shell
is copied but no data, i.e. this creates a new instance of an empty collection
with the same settings as the original one. Otherwise, the data are also copied
as references.
The comparator is shared with the new instance.copyData
- if true the collection data are copied as references, otherwise,
only the collection shell is copied but no data, i.e. this creates a new instance of
an empty collection with the same settings as the original onejava.lang.CloneNotSupportedException
- if there was a problem cloning this collectionpublic java.lang.Object[] toArray()
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray
in interface java.util.Collection<T>
public <E> E[] toArray(E[] array)
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
toArray
in interface java.util.Collection<T>
E
- the type of array componentsarray
- the array into which the elements of the list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.java.lang.ArrayStoreException
- if the runtime type of the specified array
is not a supertype of the runtime type of every element in
this listjava.lang.NullPointerException
- if the specified array is nullpublic boolean add(T e)
protected boolean add(T e, int index)
e
- element to be appended to this listindex
- index on which the object should be storedpublic boolean remove(java.lang.Object o)
remove
in interface java.util.Collection<T>
o
- element to be removed from this list, if presentprotected boolean remove(int index)
index
- index of the element to removeprotected boolean removeAll(int indexFrom, int number)
indexFrom
- index of the first element to removed (inclusive)number
- number of objects to be removed from this collectionpublic boolean addAll(java.util.Collection<? extends T> c)
SortedCollection
over the same type, then it is assumed that
it is ordered by the same comparator and these lists are effectively merged.addAll
in interface java.util.Collection<T>
c
- collection containing elements to be added to this listjava.lang.NullPointerException
- if the specified collection is nullprotected boolean addAllSortedArray(java.lang.Object[] array, int size)
array
are ordered according to the same comparator
as
this collection. If capacity C would be exceeded, then only the top-C objects remain in the merged answer.array
- array of objects ordered according to the same comparator
as this collectionsize
- number of elements from the array to be added to this collection (the rest is ignored)array
is added.java.lang.NullPointerException
- if the specified array is nullpublic boolean removeAll(java.util.Collection<?> c)
removeAll
in interface java.util.Collection<T>
c
- collection containing elements to be removed from this collectionjava.lang.NullPointerException
- if the specified collection is nullremove(Object)
,
contains(Object)
public boolean retainAll(java.util.Collection<?> c)
retainAll
in interface java.util.Collection<T>
c
- collection containing elements to be retained in this collectionjava.lang.NullPointerException
- if the specified collection is nullremove(Object)
,
contains(Object)
public void clear()
clear
in interface java.util.Collection<T>
public java.lang.String toString()
String.valueOf(Object)
.toString
in class java.lang.Object
public java.util.Iterator<T> iterator()
public java.util.Iterator<T> iterator(int skip, int count)
skip
items and returning only count
elements. If count
is less than or equal to zero, all objects from the collection (except for
skip
) are returned. Note that their order is defined by the comparator.skip
- number of items to skipcount
- number of items to iterate (maximally, can be less)