org.pf.util
Class OrderedCollection

java.lang.Object
  extended by org.pf.util.OrderedCollection
All Implemented Interfaces:
java.lang.Iterable, java.util.Collection

public class OrderedCollection
extends java.lang.Object
implements java.util.Collection

A collection that supports similar methods to the list interface but it starts with index 1 and ends with element index size() !
IT IS NOT ZERO BASED !

Version:
1.1
Author:
Manfred Duchrow

Constructor Summary
OrderedCollection()
          Initialize the new instance with default values.
OrderedCollection(int initialCapacity)
          Initialize the new instance with a given initial capacity.
OrderedCollection(java.util.List list)
          Initialize the new instance with default values.
 
Method Summary
 void add(int index, java.lang.Object element)
          Inserts the specified element at the specified position in this list.
 boolean add(java.lang.Object o)
          Appends the specified element to the end of this list.
 boolean addAll(java.util.Collection c)
          Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
 boolean addAll(int index, java.util.Collection c)
          Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
 java.lang.Object at(int index)
          Returns the element at the specified position in this list.
 java.lang.Object atPut(int index, java.lang.Object element)
          Replaces the element at the specified position in this list with the specified element.
 void clear()
          Removes all of the elements from this list.
 boolean contains(java.lang.Object o)
          Returns true if this list contains the specified element.
 boolean containsAll(java.util.Collection c)
          Returns true if this list contains all of the elements of the specified collection.
 java.lang.Object detect_(Block1 aBlock)
          Loop over all the receiver's elements and call the given blocks eval() - method for each.
 java.lang.Object do_(Block1 aBlock)
          Loop over all the receiver's elements and call the given blocks eval() - method for each.
 boolean equals(java.lang.Object o)
          Compares the specified object with this list for equality.
 java.lang.Object first()
          Returns the first element of the receiver.
 java.lang.Object get(int index)
          Returns the element at the specified position in this list.
 int hashCode()
          Returns the hash code value for this list.
 int indexOf(java.lang.Object o)
          Returns the index in this list of the first occurrence of the specified element, or 0 if this list does not contain this element.
 boolean isEmpty()
          Returns true if this list contains no elements.
 java.util.Iterator iterator()
          Returns an iterator over the elements in this list in proper sequence.
 java.lang.Object last()
          Returns the last element of the receiver.
 int lastIndexOf(java.lang.Object o)
          Returns the index in this list of the last occurrence of the specified element, or 0 if this list does not contain this element.
 java.util.ListIterator listIterator()
          Returns a list iterator of the elements in this list (in proper sequence).
 java.util.ListIterator listIterator(int index)
          Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
 java.lang.Object remove(int index)
          Removes the element at the specified position in this list.
 boolean remove(java.lang.Object o)
          Removes the first occurrence in this list of the specified element (optional operation).
 boolean removeAll(java.util.Collection c)
          Removes from this list all the elements that are contained in the specified collection.
 boolean retainAll(java.util.Collection c)
          Retains only the elements in this list that are contained in the specified collection.
 java.lang.Object set(int index, java.lang.Object element)
          Replaces the element at the specified position in this list with the specified element.
 int size()
          Returns the number of elements in this list.
 java.util.List subList(int fromIndex, int toIndex)
          Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, inclusive.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this list in proper sequence.
 java.lang.Object[] toArray(java.lang.Object[] a)
          Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
 java.util.Vector toVector()
          Returns a copy of this collection in a Vector
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OrderedCollection

public OrderedCollection()
Initialize the new instance with default values.


OrderedCollection

public OrderedCollection(int initialCapacity)
Initialize the new instance with a given initial capacity.


OrderedCollection

public OrderedCollection(java.util.List list)
Initialize the new instance with default values.

Method Detail

size

public int size()
Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface java.util.Collection
Returns:
the number of elements in this list.

isEmpty

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

Specified by:
isEmpty in interface java.util.Collection
Returns:
true if this list contains no elements.

contains

public boolean contains(java.lang.Object o)
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Specified by:
contains in interface java.util.Collection
Parameters:
o - - element whose presence in this list is to be tested.

