com.nettgryppa.util
Class InfiniteList<E>

java.lang.Object
  extended by com.nettgryppa.util.InfiniteList<E>
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>

public class InfiniteList<E>
extends Object
implements List<E>

This class is meant to simulate a list of infinite (and optionally bi-infinite) length. All positive indices are valid but will default to null. This list can also be bi-infinite, in which case all negative indices are also valid. This class is also useful for sparse lists as it is backed by a SortedMap Copyright 2006 Gregory Rubin grrubin@gmail.com
Permission is given to use, modify, and or distribute this code so long as this message remains attached

Version:
1.0
Author:
grrubin@gmail.com

Constructor Summary
InfiniteList()
          Constructs an empty list with allowNegative set to false.
InfiniteList(boolean allowNegative)
          Constructs an empty list.
InfiniteList(InfiniteList<E> obj)
          Copy constructor
 
Method Summary
 boolean add(E element)
           
 void add(int index, E element)
           
 boolean addAll(Collection<? extends E> c)
           
 boolean addAll(int index, Collection<? extends E> c)
           
 boolean allowsNegative()
          Does this specific instance allow negative indices
 void clear()
           
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> o)
           
 boolean equals(Object o)
          Acts as expected if other object is an InfiniteList.
 E get(int index)
           
 int hashCode()
           
 int indexOf(Object o)
          WARNING! DO NOT USE THIS TO DETERMINE IF THE LIST CONTAINS AN ELEMENT AS -1 IS A VALID RESPONSE! USE contains(Object o) If o is null then returns min() - 1
 Set<Integer> indexSet()
          All indices with non-null values in ascending order
 boolean isEmpty()
           
 Iterator<E> iterator()
           
 int lastIndexOf(Object o)
          WARNING! DO NOT USE THIS TO DETERMINE IF THE LIST CONTAINS AN ELEMENT AS -1 IS A VALID RESPONSE! USE contains(Object o) WARNING! THIS IS A SLOW IMPLEMENTATION If o is null then returns max() + 1
 ListIterator<E> listIterator()
           
 ListIterator<E> listIterator(int index)
           
 int load()
           
 int max()
          Maximum assigned index
 int min()
          Minimum assigned index
 E remove(int index)
           
 boolean remove(Object o)
           
 boolean removeAll(Collection<?> c)
           
 boolean retainAll(Collection<?> c)
           
 E set(int index, E element)
           
 int size()
           
 InfiniteList<E> subList(int fromIndex, int toIndex)
           
 Object[] toArray()
          NOTE: While order is guaranteed, indices are not guarenteed to be identical unless all are non-negative
<T> T[]
toArray(T[] a)
          NOTE: While order is guaranteed, indices are not guarenteed to be identical unless all are non-negative
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InfiniteList

public InfiniteList()
Constructs an empty list with allowNegative set to false.


InfiniteList

public InfiniteList(boolean allowNegative)
Constructs an empty list.


InfiniteList

public InfiniteList(InfiniteList<E> obj)
Copy constructor

Method Detail

allowsNegative

public boolean allowsNegative()
Does this specific instance allow negative indices


size

public int size()
Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>

max

public int max()
Maximum assigned index


min

public int min()
Minimum assigned index


load

public int load()

add

public boolean add(E element)
Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>

add

public void add(int index,
                E element)
Specified by:
add in interface List<E>

addAll

public boolean addAll(Collection<? extends E> c)
Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface List<E>

addAll

public boolean addAll(int index,
                      Collection<? extends E> c)
Specified by:
addAll in interface List<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>

containsAll

public boolean containsAll(Collection<?> o)
Specified by:
containsAll in interface Collection<E>
Specified by:
containsAll in interface List<E>

indexSet

public Set<Integer> indexSet()
All indices with non-null values in ascending order


equals

public boolean equals(Object o)
Acts as expected if other object is an InfiniteList. Else returns true if this list has no negative elements and its positive elements are equal to the list passed in. NOTE: If the other object is not an InfiniteList, we cannot guarantee that infiniteList.equals(o) == o.equals(infiniteList) An example of where this will break is any list that contains nulls on either end.

Specified by:
equals in interface Collection<E>
Specified by:
equals in interface List<E>
Overrides:
equals in class Object

get

public E get(int index)
Specified by:
get in interface List<E>

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<E>
Specified by:
hashCode in interface List<E>
Overrides:
hashCode in class Object

indexOf

public int indexOf(Object o)
WARNING! DO NOT USE THIS TO DETERMINE IF THE LIST CONTAINS AN ELEMENT AS -1 IS A VALID RESPONSE! USE contains(Object o) If o is null then returns min() - 1

Specified by:
indexOf in interface List<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>

lastIndexOf

public int lastIndexOf(Object o)
WARNING! DO NOT USE THIS TO DETERMINE IF THE LIST CONTAINS AN ELEMENT AS -1 IS A VALID RESPONSE! USE contains(Object o) WARNING! THIS IS A SLOW IMPLEMENTATION If o is null then returns max() + 1

Specified by:
lastIndexOf in interface List<E>

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface List<E>

listIterator

public ListIterator<E> listIterator()
Specified by:
listIterator in interface List<E>

listIterator

public ListIterator<E> listIterator(int index)
Specified by:
listIterator in interface List<E>

remove

public E remove(int index)
Specified by:
remove in interface List<E>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<E>
Specified by:
remove in interface List<E>

set

public E set(int index,
             E element)
Specified by:
set in interface List<E>

subList

public InfiniteList<E> subList(int fromIndex,
                               int toIndex)
Specified by:
subList in interface List<E>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<E>
Specified by:
retainAll in interface List<E>

removeAll

public boolean removeAll(Collection<?> c)
Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface List<E>

toArray

public Object[] toArray()
NOTE: While order is guaranteed, indices are not guarenteed to be identical unless all are non-negative

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>

toArray

public <T> T[] toArray(T[] a)
NOTE: While order is guaranteed, indices are not guarenteed to be identical unless all are non-negative

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>