Graphics2D Actor API

g2d.jlambda
Class List

java.lang.Object
  extended by java.util.AbstractCollection
      extended by java.util.AbstractList
          extended by g2d.jlambda.List
All Implemented Interfaces:
Iterable, Collection, List
Direct Known Subclasses:
Code

public class List
extends AbstractList

Nearest thing to a Lisp List I have come up with so far. The only anomaly is that the empty list () is actually a cons cell with the empty flag switched on. Square pegs & round holes. This version of List is built upon the AbstractList and is both modifiable and of variable size.

Since:
July 6th, 2004 (converted from AbstractSequentialList December 11th 2005.)
Author:
Ian A. Mason

Field Summary
protected  Object car
          The car of this List, and arbitrary Object.
protected  List cdr
          The cdr, or rest, of this list.
protected  boolean empty
          A flag to distinguish an empty list from an non-empty one.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
List()
          Creates a new empty list.
List(Collection collection)
          Creates a new list from the given Collection.
List(Object[] array)
          Creates a new list from the given object array.
List(Object car, List cdr)
          Adds a new element onto an existing list.
 
Method Summary
 void add(int index, Object element)
          As specified by the AbstractList template.
static List array2List(Object obj)
           
 Object caar()
          The car of the car of this list.
 Object caddr()
          The car of the cdr of the cdr.
 Object cadr()
          The car of the cdr of this list.
 Object car()
          The ubiquitous Lisp/Scheme car accessor.
 Object cdar()
          The cdr of the car of this list.
 List cddr()
          The cdr of the cdr of this list.
 List cdr()
          The ubiquitous Lisp/Scheme cdr accessor.
 Object get(int index)
          As specified by the AbstractList template.
 boolean isEmpty()
          Determines if this list is empty.
 int length()
          The length, or number of elements, in this list.
static List list()
          A static convenience method, that creates new empty lists.
static List list(Object o1)
          A static convenience method, that creates a new singleton list.
static List list(Object o1, Object o2)
          A static convenience method, that creates a new list with two elements.
static List list(Object a, Object b, Object c)
          A static convenience method, that creates a new list with two elements.
static void main(String[] args)
           
 Object nth(int n)
          Returns the nth element of this list, starting from zero.
 Object remove(int index)
          As specified by the AbstractList template.
 Object set(int index, Object element)
          As specified by the AbstractList template.
 int size()
          As specified by the AbstractList template.
protected  List snoc(Iterator iter)
          Adds the contents of the Iterator object destructively onto the end of this list.
protected  List snoc(Object obj)
          Destructively updates this, which must be an empty list, so that it's car in the obj passed in, and it's cdr is a new empty list.
protected  List snoc(Object[] array)
          Adds the contents of the array of Objects destructively onto the end of this list.
 String toString()
          The unprettyprinted String representation of this list.
 
Methods inherited from class java.util.AbstractList
add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray
 

Field Detail

car

protected Object car
The car of this List, and arbitrary Object.


cdr

protected List cdr
The cdr, or rest, of this list.


empty

protected boolean empty
A flag to distinguish an empty list from an non-empty one.

Constructor Detail

List

public List()
Creates a new empty list.


List

public List(Object car,
            List cdr)
Adds a new element onto an existing list. The cdr is not copied, but rather shared.

Parameters:
car - the new first element of the resulting new list.
cdr - the tail of the resulting new list.

List

public List(Collection collection)
Creates a new list from the given Collection.

Parameters:
collection - the collection whose members will make up the new list. It creates an new empty list if the collection is null.

List

public List(Object[] array)
Creates a new list from the given object array.

Parameters:
array - the object array whose members will make up the new list. It creates an new empty list if the array reference is null.
Method Detail

array2List

public static List array2List(Object obj)

size

public int size()
As specified by the AbstractList template. The length, or number of elements, in this list.

Specified by:
size in interface Collection
Specified by:
size in interface List
Specified by:
size in class AbstractCollection
Returns:
the length of the list, zero if the list is empty.

get

public Object get(int index)
As specified by the AbstractList template.

Specified by:
get in interface List
Specified by:
get in class AbstractList

add

public void add(int index,
                Object element)
As specified by the AbstractList template.

Specified by:
add in interface List
Overrides:
add in class AbstractList

remove

public Object remove(int index)
As specified by the AbstractList template.

Specified by:
remove in interface List
Overrides:
remove in class AbstractList

set

public Object set(int index,
                  Object element)
As specified by the AbstractList template.

Specified by:
set in interface List
Overrides:
set in class AbstractList

list

public static List list()
A static convenience method, that creates new empty lists.

Returns:
a new empty list.

list

public static List list(Object o1)
A static convenience method, that creates a new singleton list.

Parameters:
o1 - the single element of the new list.
Returns:
the new singleton list.

list

public static List list(Object o1,
                        Object o2)
A static convenience method, that creates a new list with two elements.

Parameters:
o1 - the first element of the new list.
o2 - the second element of the new list.
Returns:
the new list of length 2.

list

public static List list(Object a,
                        Object b,
                        Object c)
A static convenience method, that creates a new list with two elements.

Parameters:
a - the first element of the new list.
b - the second element of the new list.
c - the thrid element of the new list.
Returns:
the new list of length 3.

car

public final Object car()
The ubiquitous Lisp/Scheme car accessor. If the list in question is empty, then if assertions are enabled using the -ea flag this will generate a failed assertion, otherwise null will be returned.

Returns:
the first element of this list.

cdr

public final List cdr()
The ubiquitous Lisp/Scheme cdr accessor. If the list in question is empty, then if assertions are enabled using the -ea flag this will generate a failed assertion, otherwise null will be returned.

Returns:
the rest or tail of this list.

caar

public final Object caar()
The car of the car of this list. If the first element of the list in question is not a list or is empty, then if assertions are enabled, this will generate a failed assertion, otherwise it will return null if the car is empty, or throw a ClassCastException if it is not a List.

Returns:
the car of the car of this list.
Throws:
ClassCastException - if the car of the list is not a List, and assertions are not enabled.

cadr

public final Object cadr()
The car of the cdr of this list. If the cdr of the list in question is empty, then if assertions are enabled, this will generate a failed assertion, otherwise it will return null.

Returns:
the car of the cdr of this list.

cdar

public final Object cdar()
The cdr of the car of this list. If the first element of the list in question is not a list or is empty, then if assertions are enabled, this will generate a failed assertion, otherwise it will return null if the car is empty, or throw a ClassCastException if it is not a List.

Returns:
the cdr of the car of this list.
Throws:
ClassCastException - if the car of the list is not a List, and assertions are not enabled.

cddr

public final List cddr()
The cdr of the cdr of this list. If the cdr of the list in question is empty, then if assertions are enabled, this will generate a failed assertion, otherwise it will return null.

Returns:
the cdr of the cdr of this list.

caddr

public final Object caddr()
The car of the cdr of the cdr. If either the cdr or the cdr of the cdr is empty then this will either generate a failed assertion (if assertions are enabled) or else return null.

Returns:
the car of cdr of the cdr of this list.

nth

public Object nth(int n)
Returns the nth element of this list, starting from zero. If the list contains less than n + 1 elements this will either generate a failed assertion, if they are enabled, or else throw a NullPointerException.

Parameters:
n - an int specifying the element to return, starting from zero.
Returns:
the nth element of this list, (the car is the zeroth element).
Throws:
NullPointerException - if the list has fewer elements.

isEmpty

public boolean isEmpty()
Determines if this list is empty.

Specified by:
isEmpty in interface Collection
Specified by:
isEmpty in interface List
Overrides:
isEmpty in class AbstractCollection
Returns:
true if this list is empty, false otherwise.

length

public int length()
The length, or number of elements, in this list.

Returns:
the length of the list, zero if the list is empty.

toString

public String toString()
The unprettyprinted String representation of this list.

Overrides:
toString in class AbstractCollection
Returns:
the string representation of this list.

main

public static void main(String[] args)

snoc

protected List snoc(Object obj)
Destructively updates this, which must be an empty list, so that it's car in the obj passed in, and it's cdr is a new empty list. The name snoc is cons backwards, though it is a little different from the original snoc.

Parameters:
obj - the new car.
Returns:
the new empty list, the cdr of this list.

snoc

protected List snoc(Iterator iter)
Adds the contents of the Iterator object destructively onto the end of this list.

Parameters:
iter - an Iterator.
Returns:
the empty list at the end of the modifed list.

snoc

protected List snoc(Object[] array)
Adds the contents of the array of Objects destructively onto the end of this list.

Parameters:
array - an array of objects.
Returns:
the empty list at the end of the modifed list.

Graphics2D Actor API