iterator

public java.util.Iterator iterator()
Returns an iterator over the elements in this list in proper sequence.

Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Returns:
an iterator over the elements in this list in proper sequence.

toArray

public java.lang.Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence. Obeys the general contract of the Collection.toArray method.

Specified by:
toArray in interface java.util.Collection

toArray

public java.lang.Object[] toArray(java.lang.Object[] a)
                           throws java.lang.ArrayStoreException
Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the Collection.toArray(Object[]) method.

Specified by:
toArray in interface java.util.Collection
Parameters:
a - - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Throws:
java.lang.ArrayStoreException - - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.

add

public boolean add(java.lang.Object o)
Appends the specified element to the end of this list.

Specified by:
add in interface java.util.Collection
Parameters:
o - - element to be appended to this list.
Returns:
true (as per the general contract of the Collection.add method).

remove

public boolean remove(java.lang.Object o)
Removes the first occurrence in this list of the specified element (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Specified by:
remove in interface java.util.Collection
Parameters:
o - - element to be removed from this list, if present.
Returns:
true if this list contained the specified element.

containsAll

public boolean containsAll(java.util.Collection c)
Returns true if this list contains all of the elements of the specified collection.

Specified by:
containsAll in interface java.util.Collection
Parameters:
c - - collection to be checked for containment in this list.
Returns:
true if this list contains all of the elements of the specified collection.

addAll

public boolean addAll(java.util.Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Specified by:
addAll in interface java.util.Collection
Parameters:
c - - collection whose elements are to be added to this list.
Returns:
true if this list changed as a result of the call.

addAll

public boolean addAll(int index,
                      java.util.Collection c)
               throws java.lang.IndexOutOfBoundsException
Inserts all of the elements in the specified collection into this list at the specified position (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters:
index - - index at which to insert first element from the specified collection. ( 1 <= index <= size() )
c - - elements to be inserted into this list.
Throws:
java.lang.IndexOutOfBoundsException - - if the index is out of range (index < 1 || index > size()).

removeAll

public boolean removeAll(java.util.Collection c)
Removes from this list all the elements that are contained in the specified collection.

Specified by:
removeAll in interface java.util.Collection
Parameters:
c - - collection that defines which elements will be removed from this list.
Returns:
true if this list changed as a result of the call.

retainAll

public boolean retainAll(java.util.Collection c)
Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all the elements that are not contained in the specified collection.

Specified by:
retainAll in interface java.util.Collection
Parameters:
c - - collection that defines which elements this set will retain.
Returns:
true if this list changed as a result of the call.

clear

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

Specified by:
clear in interface java.util.Collection

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Specified by:
equals in interface java.util.Collection
Overrides:
equals in class java.lang.Object
Parameters:
o - - the object to be compared for equality with this list.
Returns:
true if the specified object is equal to this list.

hashCode

public int hashCode()
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation: hashCode = 1;
Iterator i = list.iterator();
while (i.hasNext())
{
Object obj = i.next();
hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
} This ensures that list1.equals(list2) implies that list1.hashCode()==list2.hashCode() for any two lists, list1 and list2, as required by the general contract of Object.hashCode.

Specified by:
hashCode in interface java.util.Collection
Overrides:
hashCode in class java.lang.Object
Returns:
the hash code value for this list.

get

public java.lang.Object get(int index)
                     throws java.lang.IndexOutOfBoundsException
Returns the element at the specified position in this list.

Parameters:
index - - index of element to return. ( 1 <= index <= size() )
Returns:
the element at the specified position in this list.
Throws:
java.lang.IndexOutOfBoundsException - - if the index is out of range (index < 1 || index > size()).

set

public java.lang.Object set(int index,
                            java.lang.Object element)
                     throws java.lang.IndexOutOfBoundsException
Replaces the element at the specified position in this list with the specified element.

Parameters:
index - - index of element to replace. ( 1 <= index <= size() )
element - - element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
java.lang.IndexOutOfBoundsException - - if the index is out of range (index < 1 || index > size()).

add

public void add(int index,
                java.lang.Object element)
         throws java.lang.IndexOutOfBoundsException
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index - - index at which the specified element is to be inserted.
element - - element to be inserted.
Throws:
java.lang.IndexOutOfBoundsException - - if the index is out of range (index < 1 || index > size()).

remove

public java.lang.Object remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Parameters:
index - - the index of the element to removed.
Throws:
java.lang.IndexOutOfBoundsException - - if the index is out of range (index < 1 || index > size()).

indexOf

public int indexOf(java.lang.Object o)
Returns the index in this list of the first occurrence of the specified element, or 0 if this list does not contain this element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or 0 if there is no such index.

Parameters:
o - - element to search for.
Returns:
the index in this list of the first occurrence of the specified element, or 0 if this list does not contain this element.

lastIndexOf

public int lastIndexOf(java.lang.Object o)
Returns the index in this list of the last occurrence of the specified element, or 0 if this list does not contain this element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or 0 if there is no such index.

Parameters:
o - - element to search for.
Returns:
the index in this list of the last occurrence of the specified element, or 0 if this list does not contain this element.

listIterator

public java.util.ListIterator listIterator()
Returns a list iterator of the elements in this list (in proper sequence).


listIterator

public java.util.ListIterator listIterator(int index)
                                    throws java.lang.IndexOutOfBoundsException
Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. The specified index indicates the first element that would be returned by an initial call to the nextElement method. An initial call to the previousElement method would return the element with the specified index minus one.

Parameters:
index - - index of first element to be returned from the list iterator (by a call to the next method).
Returns:
a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
Throws:
java.lang.IndexOutOfBoundsException - - if the index is out of range (index < 1 || index > size()).

subList

public java.util.List subList(int fromIndex,
                              int toIndex)
                       throws java.lang.IndexOutOfBoundsException
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, inclusive. (If fromIndex and toIndex are equal, the returned list has one element.) The returned list is a copy of this list, so changes in the returned list are not reflected in this list, and vice-versa. This method eliminates the need for explicit range operations

Parameters:
fromIndex - - low endpoint (inclusive) of the subList.
toIndex - - high endpoint (inclusive) of the subList.
Returns:
a view of the specified range within this list.
Throws:
java.lang.IndexOutOfBoundsException - - for an illegal endpoint index value (fromIndex < 1 || toIndex > size() || fromIndex > toIndex).

at

public java.lang.Object at(int index)
                    throws java.lang.IndexOutOfBoundsException
Returns the element at the specified position in this list.

Parameters:
index - - index of element to return. ( 1 <= index <= size() )
Returns:
the element at the specified position in this list.
Throws:
java.lang.IndexOutOfBoundsException - - if the index is out of range (index < 1 || index > size()).

atPut

public java.lang.Object atPut(int index,
                              java.lang.Object element)
                       throws java.lang.IndexOutOfBoundsException
Replaces the element at the specified position in this list with the specified element.

Parameters:
index - - index of element to replace. ( 1 <= index <= size() )
element - - element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
java.lang.IndexOutOfBoundsException - - if the index is out of range (index < 1 || index > size()).

toVector

public java.util.Vector toVector()
Returns a copy of this collection in a Vector

See Also:
Vector

do_

public java.lang.Object do_(Block1 aBlock)
Loop over all the receiver's elements and call the given blocks eval() - method for each.

Parameters:
aBlock - The block to evaluate for each element.
Returns:
The result of the last block evaluation.

detect_

public java.lang.Object detect_(Block1 aBlock)
Loop over all the receiver's elements and call the given blocks eval() - method for each. Returns that element that first evaluates the block to Boolean.TRUE.

Parameters:
aBlock - The block to evaluate for each element.
Returns:
The result of the last block evaluation.

first

public java.lang.Object first()
Returns the first element of the receiver.
If the collection is empty the result is null.


last

public java.lang.Object last()
Returns the last element of the receiver.
If the collection is empty the result is null.