javolution.util
Class Index

Object
  extended by Number
      extended by Index
All Implemented Interfaces:
Serializable, Comparable<Index>, Immutable, Realtime, FastCollection.Record, XMLSerializable

public final class Index
extends Number
implements Comparable<Index>, FastCollection.Record, Realtime, Immutable, XMLSerializable

This class represents a unique index which can be used instead of java.lang.Integer for primitive data types collections. For example:

         class SparseVector<F> {
             FastMap<Index, F> _elements = new FastMap<Index, F>();
             ...
         }

Unicity is guaranteed and direct equality (==) can be used in place of object equality (Index.equals(Object)).

Indices have no adverse effect on the garbage collector (persistent instances), but should not be used for large integer values as that would increase the permanent memory footprint significantly.

RTSJ: Instance of this classes are allocated in ImmortalMemory. Indices can be pre-allocated at start-up to avoid run-time allocation delays by configuring INITIAL_FIRST and/or INITIAL_LAST or through setMinimumRange(int, int).

Version:
5.1, July 26, 2007
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Field Summary
static Configurable<Integer> INITIAL_FIRST
          Holds the initial first index value (default -1).
static Configurable<Integer> INITIAL_LAST
          Holds the initial last index value (default 31).
static Index ZERO
          Holds the index zero (value 0).
 
Method Summary
 int compareTo(Index that)
           
 double doubleValue()
          Returns the index value as int.
 boolean equals(Object obj)
          Indicates if this index is equals to the one specified (unicity ensures that this method is equivalent to ==).
 float floatValue()
          Returns the index value as float.
 FastCollection.Record getNext()
          Returns the record after this one.
 FastCollection.Record getPrevious()
          Returns the record before this one.
 int hashCode()
          Returns the hash code for this index.
 int intValue()
          Returns the index value as int.
 long longValue()
          Returns the index value as long.
static List<Index> rangeOf(int start, int end)
          Returns all the indices greater or equal to start but less than end.
protected  Object readResolve()
          Ensures index unicity during deserialization.
static void setMinimumRange(int first, int last)
          Creates the indices for the specified range of values if they don't exist.
 String toString()
          Returns the String representation of this index.
 Text toText()
          Returns the textual representation of this real-time object (equivalent to toString except that the returned value can be "stack" allocated and supports fast concatenation).
static Index valueOf(int i)
          Returns the unique index for the specified int value (creating it as well as the indices toward zero if they do not exist).
static List<Index> valuesOf(int... indices)
          Returns the list of all the indices specified.
 
Methods inherited from class Number
byteValue, shortValue
 
Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Index ZERO
Holds the index zero (value 0).


INITIAL_FIRST

public static final Configurable<Integer> INITIAL_FIRST
Holds the initial first index value (default -1).


INITIAL_LAST

public static final Configurable<Integer> INITIAL_LAST
Holds the initial last index value (default 31).

Method Detail

setMinimumRange

public static void setMinimumRange(int first,
                                   int last)
Creates the indices for the specified range of values if they don't exist.

Parameters:
first - the first index value.
last - the last index value.
Throws:
IllegalArgumentException - if first > last

valueOf

public static Index valueOf(int i)
Returns the unique index for the specified int value (creating it as well as the indices toward zero if they do not exist).

Parameters:
i - the index value.
Returns:
the corresponding unique index.

rangeOf

public static List<Index> rangeOf(int start,
                                  int end)
Returns all the indices greater or equal to start but less than end.

Parameters:
start - the start index.
end - the end index.
Returns:
[start .. end[

valuesOf

public static List<Index> valuesOf(int... indices)
Returns the list of all the indices specified.

Parameters:
indices - the indices values.
Returns:
{indices[0], indices[1], ...}

intValue

public int intValue()
Returns the index value as int.

Specified by:
intValue in class Number
Returns:
the index value.

longValue

public long longValue()
Returns the index value as long.

Specified by:
longValue in class Number
Returns:
the index value.

floatValue

public float floatValue()
Returns the index value as float.

Specified by:
floatValue in class Number
Returns:
the index value.

doubleValue

public double doubleValue()
Returns the index value as int.

Specified by:
doubleValue in class Number
Returns:
the index value.

toString

public String toString()
Returns the String representation of this index.

Overrides:
toString in class Object
Returns:
TextFormat.getInstance(Cursor.class).formatToString(_value)

equals

public final boolean equals(Object obj)
Indicates if this index is equals to the one specified (unicity ensures that this method is equivalent to ==).

Overrides:
equals in class Object
Returns:
this == obj

hashCode

public final int hashCode()
Returns the hash code for this index.

Overrides:
hashCode in class Object
Returns:
the index value.

readResolve

protected final Object readResolve()
                            throws ObjectStreamException
Ensures index unicity during deserialization.

Returns:
the unique instance for this deserialized index.
Throws:
ObjectStreamException

compareTo

public final int compareTo(Index that)
Specified by:
compareTo in interface Comparable<Index>

getNext

public final FastCollection.Record getNext()
Description copied from interface: FastCollection.Record
Returns the record after this one.

Specified by:
getNext in interface FastCollection.Record
Returns:
the next record.

getPrevious

public final FastCollection.Record getPrevious()
Description copied from interface: FastCollection.Record
Returns the record before this one.

Specified by:
getPrevious in interface FastCollection.Record
Returns:
the previous record.

toText

public Text toText()
Description copied from interface: Realtime
Returns the textual representation of this real-time object (equivalent to toString except that the returned value can be "stack" allocated and supports fast concatenation).

Specified by:
toText in interface Realtime
Returns:
this object's textual representation.


Copyright © 2005-2012 Javolution. All Rights Reserved.