structure5
Class StructCollection<E>

java.lang.Object
  extended by structure5.StructCollection<E>
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Collection<E>

public class StructCollection<E>
extends java.lang.Object
implements java.util.Collection<E>

This utility class converts a Structure to a Collection. Users are advised to make use of the values method that is part of every Structure that returns a Collection.

This class is necessary because there are certain basic differences in the interfaces of the two classes.

This class works provides a facade: methods of the Collection interface are directly referred to the base Structure, or simple code supports the interface.

Example Usage:

 public static void main(String[] argv){
      Structure struct = new Vector();
      for(int i = 0; i < argv.length; i++) struct.add(argv[i]);
      Collection collect = new StructCollection(struct);
 } 
 

Since:
Java Structures, 2nd edition

Field Summary
protected  Structure<E> base
          The subordinate Structure
 
Constructor Summary
StructCollection(Structure<E> s)
          Constructs a Collection, based on the contents of the subordinate Structure object.
 
Method Summary
 boolean add(E o)
          Add an object to the subordinate Structure, and return boolean indicating success.
 boolean addAll(java.util.Collection<? extends E> c)
          Adds all the elements of another collection (c) into this Structure.
 void clear()
          Remove all elements from the Structure.
 boolean contains(java.lang.Object o)
          Returns true iff object o is contained within the subordinate structure.
 boolean containsAll(java.util.Collection<?> c)
          Returns true if all of the elements of c are contained within the subordinate structure.
 boolean equals(java.lang.Object o)
          Compare one StructCollection with another.
 int hashCode()
          Return hash code.
 boolean isEmpty()
          Detect an empty structure.
 java.util.Iterator<E> iterator()
          Return an iterator to traverse the subordinate structure.
 boolean remove(java.lang.Object o)
          Removes an instance of object o, if it appears within structure.
 boolean removeAll(java.util.Collection<?> c)
          Removes all objects found within collection c from this collection.
 boolean retainAll(java.util.Collection<?> c)
          Retains elements of this Collection that are also in c.
 int size()
          Determine the number of elements in this collection.
 java.lang.Object[] toArray()
          Construct an array of values found in the subordinate Structure.
<T> T[]
toArray(T[] target)
          Copy elements of this structure into target array.
 java.lang.String toString()
          Construct a string representation of this class.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

base

protected Structure<E> base
The subordinate Structure

Constructor Detail

StructCollection

public StructCollection(Structure<E> s)
Constructs a Collection, based on the contents of the subordinate Structure object.

Postcondition:
the Collection is a facade for manipulations to a subordinate Structure.
Method Detail

add

public boolean add(E o)
Add an object to the subordinate Structure, and return boolean indicating success.

Specified by:
add in interface java.util.Collection<E>
Returns:
true.
Postcondition:
the object o is added to the Structure, and true is returned

addAll

public boolean addAll(java.util.Collection<? extends E> c)
Adds all the elements of another collection (c) into this Structure. A boolean value of true is returned.

Specified by:
addAll in interface java.util.Collection<E>
Returns:
true if each of the adds returned true.
Precondition:
c is a valid collection
Postcondition:
the elements of c have been added to the Structure, and the value true is returned if all adds returned true.

clear

public void clear()
Remove all elements from the Structure.

Specified by:
clear in interface java.util.Collection<E>
Postcondition:
the subordinate structure is empty

contains

public boolean contains(java.lang.Object o)
Returns true iff object o is contained within the subordinate structure.

Specified by:
contains in interface java.util.Collection<E>
Precondition:
o is a non-null object
Postcondition:
returns true if o is equal to some object in the subordinate structure.

containsAll

public boolean containsAll(java.util.Collection<?> c)
Returns true if all of the elements of c are contained within the subordinate structure.

Specified by:
containsAll in interface java.util.Collection<E>
Returns:
true if c is a subset of this
Precondition:
c is a valid Collection
Postcondition:
returns true if all elements of c appear in this Structure

equals

public boolean equals(java.lang.Object o)
Compare one StructCollection with another.

Specified by:
equals in interface java.util.Collection<E>
Overrides:
equals in class java.lang.Object
Returns:
true iff Structures are equals
Precondition:
o is of similar type to base Structure
Postcondition:
returns true if both objects appear equal as Structures

hashCode

public int hashCode()
Return hash code.

Specified by:
hashCode in interface java.util.Collection<E>
Overrides:
hashCode in class java.lang.Object
Returns:
hash code of subordinate structure.
Postcondition:
hash code returned is the hashcode of the subordinate structure.

isEmpty

public boolean isEmpty()
Detect an empty structure.

Specified by:
isEmpty in interface java.util.Collection<E>
Returns:
true iff subordinate structure is empty.
Postcondition:
returns true iff subordinate structure is empty

iterator

public java.util.Iterator<E> iterator()
Return an iterator to traverse the subordinate structure.

Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Returns:
the iterator over the subordinate structure.
Postcondition:
returns an iterator over subordinate strucutre

remove

public boolean remove(java.lang.Object o)
Removes an instance of object o, if it appears within structure.

Specified by:
remove in interface java.util.Collection<E>
Returns:
returns true if instance was found and removed.
Precondition:
o is non-null
Postcondition:
removes up to one instance of o from structure

removeAll

public boolean removeAll(java.util.Collection<?> c)
Removes all objects found within collection c from this collection.

Specified by:
removeAll in interface java.util.Collection<E>
Returns:
true if every element of c could be removed from this.
Precondition:
c is a valid collection
Postcondition:
a copy of each element c is removed from this, if it exists

retainAll

public boolean retainAll(java.util.Collection<?> c)
Retains elements of this Collection that are also in c.

Specified by:
retainAll in interface java.util.Collection<E>
Returns:
true iff this Collection was changed by this operation
Precondition:
c is a valid Collection
Postcondition:
this collection retains only those elements found within both this and c.

size

public int size()
Determine the number of elements in this collection.

Specified by:
size in interface java.util.Collection<E>
Returns:
the number of elements in this collection.
Postcondition:
returns the number of elements in this collection

toArray

public java.lang.Object[] toArray()
Construct an array of values found in the subordinate Structure.

Specified by:
toArray in interface java.util.Collection<E>
Returns:
array containing exactly the elements of this structure
Postcondition:
returns an array of objects containing the values of structure

toArray

public <T> T[] toArray(T[] target)
Copy elements of this structure into target array.

Specified by:
toArray in interface java.util.Collection<E>
Returns:
the array of values
Precondition:
target is valid
Postcondition:
values are copied to target; target is enlarged if necessary to contain the values of this collection; ultimate target is returned.

toString

public java.lang.String toString()
Construct a string representation of this class.

Overrides:
toString in class java.lang.Object
Returns:
string representing this class
Precondition:
base class can stringify itself
Postcondition:
string representation of this class is returned