Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Util.Collections
Class SortedArraySet  view SortedArraySet download SortedArraySet.java

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractList
          extended byUtil.Collections.SortedArraySet
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, java.lang.Iterable, java.util.List, java.util.RandomAccess, java.io.Serializable, java.util.Set, java.util.SortedSet

public class SortedArraySet
extends java.util.AbstractList
implements java.util.SortedSet, java.util.List, java.lang.Cloneable, java.io.Serializable, java.util.RandomAccess

Set that is stored as a sorted list. This allows linear-time merge operations, among other things. Does not handle "null" elements.

Version:
$Id: SortedArraySet.java,v 1.3 2003/07/07 10:29:55 joewhaley Exp $

Nested Class Summary
static class SortedArraySet.SortedArraySetFactory
           
private  class SortedArraySet.SubSet
           
 
Nested classes inherited from class java.util.AbstractList
 
Field Summary
private  java.util.Comparator comparator
          The comparator used for this SortedArraySet, or "null" if we are using the default element ordering.
static boolean DISALLOW_DIRECT_MODIFICATIONS
           
private  java.lang.Object[] elementData
          The array buffer into which the elements of the SortedArraySet are stored.
static SortedArraySet.SortedArraySetFactory FACTORY
           
static boolean REDUCE_ALLOCATIONS
           
private  int size
          The size of the SortedArraySet (the number of elements it contains).
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
private SortedArraySet()
          Constructs an empty set with an initial capacity of ten.
private SortedArraySet(java.util.Collection c)
           
private SortedArraySet(java.util.Comparator comparator)
           
private SortedArraySet(int initialCapacity)
          Constructs an empty set with the specified initial capacity.
private SortedArraySet(int initialCapacity, java.util.Comparator comparator)
           
 
Method Summary
 void add(int arg0, java.lang.Object arg1)
          Insert an element into the list at a given position (optional operation).
 boolean add(java.lang.Object arg0)
          Add an element to the end of the list (optional operation).
 boolean addAll(java.util.Collection that)
          Add the contents of a collection to the end of the list (optional operation).
 boolean addAll(java.util.SortedSet that)
           
private  void checkAgainstSize(int arg0)
           
 java.lang.Object clone()
          This method may be called to create a new copy of the Object.
 java.util.Comparator comparator()
          Returns the comparator used in sorting this set, or null if it is the elements' natural ordering.
private  int compare(java.lang.Object o1, java.lang.Object o2)
           
 boolean contains(java.lang.Object arg0)
          Test whether this list contains a given object as one of its elements.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this SortedArraySet instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
 boolean equals(java.util.Collection that)
           
 boolean equals(java.lang.Object arg0)
          Test whether this list is equal to another object.
 boolean equals(java.util.SortedSet that)
           
 java.lang.Object first()
          Returns the first (lowest sorted) element in the set.
 java.lang.Object get(int arg0)
          Get the element at a given index in this list.
 int hashCode()
          Obtains a hash code for this list.
 java.util.SortedSet headSet(java.lang.Object arg0)
          Returns a view of the portion of the set strictly less than toElement.
 int indexOf(java.lang.Object arg0)
          Obtain the first index at which a given object is to be found in this list.
 java.lang.Object last()
          Returns the last (highest sorted) element in the set.
 int lastIndexOf(java.lang.Object arg0)
          Obtain the last index at which a given object is to be found in this list.
 java.lang.Object remove(int arg0)
          Remove the element at a given position in this list (optional operation).
 boolean remove(java.lang.Object arg0)
          Remove the first occurence of an object from this list (optional operation).
protected  void removeRange(int arg0, int arg1)
          Remove a subsection of the list.
 java.lang.Object set(int arg0, java.lang.Object arg1)
          Replace an element of this list with another object (optional operation).
 int size()
          Get the number of elements in this list.
 java.util.List subList(int arg0, int arg1)
          Obtain a List view of a subsection of this list, from fromIndex (inclusive) to toIndex (exclusive).
 java.util.SortedSet subSet(java.lang.Object arg0, java.lang.Object arg1)
          Returns a view of the portion of the set greater than or equal to fromElement, and strictly less than toElement.
 java.util.SortedSet tailSet(java.lang.Object arg0)
          Returns a view of the portion of the set greater than or equal to fromElement.
private  int whereDoesItGo(java.lang.Object o)
           
 
Methods inherited from class java.util.AbstractList
addAll, clear, iterator, listIterator, listIterator
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
clear, containsAll, isEmpty, iterator, removeAll, retainAll, toArray, toArray
 
Methods inherited from interface java.util.List
addAll, clear, containsAll, isEmpty, iterator, listIterator, listIterator, removeAll, retainAll, toArray, toArray
 

Field Detail

elementData

private transient java.lang.Object[] elementData
The array buffer into which the elements of the SortedArraySet are stored. The capacity of the SortedArraySet is the length of this array buffer.


size

private int size
The size of the SortedArraySet (the number of elements it contains).


comparator

private final java.util.Comparator comparator
The comparator used for this SortedArraySet, or "null" if we are using the default element ordering.


DISALLOW_DIRECT_MODIFICATIONS

public static final boolean DISALLOW_DIRECT_MODIFICATIONS
See Also:
Constant Field Values

REDUCE_ALLOCATIONS

public static final boolean REDUCE_ALLOCATIONS
See Also:
Constant Field Values

FACTORY

public static final SortedArraySet.SortedArraySetFactory FACTORY
Constructor Detail

SortedArraySet

private SortedArraySet(int initialCapacity)
Constructs an empty set with the specified initial capacity.


SortedArraySet

private SortedArraySet()
Constructs an empty set with an initial capacity of ten.


SortedArraySet

private SortedArraySet(java.util.Collection c)

SortedArraySet

private SortedArraySet(java.util.Comparator comparator)

SortedArraySet

private SortedArraySet(int initialCapacity,
                       java.util.Comparator comparator)
Method Detail

get

public java.lang.Object get(int arg0)
Description copied from interface: java.util.List
Get the element at a given index in this list.

Specified by:
get in interface java.util.List

checkAgainstSize

private void checkAgainstSize(int arg0)

size

public int size()
Description copied from interface: java.util.List
Get the number of elements in this list. If the list contains more than Integer.MAX_VALUE elements, return Integer.MAX_VALUE.

Specified by:
size in interface java.util.Set

comparator

public java.util.Comparator comparator()
Description copied from interface: java.util.SortedSet
Returns the comparator used in sorting this set, or null if it is the elements' natural ordering.

Specified by:
comparator in interface java.util.SortedSet

subSet

public java.util.SortedSet subSet(java.lang.Object arg0,
                                  java.lang.Object arg1)
Description copied from interface: java.util.SortedSet
Returns a view of the portion of the set greater than or equal to fromElement, and strictly less than toElement. The view is backed by this set, so changes in one show up in the other. The subset supports all optional operations of the original.

The returned set throws an IllegalArgumentException any time an element is used which is out of the range of fromElement and toElement. Note that the lower endpoint is included, but the upper is not; if you want to change the inclusion or exclusion of an endpoint, pass its successor object in instead. For example, for Integers, you can request subSet(new Integer(lowlimit.intValue() + 1), new Integer(highlimit.intValue() + 1)) to reverse the inclusiveness of both endpoints.

Specified by:
subSet in interface java.util.SortedSet

headSet

public java.util.SortedSet headSet(java.lang.Object arg0)
Description copied from interface: java.util.SortedSet
Returns a view of the portion of the set strictly less than toElement. The view is backed by this set, so changes in one show up in the other. The subset supports all optional operations of the original.

The returned set throws an IllegalArgumentException any time an element is used which is out of the range of toElement. Note that the endpoint, toElement, is not included; if you want this value included, pass its successor object in to toElement. For example, for Integers, you could request headSet(new Integer(limit.intValue() + 1)).

Specified by:
headSet in interface java.util.SortedSet

tailSet

public java.util.SortedSet tailSet(java.lang.Object arg0)
Description copied from interface: java.util.SortedSet
Returns a view of the portion of the set greater than or equal to fromElement. The view is backed by this set, so changes in one show up in the other. The subset supports all optional operations of the original.

The returned set throws an IllegalArgumentException any time an element is used which is out of the range of fromElement. Note that the endpoint, fromElement, is included; if you do not want this value to be included, pass its successor object in to fromElement. For example, for Integers, you could request tailSet(new Integer(limit.intValue() + 1)).

Specified by:
tailSet in interface java.util.SortedSet

first

public java.lang.Object first()
Description copied from interface: java.util.SortedSet
Returns the first (lowest sorted) element in the set.

Specified by:
first in interface java.util.SortedSet

last

public java.lang.Object last()
Description copied from interface: java.util.SortedSet
Returns the last (highest sorted) element in the set.

Specified by:
last in interface java.util.SortedSet

compare

private int compare(java.lang.Object o1,
                    java.lang.Object o2)

whereDoesItGo

private int whereDoesItGo(java.lang.Object o)

add

public boolean add(java.lang.Object arg0)
Description copied from interface: java.util.List
Add an element to the end of the list (optional operation). If the list imposes restraints on what can be inserted, such as no null elements, this should be documented.

Specified by:
add in interface java.util.Set

ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity of this SortedArraySet instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.


add

public void add(int arg0,
                java.lang.Object arg1)
Description copied from interface: java.util.List
Insert an element into the list at a given position (optional operation). This shifts all existing elements from that position to the end one index to the right. This version of add has no return, since it is assumed to always succeed if there is no exception.

Specified by:
add in interface java.util.List

remove

