pt.ua.base
Class ImmutableList<T>

java.lang.Object
  extended by pt.ua.base.ImmutableList<T>

public class ImmutableList<T>
extends java.lang.Object

Generic immutable list module.

This class follows DbC(tm) methodology. Where possible, contracts are implement with JML and native's Java assert.

Author:
Miguel Oliveira e Silva (mos@ua.pt)

Field Summary
 T head
          The list head element
 int size
          The list total number of elements.
 ImmutableList<T> tail
          The list tail list
 
Constructor Summary
ImmutableList()
          Creates an empty list.
ImmutableList(T head, ImmutableList<T> tail)
          Creates a new list by appending tail list to head element.
 
Method Summary
 boolean equals(ImmutableList<T> other)
          Queries equality with other list.
 T itemAt(int idx)
          Returns the element of the list at the idx position.
 java.lang.String toString()
          Returns a String with all list's elements separated with a space.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

size

public final int size
The list total number of elements.


head

public final T head
The list head element


tail

public final ImmutableList<T> tail
The list tail list

Constructor Detail

ImmutableList

public ImmutableList()
Creates an empty list.

ensures: size == 0
ensures: head == null
ensures: tail == null


ImmutableList

public ImmutableList(T head,
                     ImmutableList<T> tail)
Creates a new list by appending tail list to head element.

requires: tail != null

ensures: size == \old(size)+1
ensures: this.head == head
ensures: this.tail == tail

Parameters:
head - the element
tail - an immutable list
Method Detail

equals

public boolean equals(ImmutableList<T> other)
Queries equality with other list.

requires: other != null

Returns:
true if lists are equal, false otherwise

itemAt

public T itemAt(int idx)
Returns the element of the list at the idx position.

requires: idx >= 0 && idx < size

Parameters:
idx - the index
Returns:
the element

toString

public java.lang.String toString()
Returns a String with all list's elements separated with a space.

Overrides:
toString in class java.lang.Object
Returns:
the complete list as a string