viceroy.misc.typeSafeCollections
Class TypeSafeCollection

java.lang.Object
  |
  +--viceroy.misc.typeSafeCollections.TypeSafeCollection
All Implemented Interfaces:
java.util.Collection
Direct Known Subclasses:
TypeSafeList, TypeSafeSet

class TypeSafeCollection
extends java.lang.Object
implements java.util.Collection

Implementation of the java.util.Collection class which accepts only objects of a given type for add operations. Is backed by another implementation of Collection.


Field Summary
private  java.util.Collection _collection
           
private  java.lang.Class _type
           
 
Constructor Summary
TypeSafeCollection(java.util.Collection collection, java.lang.Class type)
          Constructs a new TypeSafeCollection object.
 
Method Summary
 boolean add(java.lang.Object o)
          see java.util.Collection Checks the type of the object.
 boolean addAll(java.util.Collection c)
          see java.util.Collection Checks the type of the objects in the Collection.
protected  void checkType(java.lang.Object o)
          Tests whether the an object is an instance of the allowed type.
 void clear()
          see java.util.Collection
 boolean contains(java.lang.Object o)
          see java.util.Collection
 boolean containsAll(java.util.Collection c)
          see java.util.Collection
 boolean equals(java.lang.Object o)
          see java.util.Collection
protected  java.lang.Class getType()
          Returns the type of objects which can be added to this collection
 boolean isEmpty()
          see java.util.Collection
 java.util.Iterator iterator()
          see java.util.Collection
 boolean remove(java.lang.Object o)
          see java.util.Collection
 boolean removeAll(java.util.Collection c)
          see java.util.Collection
 boolean retainAll(java.util.Collection c)
          see java.util.Collection
 int size()
          see java.util.Collection
 java.lang.Object[] toArray()
          see java.util.Collection
 java.lang.Object[] toArray(java.lang.Object[] a)
          see java.util.Collection
 java.lang.String toString()
          Returns a string representation of the TypeSafeCollection.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
hashCode
 

Field Detail

_collection

private java.util.Collection _collection

_type

private java.lang.Class _type
Constructor Detail

TypeSafeCollection

public TypeSafeCollection(java.util.Collection collection,
                          java.lang.Class type)
Constructs a new TypeSafeCollection object. iterates over the given collection and checks that all the given elements in the Collection match the given type

Parameters:
collection - the Collection to be wrapped
type - the type of the objects which can be added
Method Detail

checkType

protected void checkType(java.lang.Object o)
Tests whether the an object is an instance of the allowed type.

Parameters:
o - The object to be checked
Throws:
java.lang.ClassCastException - if the object is not of the right type

getType

protected java.lang.Class getType()
Returns the type of objects which can be added to this collection


size

public int size()
see java.util.Collection

Specified by:
size in interface java.util.Collection

isEmpty

public boolean isEmpty()
see java.util.Collection

Specified by:
isEmpty in interface java.util.Collection

contains

public boolean contains(java.lang.Object o)
see java.util.Collection

Specified by:
contains in interface java.util.Collection

iterator

public java.util.Iterator iterator()
see java.util.Collection

Specified by:
iterator in interface java.util.Collection

toArray

public java.lang.Object[] toArray()
see java.util.Collection

Specified by:
toArray in interface java.util.Collection

toArray

public java.lang.Object[] toArray(java.lang.Object[] a)
see java.util.Collection

Specified by:
toArray in interface java.util.Collection

add

public boolean add(java.lang.Object o)
see java.util.Collection Checks the type of the object.

Specified by:
add in interface java.util.Collection

remove

public boolean remove(java.lang.Object o)
see java.util.Collection

Specified by:
remove in interface java.util.Collection

containsAll

public boolean containsAll(java.util.Collection c)
see java.util.Collection

Specified by:
containsAll in interface java.util.Collection

addAll

public boolean addAll(java.util.Collection c)
see java.util.Collection Checks the type of the objects in the Collection.

Specified by:
addAll in interface java.util.Collection

removeAll

public boolean removeAll(java.util.Collection c)
see java.util.Collection

Specified by:
removeAll in interface java.util.Collection

retainAll

public boolean retainAll(java.util.Collection c)
see java.util.Collection

Specified by:
retainAll in interface java.util.Collection

clear

public void clear()
see java.util.Collection

Specified by:
clear in interface java.util.Collection

equals

public boolean equals(java.lang.Object o)
see java.util.Collection

Specified by:
equals in interface java.util.Collection
Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Returns a string representation of the TypeSafeCollection.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the object.