de.grogra.xl.util
Class BooleanList

java.lang.Object
  extended by de.grogra.xl.util.BooleanList
All Implemented Interfaces:
BooleanConsumer, VoidToBooleanGenerator, java.io.Serializable, java.lang.Cloneable

public class BooleanList
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable, BooleanConsumer, VoidToBooleanGenerator

A BooleanList represents a list of boolean values. It provides list- and stack-oriented methods for insertion, addition, and removal, of values. The methods are not thread-safe.

Author:
Ole Kniemeyer
See Also:
Serialized Form

Field Summary
 boolean[] elements
          The array holding the elements.
 int size
          The size of this list.
 
Constructor Summary
BooleanList()
          Constructs a new BooleanList.
BooleanList(boolean[] elements)
          Constructs a new BooleanList whose elements are a copy of elements.
BooleanList(int capacity)
          Constructs a new BooleanList with a given initial capacity.
 
Method Summary
 boolean add(boolean o)
          Adds o as last element to this list.
 void add(int index, boolean o)
          Inserts o at position index to this list.
 BooleanList addAll(boolean[] v, int begin, int length)
          Appends length components of v to this list, starting at index begin.
 BooleanList addAll(BooleanList v)
          Appends all elements of v to this list.
 void addIfNotContained(boolean o)
          Adds o as last element to this list if is not yet contained as reported by contains(boolean).
static void arraycopy(boolean[] src, int srcIndex, boolean[] dest, int destIndex, int length)
           
 void clear()
          Removes all of the elements from this list.
static void clear(boolean[] array, int index, int length)
           
 java.lang.Object clone()
           
 void consume(boolean value)
          Receives a value of type boolean.
 boolean contains(boolean o)
          Returns true iff this list contains the given element o.
 void ensureCapacity(int capacity)
          Ensures a capacity of the internal array of at least capacity.
 boolean equals(java.lang.Object o)
           
 void evaluateBoolean(BooleanConsumer cons)
          This generator method yields all values to cons.
 boolean get(int index)
          Returns the list element at index.
 int hashCode()
           
 int indexOf(boolean o)
          Returns the index of o in this list.
 boolean isEmpty()
          Returns if this list is empty, i.e., if its size is zero.
 int lastIndexOf(boolean o)
          Returns the last index of o in this list.
 boolean peek(int index)
          Returns the list element at index as seen from the top, i.e., at absolute position size - index.
 boolean pop()
          Removes and returns the object at the top of this list.
 BooleanList push(boolean o)
          Pushes o on top of this list, i.e., as last element.
 BooleanList push(boolean o1, boolean o2)
          Pushes o1 and o2 on top of this list, i.e., as last elements.
 BooleanList push(boolean o1, boolean o2, boolean o3)
          Pushes o1 ... o3 on top of this list, i.e., as last elements.
 boolean remove(boolean o)
          Removes the element o.
 boolean removeAt(int index)
          Removes the element at position index.
 boolean set(int index, boolean o)
          Sets the element at position index to o.
 void setSize(int size)
          Sets the size of this list to the given value.
 int size()
          Returns the size of this list.
 boolean[] toArray()
          Returns an array containing the elements of this list.
 boolean[] toArray(boolean[] array)
          Returns an array containing the elements of this list.
 java.lang.String toString()
           
 void trimToSize()
          Trims the capacity of this list to be its current size.
 void values(BooleanConsumer cons)
          This method is an alias for evaluateBoolean(de.grogra.xl.lang.BooleanConsumer).
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

elements

public transient boolean[] elements
The array holding the elements. Only the components from 0 to size - 1 are valid. Direct operation on this array has to be designed carefully to avoid inconsistencies.


size

public int size
The size of this list. Direct operation on this field has to be designed carefully to avoid inconsistencies.

Constructor Detail

BooleanList

public BooleanList()
Constructs a new BooleanList.


BooleanList

public BooleanList(boolean[] elements)
Constructs a new BooleanList whose elements are a copy of elements.

Parameters:
elements - the initial elements of the list

BooleanList

public BooleanList(int capacity)
Constructs a new BooleanList with a given initial capacity.

Parameters:
capacity - the initial capacity
Method Detail

add

public boolean add(boolean o)
Adds o as last element to this list.

Parameters:
o - the value to add
Returns:
true

add

public void add(int index,
                boolean o)
Inserts o at position index to this list. If index is not less than size, the list is enlarged and filled with false-values before.

Parameters:
index - the insert position
o - the value to insert

addAll

public BooleanList addAll(boolean[] v,
                          int begin,
                          int length)
