net.sf.molae.pipe.hash
Class HashList<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by net.sf.molae.pipe.hash.HashList<E>
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>

public class HashList<E>
extends AbstractList<E>

A list where the index of each element equals its hash code.


Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
HashList(List<E> base)
          Constructs a HashList object.
 
Method Summary
 boolean add(E o)
          Appends the specified element to the end of this List (optional operation).
 void clear()
          Removes all of the elements from this collection (optional operation).
 boolean contains(Object o)
          Returns true if this collection contains the specified element.
 E get(int index)
          Returns the element at the specified position in this list.
 int indexOf(Object o)
          Returns the index in this list of the first occurence of the specified element, or -1 if the list does not contain this element.
 boolean isEmpty()
          Returns true if this collection contains no elements.
 int lastIndexOf(Object o)
          Returns the index in this list of the last occurence of the specified element, or -1 if the list does not contain this element.
 E set(int index, E element)
          Replaces the element at the specified position in this list with the specified element (optional operation).
 int size()
          Returns the number of elements in this collection.
 
Methods inherited from class java.util.AbstractList
add, addAll, equals, hashCode, iterator, listIterator, listIterator, remove, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, containsAll, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

HashList

public HashList(List<E> base)
Constructs a HashList object.

Parameters:
base - the underling lists where the elements are stored.
Method Detail

size

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

Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the number of elements in this collection.

isEmpty

public boolean isEmpty()
Returns true if this collection contains no elements.

Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>
Overrides:
isEmpty in class AbstractCollection<E>
Returns:
true if this collection contains no elements.

contains

public boolean contains(Object o)
Returns true if this collection contains the specified element. This implementation checks if the specified element equals the element at the position with the specified element's hash code.

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
o - object to be checked for containment in this collection.
Returns:
true if this collection contains the specified element.

add

public boolean add(E o)
Appends the specified element to the end of this List (optional operation).

This implementation calls the corresponding method of the base list if the hash code of the specified element equals the size of the list.

Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>
Overrides:
add in class AbstractList<E>
Parameters:
o - element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).
Throws:
UnsupportedOperationException - if the add method is not supported by the base list.
ClassCastException - if the class of the specified element prevents it from being added to the base list.
IllegalArgumentException - some aspect of this element prevents it from being added to the base list or if the hash code of the specified element is not equal to the size of this list.

clear

public void clear()
Removes all of the elements from this collection (optional operation).

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>
Overrides:
clear in class AbstractList<E>
Throws:
UnsupportedOperationException - if the clear method is not supported by the base list

get

public E get(int index)
Returns the element at the specified position in this list.

Specified by:
get in interface List<E>
Specified by:
get in class AbstractList<E>
Parameters:
index - index of element to return.
Returns:
the element at the specified position in this list.
Throws:
IndexOutOfBoundsException - if the given index is out of range (index < 0 || index >= size()).

set

public E set(int index,
             E element)
Replaces the element at the specified position in this list with the specified element (optional operation).

This implementation checks the hash code of the new element first.

Specified by:
set in interface List<E>
Overrides:
set in class AbstractList<E>
Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
UnsupportedOperationException - if the set method is not supported by the base list.
ClassCastException - if the class of the specified element prevents it from being added to the base list.
IllegalArgumentException - if some aspect of the specified element prevents it from being added to this list.
IndexOutOfBoundsException - if the specified index is out of range (index < 0 || index >= size()).

indexOf

public int indexOf(Object o)
Returns the index in this list of the first occurence of the specified element, or -1 if the list does not contain this element. This implementation returns the hash code of the element if it is contained in the list.

Specified by:
indexOf in interface List<E>
Overrides:
indexOf in class AbstractList<E>
Parameters:
o - element to search for.
Returns:
the index in this List of the first occurence of the specified element, or -1 if the List does not contain this element.

lastIndexOf

public int lastIndexOf(Object o)
Returns the index in this list of the last occurence of the specified element, or -1 if the list does not contain this element. This implementation calls indexOf.

Specified by:
lastIndexOf in interface List<E>
Overrides:
lastIndexOf in class AbstractList<E>
Parameters:
o - element to search for.
Returns:
the index in this list of the last occurence of the specified element, or -1 if the list does not contain this element.