|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgoldman.collection.AbstractCollection<E>
public abstract class AbstractCollection<E>
The AbstractCollection class implements methods that can be shared by all
data structures that implement a collection. Some of these methods involve
maintaining and accessing the number of elements in the collection (e.g.
size
, isEmpty
), and others are implemented in
terms of abstract methods like add
. In some cases, the
implementations provided here are "brute-force," implementations
but they can be overridden
when a more efficient algorithm exists for a specific data structure. Also, the methodology
and associated instance variable version
,
for triggering a ConcurrentModificationException
when appropriate, is included
where to be inherited by all collection data structures.
Nested Class Summary | |
---|---|
class |
AbstractCollection.AbstractLocator<T extends E>
|
class |
AbstractCollection.VisitingIterator
|
Field Summary | |
---|---|
Comparator<? super E> |
comp
|
static int |
DEFAULT_CAPACITY
|
protected static int |
FORE
|
protected static int |
NOT_FOUND
|
protected int |
size
|
protected Version |
version
|
Constructor Summary | |
---|---|
AbstractCollection(Comparator<? super E> comparator)
|
Method Summary | ||
---|---|---|
void |
accept(Visitor<? super E> v)
Traverses the entire collection on behalf of a visitor. |
|
void |
addAll(Collection<? extends E> c)
Adds all elements in c to the collection. |
|
void |
checkRep()
Checks, for the purposes of checking correctness, that the correctness properties are preserved. |
|
void |
clear()
Removes all items from the collection. |
|
protected int |
compare(E e1,
E e2)
|
|
boolean |
contains(E value)
This implementation for contains takes linear time, so it
is overridden by a more efficient method in most data structure
implementations. |
|
void |
ensureCapacity(int capacity)
Increases the capacity of the collection, if necessary, to ensure that it can hold at least capacity elements. |
|
protected boolean |
equivalent(E e1,
E e2)
|
|
int |
getCapacity()
By default this method returns Integer.MAX_VALUE . |
|
Comparator<? super E> |
getComparator()
Returns the comparator for elements in this collection. |
|
static
|
getElementAtRank(Collection<T> coll,
int rank)
This is a non-mutating method. |
|
static
|
getElementAtRank(Collection<T> coll,
int rank,
Comparator<? super T> comp)
This is a non-mutating method. |
|
E |
getEquivalentElement(E target)
Returns an element in the collection that is equivalent to target . |
|
int |
getSize()
Returns the number of elements, size, in this collection. |
|
boolean |
isEmpty()
Returns true if this collection contains no elements, and otherwise returns false. |
|
void |
retainAll(Collection<E> c)
Updates the current collection to contain only elements that are also in c |
|
Object[] |
toArray()
Returns a Java primitive array of length n that holds the elements in this collection in the iteration order. |
|
E[] |
toArray(E[] array)
Fills a Java array with the elements in this collection in the iteration order, and returns the array that was filled. |
|
String |
toString()
Returns a string that includes each element in this collection (as produced by the toString method for that element), in the iteration order. |
|
protected void |
traverseForVisitor(Visitor<? super E> v)
Traverses the collection applying v to each element |
|
void |
trimToSize()
Trims the capacity of this collection to be its current size. |
|
protected void |
writeElements(StringBuilder s)
Appends, to the string buffer a comma-separated string representation for each element in the collection, in the iteration order. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface goldman.collection.Collection |
---|
add, getLocator, iterator, remove |
Field Detail |
---|
protected static final int FORE
protected static final int NOT_FOUND
public static final int DEFAULT_CAPACITY
protected int size
protected Version version
public Comparator<? super E> comp
Constructor Detail |
---|
public AbstractCollection(Comparator<? super E> comparator)
Method Detail |
---|
public void checkRep()
checkRep
in interface Collection<E>
public boolean isEmpty()
Collection
isEmpty
in interface Collection<E>
public int getSize()
Collection
getSize
in interface Collection<E>
public int getCapacity()
Integer.MAX_VALUE
.
getCapacity
in interface Collection<E>
public Comparator<? super E> getComparator()
Collection
getComparator
in interface Collection<E>
protected int compare(E e1, E e2)
e1
- the first element to comparee2
- the second element to compare
e1
< e2
,
zero when e1
equals e2
, and
a positive value when e1
> e2
.protected boolean equivalent(E e1, E e2)
e1
- one element to comparee2
- the second element to compare
e1
and e2
are
equivalent with respect to the setpublic Object[] toArray()
Collection
toArray
in interface Collection<E>
public E[] toArray(E[] array)
Collection
toArray
in interface Collection<E>
array
- an array of the correct type into which the collection's elements
are to be placed
public boolean contains(E value)
contains
takes linear time, so it
is overridden by a more efficient method in most data structure
implementations.
contains
in interface Collection<E>
value
- the element to be located
public E getEquivalentElement(E target)
Collection
target
. It
throws a NoSuchElementException
when there is no equivalent
element. The contains
method should be used to
determine if an element is in the collection.
getEquivalentElement
in interface Collection<E>
target
- the target element
NoSuchElementException
- there is no equivalent element in the
collection.protected void writeElements(StringBuilder s)
s
- a string builderpublic String toString()
Collection
toString
method for that element), in the iteration order.
toString
in interface Collection<E>
toString
in class Object
public final void accept(Visitor<? super E> v)
accept
in interface Collection<E>
v
- a visitor
VisitAbortedException
- the traversal is aborted due to an
exception raised by the visitor,
in which case the cause held by the VisitAbortedException
is the
exception thrown by the visitor.protected void traverseForVisitor(Visitor<? super E> v) throws Exception
v
- a visitor
Exception
public static <T> T getElementAtRank(Collection<T> coll, int rank)
coll
- the collection to userank
- the rank of the desired element
public static <T> T getElementAtRank(Collection<T> coll, int rank, Comparator<? super T> comp)
coll
- the collection to userank
- the rank of the desired elementcomp
- the comparator that defines the ordering of the elements
public void ensureCapacity(int capacity)
capacity
elements.
ensureCapacity
in interface Collection<E>
capacity
- the desired capacity for the collectionpublic void trimToSize()
trimToSize
in interface Collection<E>
public void addAll(Collection<? extends E> c)
c
to the collection.
addAll
in interface Collection<E>
c
- the collection to be addedpublic void retainAll(Collection<E> c)
c
retainAll
in interface Collection<E>
c
- a collectionpublic void clear()
clear
in interface Collection<E>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |