net.sourceforge.cilib.container
Class SortedList<E extends Comparable<? super E>>

java.lang.Object
  extended by net.sourceforge.cilib.container.SortedList<E>
Type Parameters:
E - The Comparable type.
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, List<E>, Cloneable

public class SortedList<E extends Comparable<? super E>>
extends Object
implements List<E>, Cloneable

A collection that always provides a list of elements that is sorted. The ordering is specified by the provided Comparator instance. If no comparator is provided, the natural ordering of the type E will be used.

Author:
Gary Pampara
See Also:
Serialized Form

Constructor Summary
SortedList()
          Create a new instance of SortedList without a Comparator defined.
SortedList(Comparator<E> comparator)
          Create a new instance of SortedList with the provided Comparator.
SortedList(SortedList<E> copy)
          Create a copy of the provided instance.
 
Method Summary
 boolean add(E e)
          Add the provided element to the list.
 void add(int index, E element)
          Add element to the list at index index.
 boolean addAll(Collection<? extends E> c)
          
 boolean addAll(int index, Collection<? extends E> c)
          Insert the proveded collection to the list at index.
 void clear()
          
 boolean contains(Object o)
          Returns true if this list contains the specified element.
 boolean containsAll(Collection<?> c)
          
 E get(int index)
          
 SortedList<E> getClone()
          Create a cloned copy of the current object and return it.
 Comparator<E> getComparator()
          Get the current Comparator instance.
 int indexOf(Object o)
          
 boolean isEmpty()
          Returns true if this list contains no elements.
 Iterator<E> iterator()
          Returns an iterator over this sequence in proper order.
 int lastIndexOf(Object o)
          
 ListIterator<E> listIterator()
          
 ListIterator<E> listIterator(int index)
          
 E remove(int index)
          
 boolean remove(Object o)
          
 boolean removeAll(Collection<?> c)
          
 boolean retainAll(Collection<?> c)
          
 E set(int index, E element)
          Replace the current element located at index with element.
 void setComparator(Comparator<E> comparator)
          Set the Comparator to use.
 int size()
          Determine the size of the current list.
 List<E> subList(int fromIndex, int toIndex)
          
 Object[] toArray()
          Return a new array containg the elements of this list.
<T> T[]
toArray(T[] a)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

SortedList

public SortedList()
Create a new instance of SortedList without a Comparator defined.


SortedList

public SortedList(Comparator<E> comparator)
Create a new instance of SortedList with the provided Comparator.

Parameters:
comparator - The Comparator to use.

SortedList

public SortedList(SortedList<E> copy)
Create a copy of the provided instance.

Parameters:
copy - The instance to copy.
Method Detail

getClone

public SortedList<E> getClone()
Create a cloned copy of the current object and return it. In general the created copy will be a deep copy of the provided instance. As a result this operation an be quite expensive if used incorrectly.

Specified by:
getClone in interface Cloneable
Returns:
An exact clone of the current object instance.
See Also:
Object.clone()

size

public int size()
Determine the size of the current list.

Specified by:
size in interface Collection<E extends Comparable<? super E>>
Specified by:
size in interface List<E extends Comparable<? super E>>
Returns:
The current size of the list.

isEmpty

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

Specified by:
isEmpty in interface Collection<E extends Comparable<? super E>>
Specified by:
isEmpty in interface List<E extends Comparable<? super E>>
Returns:
true if this list contains no elements.

contains

