Class FastCollection<E>
- java.lang.Object
-
- javolution37.javolution.realtime.RealtimeObject
-
- javolution37.javolution.util.FastCollection<E>
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, Realtime
public abstract class FastCollection<E> extends RealtimeObject implements java.util.Collection<E>, java.io.Serializable
This class represents collections which can quickly be iterated over (forward or backward) in a thread-safe manner without creating new objects and without using
iterators. For example:boolean search(Object item, FastCollection c) { for (Record r = c.head(), end = c.tail(); (r = r.getNext()) != end;) { if (item.equals(c.valueOf(r))) return true; } return false; }Iterations are thread-safe as long as the
recordsequence iterated over is not structurally modified by another thread (objects can safely be append/prepend during iterations but not inserted/removed).Users may provide a read-only view of any
FastCollectioninstance using theunmodifiable()method (the view is thread-safe if iterations are thread-safe). For example:public class Polynomial { private final FastTable<Coefficient> _coefficients = new FastTable<Coefficient>(); public List<Coefficient> getCoefficients() { // Read-only view. return _coefficients.unmodifiable(); } }Finally,
FastCollectionmay use customcomparatorsfor element equality or ordering if the collection is ordered (e.g.FastTree).- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static interfaceFastCollection.RecordThis interface represents the collection records which can directly be iterated over.-
Nested classes/interfaces inherited from class javolution37.javolution.realtime.RealtimeObject
RealtimeObject.Factory<T extends RealtimeObject>
-
Nested classes/interfaces inherited from interface javolution37.javolution.realtime.Realtime
Realtime.ObjectSpace
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description booleanadd(E value)Appends the specified value to the end of this collection (optional operation).booleanaddAll(java.util.Collection<? extends E> 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 or the node order if the specified collection is aFastCollection.voidclear()Removes all of the values from this collection (optional operation).booleancontains(java.lang.Object value)Indicates if this collection contains the specified value.booleancontainsAll(java.util.Collection<?> c)Indicates if this collection contains all of the values of the specified collection.abstract voiddelete(FastCollection.Record record)Deletes the specified record from this collection.booleanequals(java.lang.Object obj)Compares the specified object with this collection for equality.FastComparatorgetValueComparator()Returns the value comparator for this collection (defaultFastComparator.DEFAULT).inthashCode()Returns the hash code for this collection (independent from the collection order; unless this collection is a list instance).abstract FastCollection.Recordhead()Returns the head record of this collection; it is the record such ashead().getNext()holds the first collection value.booleanisEmpty()Indicates if this collection is empty.java.util.Iterator<E>iterator()Returns an iterator over the elements in this collection (allocated on the stack when executed in aPoolContext).booleanremove(java.lang.Object value)Removes the first occurrence in this collection of the specified value (optional operation).booleanremoveAll(java.util.Collection<?> c)Removes from this collection all the values that are contained in the specified collection.booleanretainAll(java.util.Collection<?> c)Retains only the values in this collection that are contained in the specified collection.FastCollection<E>setValueComparator(FastComparator comparator)Sets the comparator to use for value equality or ordering if the collection is ordered (e.g.abstract intsize()Returns the number of values in this collection.abstract FastCollection.Recordtail()Returns the tail record of this collection; it is the record such astail().getPrevious()holds the last collection value.java.lang.Object[]toArray()Returns a new array allocated on the heap containing all of the values in this collection in proper sequence.<T> T[]toArray(T[] array)Fills the specified array with the values of this collection in the proper sequence.TexttoText()Returns the textual representation of this collection.java.util.Collection<E>unmodifiable()Returns the unmodifiable view associated to this collection.abstract EvalueOf(FastCollection.Record record)Returns the collection value for the specified record.-
Methods inherited from class javolution37.javolution.realtime.RealtimeObject
export, move, moveHeap, preserve, toString, unpreserve
-
-
-
-
Method Detail
-
size
public abstract int size()
Returns the number of values in this collection.- Specified by:
sizein interfacejava.util.Collection<E>- Returns:
- the number of values.
-
head
public abstract FastCollection.Record head()
Returns the head record of this collection; it is the record such ashead().getNext()holds the first collection value.- Returns:
- the head record.
-
tail
public abstract FastCollection.Record tail()
Returns the tail record of this collection; it is the record such astail().getPrevious()holds the last collection value.- Returns:
- the tail record.
-
valueOf
public abstract E valueOf(FastCollection.Record record)
Returns the collection value for the specified record.- Parameters:
record- the record whose current value is returned.- Returns:
- the current value.
-
delete
public abstract void delete(FastCollection.Record record)
Deletes the specified record from this collection.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).
- Parameters:
record- the record to be removed.- Throws:
java.lang.UnsupportedOperationException- if not supported.
-
unmodifiable
public java.util.Collection<E> unmodifiable()
Returns the unmodifiable view associated to this collection. Attempts to modify the returned collection result in anUnsupportedOperationExceptionbeing thrown. The view is typically part of the collection itself (created only once) and also an instance ofFastCollectionsupporting direct iterations.- Returns:
- the unmodifiable view over this collection.
-
iterator
public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this collection (allocated on the stack when executed in aPoolContext).
-
setValueComparator
public FastCollection<E> setValueComparator(FastComparator comparator)
Sets the comparator to use for value equality or ordering if the collection is ordered (e.g.FastTree).- Parameters:
comparator- the value comparator.- Returns:
this
-
getValueComparator
public FastComparator getValueComparator()
Returns the value comparator for this collection (defaultFastComparator.DEFAULT).- Returns:
- the comparator to use for value equality (or ordering if the collection is ordered)
-
add
public boolean add(E value)
Appends the specified value to the end of this collection (optional operation).Note: This default implementation always throws
UnsupportedOperationException.- Specified by:
addin interfacejava.util.Collection<E>- Parameters:
value- the value to be appended to this collection.- Returns:
true(as per the general contract of theCollection.addmethod).- Throws:
java.lang.UnsupportedOperationException- if not supported.
-
remove
public boolean remove(java.lang.Object value)
Removes the first occurrence in this collection of the specified value (optional operation).- Specified by:
removein interfacejava.util.Collection<E>- Parameters:
value- the value to be removed from this collection.- Returns:
trueif this collection contained the specified value;falseotherwise.- Throws:
java.lang.UnsupportedOperationException- if not supported.
-
clear
public void clear()
Removes all of the values from this collection (optional operation).- Specified by:
clearin interfacejava.util.Collection<E>- Throws:
java.lang.UnsupportedOperationException- if not supported.
-
isEmpty
public final boolean isEmpty()
Indicates if this collection is empty.- Specified by:
isEmptyin interfacejava.util.Collection<E>- Returns:
trueif this collection contains no value;falseotherwise.
-
contains
public boolean contains(java.lang.Object value)
Indicates if this collection contains the specified value.- Specified by:
containsin interfacejava.util.Collection<E>- Parameters:
value- the value whose presence in this collection is to be tested.- Returns:
trueif this collection contains the specified value;falseotherwise.
-
addAll
public boolean addAll(java.util.Collection<? extends E> 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 or the node order if the specified collection is aFastCollection.- Specified by:
addAllin interfacejava.util.Collection<E>- Parameters:
c- collection whose values are to be added to this collection.- Returns:
trueif this collection changed as a result of the call;falseotherwise.
-
containsAll
public boolean containsAll(java.util.Collection<?> c)
Indicates if this collection contains all of the values of the specified collection.- Specified by:
containsAllin interfacejava.util.Collection<E>- Parameters:
c- collection to be checked for containment in this collection.- Returns:
trueif this collection contains all of the values of the specified collection;falseotherwise.
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
Removes from this collection all the values that are contained in the specified collection.- Specified by:
removeAllin interfacejava.util.Collection<E>- Parameters:
c- collection that defines which values will be removed from this collection.- Returns:
trueif this collection changed as a result of the call;falseotherwise.
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
Retains only the values in this collection that are contained in the specified collection.- Specified by:
retainAllin interfacejava.util.Collection<E>- Parameters:
c- collection that defines which values this set will retain.- Returns:
trueif this collection changed as a result of the call;falseotherwise.
-
toArray
public java.lang.Object[] toArray()
Returns a new array allocated on the heap containing all of the values in this collection in proper sequence.Note: To avoid heap allocation
toArray(Object[])is recommended.- Specified by:
toArrayin interfacejava.util.Collection<E>- Returns:
toArray(new Object[size()])
-
toArray
public <T> T[] toArray(T[] array)
Fills the specified array with the values of this collection in the proper sequence.Note: Unlike standard Collection, this method does not try to resize the array using reflection (which might not be supported) if the array is too small. UnsupportedOperationException is raised if the specified array is too small for this collection.
- Specified by:
toArrayin interfacejava.util.Collection<E>- Parameters:
array- the array into which the values of this collection are to be stored.- Returns:
- the specified array.
- Throws:
java.lang.UnsupportedOperationException- ifarray.length < size()
-
toText
public Text toText()
Returns the textual representation of this collection.- Specified by:
toTextin interfaceRealtime- Overrides:
toTextin classRealtimeObject- Returns:
- this collection textual representation.
-
equals
public boolean equals(java.lang.Object obj)
Compares the specified object with this collection for equality. Returnstrueif and only both collection contains the same values regardless of the order; unless this collection is a list instance in which case both collection must be list with the same order.- Specified by:
equalsin interfacejava.util.Collection<E>- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to be compared for equality with this collection.- Returns:
trueif the specified object is equal to this collection;falseotherwise.
-
hashCode
public int hashCode()
Returns the hash code for this collection (independent from the collection order; unless this collection is a list instance).- Specified by:
hashCodein interfacejava.util.Collection<E>- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hash code for this collection.
-
-
DMelt 3.0 © DataMelt by jWork.ORG