public abstract class XShortCollectionBase extends Object implements XShortCollection
This class represents collections which can quickly be iterated over
(forward or backward) and which an be made thread-safe
and/or unmodifiable.
XShortCollections can be iterated over without creating new objects
and without using iterators .
[code]
public boolean search(Object item, XCollection c) {
for (ShortRecord r = c.head(), end = c.tail(); (r = r.getNext()) != end;) {
if (item.equals(c.valueOf(r))) return true;
}
return false;
}
[/code]
Alternatively XCollection}s can be iterated over using reusableIterator(),
Utilization of this iterators does not introduce garbage because it is created once.
This iterator may not be used concurrently by multiple threads as they are
single instance by nature.
XShortCollections are thread-safe when marked shared
(can be read, iterated over or modified concurrently).
[code]
public class Foo {
private static final CollectionXLinkedHashMap().shared();
public Foo() {
INSTANCES.add(this);
}
public static void showInstances() {
for (Foo foo : INSTANCES) { // Iterations are thread-safe even if new Foo instances are added.
System.out.println(foo);
}
}
}[/code]
This class was based on collections code from http://javolution.org, but modified for use outside of realtime jvms.
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(short value)
Appends the specified value to the end of this collection
(optional operation).
|
boolean |
addAll(XShortCollection c)
Appends all of the values in the specified collection to the end of
this collection, in the order that they are returned by the specified
collection's iterator.
|
void |
clear()
Removes all of the values from this collection (optional operation).
|
boolean |
contains(short value)
Indicates if this collection contains the specified value.
|
boolean |
containsAll(XShortCollection c)
Indicates if this collection contains all of the values of the
specified collection.
|
abstract void |
delete(XCollection.Record record)
Deletes the specified record from this collection.
|
boolean |
equals(Object obj)
Compares the specified object with this collection for equality.
|
int |
hashCode()
Returns the hash code for this collection.
|
abstract XCollection.Record |
head()
Returns the head record of this collection; it is the record such as
head().getNext() holds the first collection value. |
boolean |
isEmpty()
Indicates if this collection is empty.
|
XShortIterator |
iterator()
Returns an iterator over the elements in this collection
|
boolean |
remove(short value)
Removes the first occurrence in this collection of the specified value
(optional operation).
|
boolean |
removeAll(XShortCollection c)
Removes from this collection all the values that are contained in the
specified collection.
|
boolean |
retainAll(XShortCollection c)
Retains only the values in this collection that are contained in the
specified collection.
|
XShortIterator |
reusableIterator()
Returns an iterator over the elements in this collection.
|
XShortCollection |
shared()
Returns a thread-safe read-write view of this collection.
|
abstract int |
size()
Returns the number of values in this collection.
|
boolean |
supportsIteratorModifications()
Tests if modification operations are supported for this collection's iterators.
|
abstract XCollection.Record |
tail()
Returns the tail record of this collection; it is the record such as
tail().getPrevious() holds the last collection value. |
short[] |
toArray()
Returns a new array allocated on the heap containing all of the values
in this collection in proper sequence.
|
short[] |
toArray(short[] array)
Fills the specified array with the values of this collection in
the proper sequence.
|
String |
toString()
Returns a string representation of this collection.
|
XShortCollection |
unmodifiable()
Returns the unmodifiable view associated to this collection.
|
abstract short |
valueOf(XCollection.Record record)
Returns the collection value for the specified record.
|
public abstract int size()
size in interface XShortCollectionpublic abstract XCollection.Record head()
head().getNext() holds the first collection value.head in interface XShortCollectionpublic abstract XCollection.Record tail()
tail().getPrevious() holds the last collection value.tail in interface XShortCollectionpublic abstract short valueOf(XCollection.Record record)
valueOf in interface XShortCollectionrecord - the record whose current value is returned.public abstract void delete(XCollection.Record record)
Implementation must ensure that removing a record from the collection does not affect in any way the records preceding the record being removed (it might affect the next records though, e.g. in a list collection, the indices of the subsequent records will change).
delete in interface XShortCollectionrecord - the record to be removed.UnsupportedOperationException - if not supported.public XShortCollection unmodifiable()
UnsupportedOperationException being thrown.unmodifiable in interface XShortCollectionpublic XShortCollection shared()
Returns a thread-safe read-write view of this collection.
The default implementation performs synchronization on read and write. Sub-classes may provide more efficient implementation (e.g. only synchronizing on writes modifying the internal data structure).
Having a shared collection does not mean that modifications made by onethread are automatically viewed by others thread. Which in practice is not an issue. In a well-behaved system, threads need to synchronize only at predetermined synchronization points (the fewer the better).
shared in interface XShortCollectionpublic XShortIterator iterator()
iterator in interface XShortCollectionpublic XShortIterator reusableIterator()
XCollectionIterator.toFirst() on the iterator prior to returning
it.reusableIterator in interface XShortCollectionpublic boolean supportsIteratorModifications()
XShortCollectionsupportsIteratorModifications in interface XShortCollectionpublic boolean add(short value)
Note: This default implementation always throws
UnsupportedOperationException.
add in interface XShortCollectionvalue - the value to be appended to this collection.true (as per the general contract of the
Collection.add method).UnsupportedOperationException - if not supported.public boolean remove(short value)
remove in interface XShortCollectionvalue - the value to be removed from this collection.true if this collection contained the specified
value; false otherwise.UnsupportedOperationException - if not supported.public void clear()
clear in interface XShortCollectionUnsupportedOperationException - if not supported.public boolean isEmpty()
isEmpty in interface XShortCollectiontrue if this collection contains no value;
false otherwise.public boolean contains(short value)
contains in interface XShortCollectionvalue - the value whose presence in this collection
is to be tested.true if this collection contains the specified
value;false otherwise.public boolean addAll(XShortCollection c)
addAll in interface XShortCollectionc - collection whose values are to be added to this collection.true if this collection changed as a result of
the call; false otherwise.public boolean containsAll(XShortCollection c)
containsAll in interface XShortCollectionc - collection to be checked for containment in this collection.true if this collection contains all of the values
of the specified collection; false otherwise.public boolean removeAll(XShortCollection c)
removeAll in interface XShortCollectionc - collection that defines which values will be removed from
this collection.true if this collection changed as a result of
the call; false otherwise.public boolean retainAll(XShortCollection c)
retainAll in interface XShortCollectionc - collection that defines which values this set will retain.true if this collection changed as a result of
the call; false otherwise.public short[] toArray()
Note: To avoid heap allocation toArray(short[]) is
recommended.
toArray in interface XShortCollectiontoArray(new Object[size()])public short[] toArray(short[] array)
toArray in interface XShortCollectionarray - the array into which the values of this collection
are to be stored.public String toString()
String.valueOf(Object).public boolean equals(Object obj)
List instances).equals in interface XShortCollectionequals in class Objectobj - the object to be compared for equality with this collectiontrue if the specified object is a collection with
the same content and iteration order when necessary;
false otherwise.public int hashCode()
hashCode in interface XShortCollectionhashCode in class ObjectCopyright © 2019 Neeve Research, LLC. All Rights Reserved.