net.sf.ivmaidns.util
Class ConstVector

java.lang.Object
  extended by net.sf.ivmaidns.util.ConstVector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Immutable, Indexable, ReallyCloneable, Sortable, Verifiable

public final class ConstVector
extends java.lang.Object
implements Immutable, ReallyCloneable, java.io.Serializable, Indexable, Sortable, Verifiable

Class for immutable vectors for objects. This final class is useful for representation of a constant vector (container) of custom objects of any type. An instance of this class encapsulates/wraps (when constructed) a given Object array. Important notes: the component type of the encapsulated Object array is hidden; an object of this class may be serialized only if all custom objects it contains are serializable.

Since:
1.2
Version:
2.0
Author:
Ivan Maidanski
See Also:
ConstPair, ObjectVector, GComparator, Serialized Form

Field Summary
protected  java.lang.Object[] array
          The wrapped (encapsulated) custom array of objects.
 
Constructor Summary
ConstVector(java.lang.Object[] array)
          Constructs an immutable vector by wrapping a given custom array of objects.
 
Method Summary
 java.lang.Object clone()
          Creates and returns a copy of this object.
 boolean equals(java.lang.Object obj)
          Indicates whether this object is equal to the specified one.
 java.lang.Object getAt(int index)
          Returns value of the element at the specified index.
 boolean greaterThan(java.lang.Object obj)
          Tests for being semantically greater than the argument.
 int hashCode()
          Computes and returns a hash code value for the object.
 void integrityCheck()
          Verifies this object for its integrity.
 int length()
          Returns the number of elements in this vector.
 java.lang.Object[] toArray()
          Returns a newly created array filled with the values of the elements of this vector.
 java.lang.String toString()
          Converts vector to its 'in-line' string representation.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

array

protected final java.lang.Object[] array
The wrapped (encapsulated) custom array of objects. array must be non-null (but its elements may be null). Important notes: array[index] must not be changed anyhow for every index; the component type of array is of no use here.

See Also:
ConstVector(java.lang.Object[]), length(), getAt(int), toArray(), integrityCheck()
Constructor Detail

ConstVector

public ConstVector(java.lang.Object[] array)
            throws java.lang.NullPointerException
Constructs an immutable vector by wrapping a given custom array of objects. Important notes: array[index] must not be changed anyhow for each index (since no cloning is performed for array here).

Parameters:
array - the object array (must be non-null) to be encapsulated.
Throws:
java.lang.NullPointerException - if array is null.
See Also:
getAt(int), toArray(), equals(java.lang.Object), greaterThan(java.lang.Object), toString()
Method Detail

length

public int length()
Returns the number of elements in this vector. The result is the same as length of toArray().

Specified by:
length in interface Indexable
Returns:
amount (non-negative value) of elements.
See Also:
getAt(int), toArray()

getAt

public java.lang.Object getAt(int index)
                       throws java.lang.ArrayIndexOutOfBoundsException
Returns value of the element at the specified index. The result is the same as of toArray()[index].

Specified by:
getAt in interface Indexable
Parameters:
index - the index (must be in the range) at which to return an element.
Returns:
an element (may be null) at index.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if index is negative or is not less than length().
See Also:
ConstVector(java.lang.Object[]), length(), toArray()

toArray

public java.lang.Object[] toArray()
Returns a newly created array filled with the values of the elements of this vector. Here, the result is the exact instance of Object[].

Returns:
a new array (not null), containing values of all the elements of this vector.
Throws:
java.lang.OutOfMemoryError - if there is not enough memory.
See Also:
ConstVector(java.lang.Object[]), getAt(int), length(), equals(java.lang.Object), greaterThan(java.lang.Object), toString()

clone

public java.lang.Object clone()
Creates and returns a copy of this object. Important notes: the encapsulated array is not cloned itself.

Specified by:
clone in interface ReallyCloneable
Overrides:
clone in class java.lang.Object
Returns:
a copy (not null and != this) of this instance.
Throws:
java.lang.OutOfMemoryError - if there is not enough memory.
See Also:
ConstVector(java.lang.Object[]), toArray(), equals(java.lang.Object)

hashCode

public int hashCode()
Computes and returns a hash code value for the object. This method hashes all non-null elements of this vector and mixes them all to produce a single hash code value.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object.
See Also:
length(), getAt(int), equals(java.lang.Object)

equals

public boolean equals(java.lang.Object obj)
Indicates whether this object is equal to the specified one. This method returns true if and only if obj is instance of this vector class and all the elements of this vector are equal to the corresponding elements of obj vector.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object (may be null) with which to compare.
Returns:
true if and only if this value is the same as obj value.
See Also:
length(), getAt(int), hashCode(), greaterThan(java.lang.Object)

greaterThan

public boolean greaterThan(java.lang.Object obj)
Tests for being semantically greater than the argument. The result is true if and only if obj is instance of this class and this object is greater than the specified object. Vectors are compared in the element-by-element manner, starting at index 0 and at each index elements are compared for equality and the first non-equal elements pair is compared through INSTANCE of GComparator class.

Specified by:
greaterThan in interface Sortable
Parameters:
obj - the second compared object (may be null).
Returns:
true if obj is comparable with this and this object is greater than obj, else false.
Since:
1.8
See Also:
length(), getAt(int), equals(java.lang.Object)

toString

public java.lang.String toString()
Converts vector to its 'in-line' string representation. The string representations of the values (if value is null then "null") of this vector are placed into the resulting string in the direct index order, delimited by a single space.

Overrides:
toString in class java.lang.Object
Returns:
the string representation (not null) of this object.
Throws:
java.lang.OutOfMemoryError - if there is not enough memory.
See Also:
ConstVector(java.lang.Object[]), toArray()

integrityCheck

public void integrityCheck()
Verifies this object for its integrity. The elements are not checked. For debug purpose only.

Specified by:
integrityCheck in interface Verifiable
Throws:
java.lang.InternalError - if integrity violation is detected.
Since:
2.0
See Also:
ConstVector(java.lang.Object[])