edu.mit.csail.sdg.alloy4
Class SafeList<T>

java.lang.Object
  extended by edu.mit.csail.sdg.alloy4.SafeList<T>
Type Parameters:
T - - the type of element
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<T>

public final class SafeList<T>
extends java.lang.Object
implements java.io.Serializable, java.lang.Iterable<T>

This list allows add() but disallows remove() and set(); null values are allowed.

By making this sacrifice, we are able to provide a very cheap "duplicate method" that simulates making a copy without actually making a copy.

Furthermore, this class's iterator allows concurrent insertion and iteration (that is, we can iterate over the list while adding elements to the list at the same time). The iterator is guaranteed to iterate over exactly the elements that existed at the time that the iterator was created.

Thread Safety: Safe.

See Also:
Serialized Form

Constructor Summary
SafeList()
          Constructs a modifiable empty list.
SafeList(java.util.Collection<? extends T> initialValue)
          Constructs a modifiable list containing the elements from the given collection.
SafeList(int initialCapacity)
          Constructs a modifiable empty list with the initial capacity.
SafeList(java.lang.Iterable<? extends T> initialValue)
          Constructs a modifiable list containing the elements from the given iterable.
 
Method Summary
 boolean add(T item)
          Add an element into the list.
 void addAll(java.util.Collection<? extends T> items)
          Add a collection of elements into the list.
 boolean contains(java.lang.Object item)
          Returns true if the list contains the given element.
 SafeList<T> dup()
          Constructs an unmodifiable copy of an existing SafeList.
 boolean equals(java.lang.Object that)
          Returns true if (that instanceof List or that instanceof SafeList), and that contains the same elements as this list.
 T get(int i)
          Get an element from the list.
 int hashCode()
          Computes a hash code that is consistent with SafeList's equals() and java.util.List's hashCode() methods.
 boolean isEmpty()
          Returns true if the list is empty.
 java.util.Iterator<T> iterator()
          Returns an iterator that iterates over elements in this list (in the order that they were inserted).
 ConstList<T> makeConstList()
          Constructs an unmodifiable ConstList containing the same elements as this list.
 java.util.List<T> makeCopy()
          Constructs a modifiable ArrayList containing the same elements as this list.
 int size()
          Returns the size of the list.
 java.lang.String toString()
          Returns a String representation of this list.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SafeList

public SafeList()
Constructs a modifiable empty list.


SafeList

public SafeList(int initialCapacity)
Constructs a modifiable empty list with the initial capacity.


SafeList

public SafeList(java.util.Collection<? extends T> initialValue)
Constructs a modifiable list containing the elements from the given collection.


SafeList

public SafeList(java.lang.Iterable<? extends T> initialValue)
Constructs a modifiable list containing the elements from the given iterable.

Method Detail

dup

public SafeList<T> dup()
Constructs an unmodifiable copy of an existing SafeList.


makeCopy

public java.util.List<T> makeCopy()
Constructs a modifiable ArrayList containing the same elements as this list.


makeConstList

public ConstList<T> makeConstList()
Constructs an unmodifiable ConstList containing the same elements as this list.


hashCode

public int hashCode()
Computes a hash code that is consistent with SafeList's equals() and java.util.List's hashCode() methods.

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object that)
Returns true if (that instanceof List or that instanceof SafeList), and that contains the same elements as this list.

Overrides:
equals in class java.lang.Object

contains

public boolean contains(java.lang.Object item)
Returns true if the list contains the given element.


add

public boolean add(T item)
Add an element into the list.


addAll

public void addAll(java.util.Collection<? extends T> items)
Add a collection of elements into the list.


get

public T get(int i)
Get an element from the list.


size

public int size()
Returns the size of the list.


isEmpty

public boolean isEmpty()
Returns true if the list is empty.


iterator

public java.util.Iterator<T> iterator()
Returns an iterator that iterates over elements in this list (in the order that they were inserted).

Note: This iterator's remove() method always throws UnsupportedOperationException.

Note: This iterator always returns exactly the list of elements that existed at the time that the iterator was created (even if the list is modified after that point).

Specified by:
iterator in interface java.lang.Iterable<T>

toString

public java.lang.String toString()
Returns a String representation of this list.

Overrides:
toString in class java.lang.Object