Appends length components of v to this list, starting at index begin.

Parameters:
v - the array of elements to add
begin - the array index to begin with
length - the number of elements to add
Returns:
this list

addAll

public BooleanList addAll(BooleanList v)
Appends all elements of v to this list.

Parameters:
v - the list of elements to add
Returns:
this list

addIfNotContained

public void addIfNotContained(boolean o)
Adds o as last element to this list if is not yet contained as reported by contains(boolean).

Parameters:
o - the value to add

arraycopy

public static void arraycopy(boolean[] src,
                             int srcIndex,
                             boolean[] dest,
                             int destIndex,
                             int length)

clear

public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.


clear

public static void clear(boolean[] array,
                         int index,
                         int length)

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

consume

public void consume(boolean value)
Description copied from interface: BooleanConsumer
Receives a value of type boolean.

Specified by:
consume in interface BooleanConsumer

contains

public boolean contains(boolean o)
Returns true iff this list contains the given element o.

Parameters:
o - a value
Returns:
true iff o is contained

ensureCapacity

public void ensureCapacity(int capacity)
Ensures a capacity of the internal array of at least capacity.

Parameters:
capacity - the desired minimum capacity

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

evaluateBoolean

public void evaluateBoolean(BooleanConsumer cons)
This generator method yields all values to cons.

Specified by:
evaluateBoolean in interface VoidToBooleanGenerator
Parameters:
cons - the consumer which receives the values

get

public boolean get(int index)
Returns the list element at index. If index is not less than size, false is returned.

Parameters:
index - the position
Returns:
the value at index

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

indexOf

public int indexOf(boolean o)
Returns the index of o in this list.

Parameters:
o - a value
Returns:
the index of o, or -1 of o is not contained

isEmpty

public final boolean isEmpty()
Returns if this list is empty, i.e., if its size is zero.

Returns:
true iff this list is empty

lastIndexOf

public int lastIndexOf(boolean o)
Returns the last index of o in this list.

Parameters:
o - a value
Returns:
the index of o, or -1 of o is not contained

peek

public boolean peek(int index)
Returns the list element at index as seen from the top, i.e., at absolute position size - index. Thus, the topmost element has index 1.

Parameters:
index - the position as seen from the top
Returns:
the value at that position

pop

public boolean pop()
Removes and returns the object at the top of this list.

Returns:
the removed object from the top of this list

push

public final BooleanList push(boolean o)
Pushes o on top of this list, i.e., as last element.

Parameters:
o - the value to push
Returns:
this list

push

public final BooleanList push(boolean o1,
                              boolean o2)
Pushes o1 and o2 on top of this list, i.e., as last elements. The effect is the same as the invocation list.push(o1).push(o2).

Parameters:
o1 - the first value to push
o2 - the second value to push
Returns:
this list

push

public final BooleanList push(boolean o1,
                              boolean o2,
                              boolean o3)
Pushes o1 ... o3 on top of this list, i.e., as last elements. The effect is the same as the invocation list.push(o1).push(o2).push(o3).

Parameters:
o1 - the first value to push
o2 - the second value to push
o3 - the third value to push
Returns:
this list

remove

public boolean remove(boolean o)
Removes the element o. The last occurence of o in this list is removed.

Parameters:
o - the element to be removed
Returns:
true iff o was found and removed from the list

removeAt

public boolean removeAt(int index)
Removes the element at position index.

Parameters:
index - the position of the element to be removed
Returns:
the value of the removed element

set

public boolean set(int index,
                   boolean o)
Sets the element at position index to o. If index is not less than size, the list is enlarged and filled with false-values before.

Parameters:
index - the position
o - the new value
Returns:
the old value at index

setSize

public void setSize(int size)
Sets the size of this list to the given value. If the new size is greater than the old size, the new elements are initialized with false-values.

Parameters:
size - the new size

size

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

Returns:
the size

toArray

public boolean[] toArray()
Returns an array containing the elements of this list.

Returns:
an array copy of this list

toArray

public boolean[] toArray(boolean[] array)
Returns an array containing the elements of this list. The type of the returned array is that of the specified array. If this list fits in the specified array, it is returned therein. Otherwise, a new array is allocated whose length is the size of this list's size, the values of this list are copied into the new array, and this array is returned.

If there is room for an additional element in the array, a false-value is written behind the last copied element.

Parameters:
array - an array to use
Returns:
an array copy of this list

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

trimToSize

public void trimToSize()
Trims the capacity of this list to be its current size.


values

public void values(BooleanConsumer cons)
This method is an alias for evaluateBoolean(de.grogra.xl.lang.BooleanConsumer).