com.neeve.lang
Class XLongCollectionBase

java.lang.Object
  extended by com.neeve.lang.XLongCollectionBase
All Implemented Interfaces:
XLongCollection
Direct Known Subclasses:
XLongIndexedList, XLongLinkedList

public abstract class XLongCollectionBase
extends Object
implements XLongCollection

This class represents collections which can quickly be iterated over (forward or backward) and which an be made thread-safe and/or unmodifiable.

XLongCollections can be iterated over without creating new objects and without using iterators . [code] public boolean search(Object item, XCollection c) { for (LongRecord 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.

XLongCollections are thread-safe when marked shared (can be read, iterated over or modified concurrently). [code] public class Foo { private static final Collection INSTANCES = new XLinkedHashMap().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.


Method Summary
 boolean add(long value)
          Appends the specified value to the end of this collection (optional operation).
 boolean addAll(XLongCollection 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(long value)
          Indicates if this collection contains the specified value.
 boolean containsAll(XLongCollection 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.
 XLongIterator iterator()
          Returns an iterator over the elements in this collection
 boolean remove(long value)
          Removes the first occurrence in this collection of the specified value (optional operation).
 boolean removeAll(XLongCollection c)
          Removes from this collection all the values that are contained in the specified collection.
 boolean retainAll(XLongCollection c)
          Retains only the values in this collection that are contained in the specified collection.
 XLongIterator reusableIterator()
          Returns an iterator over the elements in this collection.
 XLongCollection shared()
           Returns a thread-safe read-write view of this collection.
abstract  int size()
          Returns the number of values in this collection.
abstract  XCollection.Record tail()
          Returns the tail record of this collection; it is the record such as tail().getPrevious() holds the last collection value.
 long[] toArray()
          Returns a new array allocated on the heap containing all of the values in this collection in proper sequence.
 long[] toArray(long[] array)
          Fills the specified array with the values of this collection in the proper sequence.
 String toString()
          Returns a string representation of this collection.
 XLongCollection unmodifiable()
          Returns the unmodifiable view associated to this collection.
abstract  long valueOf(XCollection.Record record)
          Returns the collection value for the specified record.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

size

public abstract int size()
Returns the number of values in this collection.

Specified by:
size in interface XLongCollection
Returns:
the number of values.

head

public abstract XCollection.Record head()
Returns the head record of this collection; it is the record such as head().getNext() holds the first collection value.

Specified by:
head in interface XLongCollection
Returns:
the head record.

tail

public abstract XCollection.Record tail()
Returns the tail record of this collection; it is the record such as tail().getPrevious() holds the last collection value.

Specified by:
tail in interface XLongCollection
Returns:
the tail record.

valueOf

public abstract long valueOf(XCollection.Record record)
Returns the collection value for the specified record.

Specified by:
valueOf in interface XLongCollection
Parameters:
record - the record whose current value is returned.
Returns:
the current value.

delete

public abstract void delete(XCollection.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).

Specified by:
delete in interface XLongCollection
Parameters:
record - the record to be removed.
Throws:
UnsupportedOperationException - if not supported.

unmodifiable

public XLongCollection unmodifiable()
Returns the unmodifiable view associated to this collection. Attempts to modify the returned collection result in an UnsupportedOperationException being thrown.

Specified by:
unmodifiable in interface XLongCollection
Returns:
the unmodifiable view over this collection.

shared

public XLongCollection 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).

Specified by:
shared in interface XLongCollection
Returns:
a thread-safe collection.

iterator

public XLongIterator iterator()
Returns an iterator over the elements in this collection

Specified by:
iterator in interface XLongCollection
Returns:
an iterator over this collection's elements.

reusableIterator

public XLongIterator reusableIterator()
Returns an iterator over the elements in this collection. This iterator may only be used by a single thread at a time. This method invokes XCollectionIterator.toFirst() on the iterator prior to returning it.

Specified by:
reusableIterator in interface XLongCollection
Returns:
an iterator over this collection's elements.

add

public boolean add(long value)
Appends the specified value to the end of this collection (optional operation).

Note: This default implementation always throws UnsupportedOperationException.

Specified by:
add in interface XLongCollection
Parameters:
value - the value to be appended to this collection.
Returns:
true (as per the general contract of the Collection.add method).
Throws:
UnsupportedOperationException - if not supported.

remove

public boolean remove(long value)
Removes the first occurrence in this collection of the specified value (optional operation).

Specified by:
remove in interface XLongCollection
Parameters:
value - the value to be removed from this collection.
Returns:
true if this collection contained the specified value; false otherwise.
Throws:
UnsupportedOperationException - if not supported.

clear

public void clear()
Removes all of the values from this collection (optional operation).

Specified by:
clear in interface XLongCollection
Throws:
UnsupportedOperationException - if not supported.

isEmpty

public boolean isEmpty()
Indicates if this collection is empty.

Specified by:
isEmpty in interface XLongCollection
Returns:
true if this collection contains no value; false otherwise.

contains

public boolean contains(long value)
Indicates if this collection contains the specified value.

Specified by:
contains in interface XLongCollection
Parameters:
value - the value whose presence in this collection is to be tested.
Returns:
true if this collection contains the specified value;false otherwise.

addAll

public boolean addAll(XLongCollection 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.

Specified by:
addAll in interface XLongCollection
Parameters:
c - collection whose values are to be added to this collection.
Returns:
true if this collection changed as a result of the call; false otherwise.

containsAll

public boolean containsAll(XLongCollection c)
Indicates if this collection contains all of the values of the specified collection.

Specified by:
containsAll in interface XLongCollection
Parameters:
c - collection to be checked for containment in this collection.
Returns:
true if this collection contains all of the values of the specified collection; false otherwise.

removeAll

public boolean removeAll(XLongCollection c)
Removes from this collection all the values that are contained in the specified collection.

Specified by:
removeAll in interface XLongCollection
Parameters:
c - collection that defines which values will be removed from this collection.
Returns:
true if this collection changed as a result of the call; false otherwise.

retainAll

public boolean retainAll(XLongCollection c)
Retains only the values in this collection that are contained in the specified collection.

Specified by:
retainAll in interface XLongCollection
Parameters:
c - collection that defines which values this set will retain.
Returns:
true if this collection changed as a result of the call; false otherwise.

toArray

public long[] 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(long[]) is recommended.

Specified by:
toArray in interface XLongCollection
Returns:
toArray(new Object[size()])

toArray

public long[] toArray(long[] array)
Fills the specified array with the values of this collection in the proper sequence.

Specified by:
toArray in interface XLongCollection
Parameters:
array - the array into which the values of this collection are to be stored.
Returns:
the specified array.

toString

public String toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

Overrides:
toString in class Object
Returns:
a string representation of this collection

equals

public boolean equals(Object obj)
Compares the specified object with this collection for equality. The default behavior is to consider two collections equal if they hold the same values and have the same iterative order if any of the collections is an ordered collection (List instances).

Specified by:
equals in interface XLongCollection
Overrides:
equals in class Object
Parameters:
obj - the object to be compared for equality with this collection
Returns:
true if the specified object is a collection with the same content and iteration order when necessary; false otherwise.

hashCode

public int hashCode()
Returns the hash code for this collection. For non-ordered collection the hashcode of this collection is the sum of the hashcode of its values.

Specified by:
hashCode in interface XLongCollection
Overrides:
hashCode in class Object
Returns:
the hash code for this collection.


Copyright © 2016 Neeve Research, LLC. All Rights Reserved.