public java.lang.Object remove(int arg0)
Description copied from interface: java.util.List
Remove the element at a given position in this list (optional operation). Shifts all remaining elements to the left to fill the gap.

Specified by:
remove in interface java.util.List

set

public java.lang.Object set(int arg0,
                            java.lang.Object arg1)
Description copied from interface: java.util.List
Replace an element of this list with another object (optional operation).

Specified by:
set in interface java.util.List

addAll

public boolean addAll(java.util.Collection that)
Description copied from interface: java.util.List
Add the contents of a collection to the end of the list (optional operation). This operation is undefined if this list is modified during the operation (for example, if you try to insert a list into itself).

Specified by:
addAll in interface java.util.Set

addAll

public boolean addAll(java.util.SortedSet that)

indexOf

public int indexOf(java.lang.Object arg0)
Description copied from interface: java.util.List
Obtain the first index at which a given object is to be found in this list.

Specified by:
indexOf in interface java.util.List

lastIndexOf

public int lastIndexOf(java.lang.Object arg0)
Description copied from interface: java.util.List
Obtain the last index at which a given object is to be found in this list.

Specified by:
lastIndexOf in interface java.util.List

equals

public boolean equals(java.lang.Object arg0)
Description copied from interface: java.util.List
Test whether this list is equal to another object. A List is defined to be equal to an object if and only if that object is also a List, and the two lists have the same sequence. Two lists l1 and l2 are equal if and only if l1.size() == l2.size(), and for every integer n between 0 and l1.size() - 1 inclusive, l1.get(n) == null ? l2.get(n) == null : l1.get(n).equals(l2.get(n)).

Specified by:
equals in interface java.util.Set

equals

public boolean equals(java.util.SortedSet that)

equals

public boolean equals(java.util.Collection that)

hashCode

public int hashCode()
Description copied from interface: java.util.List
Obtains a hash code for this list. In order to obey the general contract of the hashCode method of class Object, this value is calculated as follows:

hashCode = 1;
Iterator i = list.iterator();
while (i.hasNext())
{
  Object obj = i.next();
  hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
}

This ensures that the general contract of Object.hashCode() is adhered to.

Specified by:
hashCode in interface java.util.Set

removeRange

protected void removeRange(int arg0,
                           int arg1)
Description copied from class: java.util.AbstractList
Remove a subsection of the list. This is called by the clear and removeRange methods of the class which implements subList, which are difficult for subclasses to override directly. Therefore, this method should be overridden instead by the more efficient implementation, if one exists. Overriding this can reduce quadratic efforts to constant time in some cases!

This implementation first checks for illegal or out of range arguments. It then obtains a ListIterator over the list using listIterator(fromIndex). It then calls next() and remove() on this iterator repeatedly, toIndex - fromIndex times.


subList

public java.util.List subList(int arg0,
                              int arg1)
Description copied from interface: java.util.List
Obtain a List view of a subsection of this list, from fromIndex (inclusive) to toIndex (exclusive). If the two indices are equal, the sublist is empty. The returned list should be modifiable if and only if this list is modifiable. Changes to the returned list should be reflected in this list. If this list is structurally modified in any way other than through the returned list, the result of any subsequent operations on the returned list is undefined.

Specified by:
subList in interface java.util.List

contains

public boolean contains(java.lang.Object arg0)
Description copied from interface: java.util.List
Test whether this list contains a given object as one of its elements. This is defined as the existence of an element e such that o == null ? e == null : o.equals(e).

Specified by:
contains in interface java.util.Set

remove

public boolean remove(java.lang.Object arg0)
Description copied from interface: java.util.List
Remove the first occurence of an object from this list (optional operation). That is, remove the first element e such that o == null ? e == null : o.equals(e).

Specified by:
remove in interface java.util.Set

clone

public java.lang.Object clone()
Description copied from class: java.lang.Object
This method may be called to create a new copy of the Object. The typical behavior is as follows:
  • o == o.clone() is false
  • o.getClass() == o.clone().getClass() is true
  • o.equals(o) is true

However, these are not strict requirements, and may be violated if necessary. Of the three requirements, the last is the most commonly violated, particularly if the subclass does not override Object.equals(Object)>Object.equals(Object) 55 .

If the Object you call clone() on does not implement java.lang.Cloneable (which is a placeholder interface), then a CloneNotSupportedException is thrown. Notice that Object does not implement Cloneable; this method exists as a convenience for subclasses that do.

Object's implementation of clone allocates space for the new Object using the correct class, without calling any constructors, and then fills in all of the new field values with the old field values. Thus, it is a shallow copy. However, subclasses are permitted to make a deep copy.

All array types implement Cloneable, and override this method as follows (it should never fail):

 public Object clone()
 {
   try
     {
       super.clone();
     }
   catch (CloneNotSupportedException e)
     {
       throw new InternalError(e.getMessage());
     }
 }