public boolean contains(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 Collection<E extends Comparable<? super E>>
Specified by:
contains in interface List<E extends Comparable<? super E>>
Parameters:
o - element whose presence is to be tested.
Returns:
true if this list contains the specified element.

iterator

public Iterator<E> iterator()
Returns an iterator over this sequence in proper order.

Specified by:
iterator in interface Iterable<E extends Comparable<? super E>>
Specified by:
iterator in interface Collection<E extends Comparable<? super E>>
Specified by:
iterator in interface List<E extends Comparable<? super E>>
Returns:
an iterator over the elements in this list.

toArray

public Object[] toArray()
Return a new array containg the elements of this list.

Specified by:
toArray in interface Collection<E extends Comparable<? super E>>
Specified by:
toArray in interface List<E extends Comparable<? super E>>
Returns:
a new array containing the list elements.

toArray

public <T> T[] toArray(T[] a)

Specified by:
toArray in interface Collection<E extends Comparable<? super E>>
Specified by:
toArray in interface List<E extends Comparable<? super E>>

add

public boolean add(E e)
Add the provided element to the list. The position of the element is determined by the ordering as defined by the assosicated Comparator. The insertion is determined by first performing a binary search to determine the location of the insert and then, finally adding the element.

Specified by:
add in interface Collection<E extends Comparable<? super E>>
Specified by:
add in interface List<E extends Comparable<? super E>>
Parameters:
e - The object to add to the list.
Returns:
true if the addition was successful.

remove

public boolean remove(Object o)

Specified by:
remove in interface Collection<E extends Comparable<? super E>>
Specified by:
remove in interface List<E extends Comparable<? super E>>

containsAll

public boolean containsAll(Collection<?> c)

Specified by:
containsAll in interface Collection<E extends Comparable<? super E>>
Specified by:
containsAll in interface List<E extends Comparable<? super E>>

addAll

public boolean addAll(Collection<? extends E> c)

Specified by:
addAll in interface Collection<E extends Comparable<? super E>>
Specified by:
addAll in interface List<E extends Comparable<? super E>>

addAll

public boolean addAll(int index,
                      Collection<? extends E> c)
Insert the proveded collection to the list at index. This operation is not guaranteed and to ensure that the list remains sorted, the list is tested after addition and reordered, if needed.

Specified by:
addAll in interface List<E extends Comparable<? super E>>
Parameters:
index - The index to attempt the addition.
c - The collection to add.
Returns:
true if the operation was successful.

removeAll

public boolean removeAll(Collection<?> c)

Specified by:
removeAll in interface Collection<E extends Comparable<? super E>>
Specified by:
removeAll in interface List<E extends Comparable<? super E>>

retainAll

public boolean retainAll(Collection<?> c)

Specified by:
retainAll in interface Collection<E extends Comparable<? super E>>
Specified by:
retainAll in interface List<E extends Comparable<? super E>>

clear

public void clear()

Specified by:
clear in interface Collection<E extends Comparable<? super E>>
Specified by:
clear in interface List<E extends Comparable<? super E>>

get

public E get(int index)

Specified by:
get in interface List<E extends Comparable<? super E>>

set

public E set(int index,
             E element)
Replace the current element located at index with element. Even though the element is replaced, there is no guarantee that the order or location of the new element will be at index. After the replacement the order of the list is shuffled to ensure that the sorted nature of the list is maintained.

Specified by:
set in interface List<E extends Comparable<? super E>>
Parameters:
index - The current location of the element to replace.
element - The element to replace the element at index index.
Returns:
The new element that has been set.

add

public void add(int index,
                E element)
Add element to the list at index index. This action is a best effort. There is no guarantee that the addition of the element at index will perserve the list's ordering. As a result the list order is verified after addition.

Specified by:
add in interface List<E extends Comparable<? super E>>
Parameters:
index - The position in the list to attempt the addition.
element - The element to add.

remove

public E remove(int index)

Specified by:
remove in interface List<E extends Comparable<? super E>>

indexOf

public int indexOf(Object o)

Specified by:
indexOf in interface List<E extends Comparable<? super E>>

lastIndexOf

public int lastIndexOf(Object o)

Specified by:
lastIndexOf in interface List<E extends Comparable<? super E>>

listIterator

public ListIterator<E> listIterator()

Specified by:
listIterator in interface List<E extends Comparable<? super E>>

listIterator

public ListIterator<E> listIterator(int index)

Specified by:
listIterator in interface List<E extends Comparable<? super E>>

subList

public List<E> subList(int fromIndex,
                       int toIndex)

Specified by:
subList in interface List<E extends Comparable<? super E>>

getComparator

public Comparator<E> getComparator()
Get the current Comparator instance.

Returns:
Returns the comparator.

setComparator

public void setComparator(Comparator<E> comparator)
Set the Comparator to use.

Parameters:
comparator - The comparator to set.


Copyright © 2009 CIRG. All Rights Reserved.