Skip navigation links

Oracle® Coherence Java API Reference
Release 3.7.0.0

E18683-01


com.tangosol.util
Class InflatableCollection

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

All Implemented Interfaces:
java.lang.Iterable, java.util.Collection
Direct Known Subclasses:
InflatableList, InflatableSet

public abstract class InflatableCollection
extends java.util.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

Nested Class Summary
protected static interface InflatableCollection.InflatedCollection
          A marker interface which is used to identify internally inflated Collections.

 

Field Summary
protected  java.util.concurrent.atomic.AtomicReference m_refValue
          Holds NO_VALUE, a single value, or an InflatedCollection of values.
protected static java.lang.Object NO_VALUE
          A marker value indicating that the single value has not been initialized.

 

Constructor Summary
InflatableCollection()
           

 

Method Summary
 boolean add(java.lang.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(java.lang.Object o)
          Returns true if this collection contains the specified element.
 boolean equals(java.lang.Object o)
          Compares the specified object with this collection for equality.
 int hashCode()
          Returns the hash code value for this collection.
protected abstract  InflatableCollection.InflatedCollection instantiateCollection()
          Factory method used to create a new Collection.
 java.util.Iterator iterator()
          Returns an iterator over the elements contained in this collection.
 boolean remove(java.lang.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.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this collection.
 java.lang.Object[] toArray(java.lang.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

NO_VALUE

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

m_refValue

protected final java.util.concurrent.atomic.AtomicReference m_refValue
Holds NO_VALUE, a single value, or an InflatedCollection of values.

Constructor Detail

InflatableCollection

public InflatableCollection()

Method Detail

equals

public boolean equals(java.lang.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(java.lang.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 java.util.Collection
Overrides:
add in class java.util.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(java.lang.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 java.util.Collection
Overrides:
remove in class java.util.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 java.util.Collection
Overrides:
clear in class java.util.AbstractCollection

contains

public boolean contains(java.lang.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 java.util.Collection
Overrides:
contains in class java.util.AbstractCollection
Parameters:
o - object to be checked for containment in this collection.
Returns:
true if this collection contains the specified element.

iterator

public java.util.Iterator iterator()
Returns an iterator over the elements contained in this collection.
Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in class java.util.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 java.util.Collection
Specified by:
size in class java.util.AbstractCollection
Returns:
the number of elements in this collection.

toArray

public java.lang.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 java.util.Collection
Overrides:
toArray in class java.util.AbstractCollection
Returns:
an array containing all of the elements in this collection.

toArray

public java.lang.Object[] toArray(java.lang.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 java.util.Collection
Overrides:
toArray in class java.util.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 InflatableCollection.InflatedCollection instantiateCollection()
Factory method used to create a new Collection. The returned Collection must provide a "safe" iterator.
Returns:
a "real" implementation to use if this collection is expanded

Skip navigation links

Oracle® Coherence Java API Reference
Release 3.7.0.0

E18683-01


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