Skip navigation links

Oracle® Coherence Java API Reference
Release 3.6.0.0

E15725-01


com.tangosol.util
Class InflatableCollection

java.lang.Object
  extended by java.util.AbstractCollection
      extended by com.tangosol.util.InflatableCollection

All Implemented Interfaces:
Iterable, Collection
Direct Known Subclasses:
InflatableList, InflatableSet

public abstract class InflatableCollection
extends AbstractCollection

A Collection implementation which optimizes memory consumption for collections that often contain just a single value. This implementation also reduces contention for read operations (e.g. contains, iterator, etc.)

Since:
Coherence 3.6
Author:
ch 2009.11.22

Field Summary
protected  Collection m_colValue
          Holds elements if there are more then one.
protected  Object m_oValue
          Holds a single value.
protected static Object NO_VALUE
          A marker value indicating that the single value has not been initialized.

 

Constructor Summary
InflatableCollection()
           

 

Method Summary
 boolean add(Object o)
          Ensures that this collection contains the specified element (optional operation).
 void clear()
          Removes all of the elements from this collection (optional operation).
 boolean contains(Object o)
          Returns true if this collection contains the specified element.
 boolean equals(Object o)
          Compares the specified object with this collection for equality.
 int hashCode()
          Returns the hash code value for this collection.
protected abstract  Collection instantiateCollection()
          Factory method used to create a new Collection.
 Iterator iterator()
          Returns an iterator over the elements contained in this collection.
 boolean remove(Object o)
          Removes a single instance of the specified element from this collection, if it is present (optional operation).
 int size()
          Returns the number of elements in this collection.
 Object[] toArray()
          Returns an array containing all of the elements in this collection.
 Object[] toArray(Object[] ao)
          Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

 

Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, removeAll, retainAll, toString

 

Field Detail

m_oValue

protected volatile Object m_oValue
Holds a single value. Once the collection becomes inflated, this slot is never used again.

m_colValue

protected volatile Collection m_colValue
Holds elements if there are more then one. Once initialized, never goes back to null. TODO: When we standardize on JDK 1.5+ this could be made a non-volatile

NO_VALUE

protected static final Object NO_VALUE
A marker value indicating that the single value has not been initialized.

Constructor Detail

InflatableCollection

public InflatableCollection()

Method Detail

equals

public boolean equals(Object o)
Compares the specified object with this collection for equality.

While the Collection interface adds no stipulations to the general contract for the Object.equals, programmers who implement the Collection interface "directly" (in other words, create a class that is a Collection but is not a Set or a List) must exercise care if they choose to override the Object.equals. It is not necessary to do so, and the simplest course of action is to rely on Object's implementation, but the implementer may wish to implement a "value comparison" in place of the default "reference comparison." (The List and Set interfaces mandate such value comparisons.)

The general contract for the Object.equals method states that equals must be symmetric (in other words, a.equals(b) if and only if b.equals(a)). The contracts for List.equals and Set.equals state that lists are only equal to other lists, and sets to other sets. Thus, a custom equals method for a collection class that implements neither the List nor Set interface must return false when this collection is compared to any list or set. (By the same logic, it is not possible to write a class that correctly implements both the Set and List interfaces.)

Parameters:
o - Object to be compared for equality with this collection.
Returns:
true if the specified object is equal to this collection
See Also:
Object.equals(Object), Set.equals(Object), List.equals(Object)

hashCode

public int hashCode()
Returns the hash code value for this collection. While the Collection interface adds no stipulations to the general contract for the Object.hashCode method, programmers should take note that any class that overrides the Object.equals method must also override the Object.hashCode method in order to satisfy the general contract for the Object.hashCodemethod. In particular, c1.equals(c2) implies that c1.hashCode()==c2.hashCode().
Returns:
the hash code value for this collection
See Also:
Object.hashCode(), Object.equals(Object)

add

public boolean add(Object o)
Ensures that this collection contains the specified element (optional operation). Returns true if the collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.) Collections that support this operation may place limitations on what elements may be added to the collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

This implementation always throws an UnsupportedOperationException.

Specified by:
add in interface Collection
Overrides:
add in class AbstractCollection
Parameters:
o - element whose presence in this collection is to be ensured.
Returns:
true if the collection changed as a result of the call.

remove

public boolean remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the collection contains one or more such elements. Returns true if the collection contained the specified element (or equivalently, if the collection changed as a result of the call).

This implementation iterates over the collection looking for the specified element. If it finds the element, it removes the element from the collection using the iterator's remove method.

Note that this implementation throws an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection contains the specified object.

Specified by:
remove in interface Collection
Overrides:
remove in class AbstractCollection
Parameters:
o - element to be removed from this collection, if present.
Returns:
true if the collection contained the specified element.

clear

public void clear()
Removes all of the elements from this collection (optional operation). The collection will be empty after this call returns (unless it throws an exception).

This implementation iterates over this collection, removing each element using the Iterator.remove operation. Most implementations will probably choose to override this method for efficiency.

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection is non-empty.

Specified by:
clear in interface Collection
Overrides:
clear in class AbstractCollection

contains

public boolean contains(Object o)
Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element.

Specified by:
contains in interface Collection
Overrides:
contains in class AbstractCollection
Parameters:
o - object to be checked for containment in this collection.
Returns:
true if this collection contains the specified element.

iterator

public Iterator iterator()
Returns an iterator over the elements contained in this collection.
Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in class AbstractCollection
Returns:
an iterator over the elements contained in this collection.

size

public int size()
Returns the number of elements in this collection. If the collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
Specified by:
size in interface Collection
Specified by:
size in class AbstractCollection
Returns:
the number of elements in this collection.

toArray

public Object[] toArray()
Returns an array containing all of the elements in this collection. If the collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array will be "safe" in that no references to it are maintained by the collection. (In other words, this method must allocate a new array even if the collection is backed by an Array). The caller is thus free to modify the returned array.

This implementation allocates the array to be returned, and iterates over the elements in the collection, storing each object reference in the next consecutive element of the array, starting with element 0.

Specified by:
toArray in interface Collection
Overrides:
toArray in class AbstractCollection
Returns:
an array containing all of the elements in this collection.

toArray

public Object[] toArray(Object[] ao)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), 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 collection only if the caller knows that the collection does not contain any null elements.)

If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

This implementation checks if the array is large enough to contain the collection; if not, it allocates a new array of the correct size and type (using reflection). Then, it iterates over the collection, storing each object reference in the next consecutive element of the array, starting with element 0. If the array is larger than the collection, a null is stored in the first location after the end of the collection.

Specified by:
toArray in interface Collection
Overrides:
toArray in class AbstractCollection
Parameters:
ao - the array into which the elements of the collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the collection.

instantiateCollection

protected abstract Collection instantiateCollection()
Factory method used to create a new Collection.
Returns:
a "real" implementation to use if this collection is expanded

Skip navigation links

Oracle® Coherence Java API Reference
Release 3.6.0.0

E15725-01


Copyright © 2000, 2010, Oracle and/or its affiliates. All rights reserved.