com.tensegrity.generic.util
Class CombinedCollection

java.lang.Object
  extended bycom.tensegrity.generic.util.CombinedCollection
All Implemented Interfaces:
java.util.Collection, java.util.List

public class CombinedCollection
extends java.lang.Object
implements java.util.List

Class CombinedCollection merges two collections into a new collection view consisting of the contents of both. Because the underlying collections are not physically combined, the cost of creating the combined List is linear.

An instance does not allow modification to any of its contents, but changes to the underlying collections are immediately reflected. The sole exception is during an iteration. In other words, it is possible to iterate over a CombinedCollection and change the underlying lists without having a ConcurrentModificationException being thrown.

It is not possible to alter the combined collection instance or their contents directly, however.

Unsupported operations will cause a RuntimeException to be thrown, which indicates that the user is trying something defined by the Collection interface yet which contradicts the semantics of the read-only view that a CombinedCollection provides.

The cost of creating a new CombinedCollection instance is very low and allows quick pseudo-concatenation of two existing Collection instances. The resulting object can be traversed with an Iterator and its size can also be determined. Additionally, the instance can be passed to constructors of other standard Collection objects, since its toObject method has been appropriately overridden. The number of operations that are supported by this class are limited, however, and restricted to read-only operations.

Combined collections can be recursively nested. Suppose you have three rather large collections, for example ArrayLists, and you wish to combine them into a single Collection object. The code would look like this:

 public Collection combineArrayLists (ArrayList a, ArrayList b, ArrayList c)
 {
   return new CombinedCollection ( a, new CombinedCollection (b, c));
 } 
 

Version:
$Id: CombinedCollection.java,v 1.29 2005/11/14 15:50:04 KevinCVS Exp $
Author:
StepanRutz

Field Summary
protected  java.util.Collection a
          Reference to the first of the two combined collections.
protected  java.util.Collection b
          Reference to the second of the two combined collections.
 
Constructor Summary
CombinedCollection(java.util.Collection a, java.util.Collection b)
          Constructs a new CombinedCollection instance that consists of the two specified
 
Method Summary
 void add(int index, java.lang.Object element)
           
 boolean add(java.lang.Object o)
           
 boolean addAll(java.util.Collection c)
           
 boolean addAll(int index, java.util.Collection c)
           
 void clear()
           
 boolean contains(java.lang.Object o)
           
 boolean containsAll(java.util.Collection c)
           
 java.lang.Object get(int index)
           
 int indexOf(java.lang.Object o)
           
 boolean isEmpty()
           
 java.util.Iterator iterator()
           
 int lastIndexOf(java.lang.Object o)
           
 java.util.ListIterator listIterator()
           
 java.util.ListIterator listIterator(int index)
           
 java.lang.Object remove(int index)
           
 boolean remove(java.lang.Object o)
           
 boolean removeAll(java.util.Collection c)
           
 boolean retainAll(java.util.Collection c)
           
 java.lang.Object set(int index, java.lang.Object element)
           
 int size()
           
 java.util.List subList(int fromIndex, int toIndex)
           
 java.lang.Object[] toArray()
           
 java.lang.Object[] toArray(java.lang.Object[] array)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Field Detail

a

protected java.util.Collection a
Reference to the first of the two combined collections.


b

protected java.util.Collection b
Reference to the second of the two combined collections.

Constructor Detail

CombinedCollection

public CombinedCollection(java.util.Collection a,
                          java.util.Collection b)
Constructs a new CombinedCollection instance that consists of the two specified

Parameters:
a - the first collection to combine.
b - the second collection to combine.
Method Detail

size

public int size()
Specified by:
size in interface java.util.List
See Also:
Collection.size()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.List
See Also:
Collection.isEmpty()

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.List
See Also:
Collection.contains(java.lang.Object)

iterator

public java.util.Iterator iterator()
Specified by:
iterator in interface java.util.List
See Also:
Collection.iterator()

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.List
See Also:
Collection.toArray()

toArray

public java.lang.Object[] toArray(java.lang.Object[] array)
Specified by:
toArray in interface java.util.List
See Also:
Collection.toArray(java.lang.Object[])

add

public boolean add(java.lang.Object o)
Specified by:
add in interface java.util.List
See Also:
Collection.add(java.lang.Object)

remove

public boolean remove(java.lang.Object o)
Specified by:
remove in interface java.util.List
See Also:
Collection.remove(java.lang.Object)

containsAll

public boolean containsAll(java.util.Collection c)
Specified by:
containsAll in interface java.util.List
See Also:
Collection.containsAll(java.util.Collection)

addAll

public boolean addAll(java.util.Collection c)
Specified by:
addAll in interface java.util.List
See Also:
Collection.addAll(java.util.Collection)

addAll

public boolean addAll(int index,
                      java.util.Collection c)
Specified by:
addAll in interface java.util.List
See Also:
List.addAll(int, java.util.Collection)

removeAll

public boolean removeAll(java.util.Collection c)
Specified by:
removeAll in interface java.util.List
See Also:
Collection.removeAll(java.util.Collection)

retainAll

public boolean retainAll(java.util.Collection c)
Specified by:
retainAll in interface java.util.List
See Also:
Collection.retainAll(java.util.Collection)

clear

public void clear()
Specified by:
clear in interface java.util.List
See Also:
Collection.clear()

get

public java.lang.Object get(int index)
Specified by:
get in interface java.util.List
See Also:
List.get(int)

set

public java.lang.Object set(int index,
                            java.lang.Object element)
Specified by:
set in interface java.util.List
See Also:
List.set(int, java.lang.Object)

add

public void add(int index,
                java.lang.Object element)
Specified by:
add in interface java.util.List
See Also:
List.add(int, java.lang.Object)

remove

public java.lang.Object remove(int index)
Specified by:
remove in interface java.util.List
See Also:
List.remove(int)

indexOf

public int indexOf(java.lang.Object o)
Specified by:
indexOf in interface java.util.List
See Also:
List.indexOf(java.lang.Object)

lastIndexOf

public int lastIndexOf(java.lang.Object o)
Specified by:
lastIndexOf in interface java.util.List
See Also:
List.lastIndexOf(java.lang.Object)

listIterator

public java.util.ListIterator listIterator()
Specified by:
listIterator in interface java.util.List
See Also:
List.listIterator()

listIterator

public java.util.ListIterator listIterator(int index)
Specified by:
listIterator in interface java.util.List
See Also:
List.listIterator(int)

subList

public java.util.List subList(int fromIndex,
                              int toIndex)
Specified by:
subList in interface java.util.List
See Also:
List.subList(int, int)


Copyright © 2005 Tensegrity Software GmbH. All Rights Reserved. Date of creation: 09.06.2006.