net.sf.ivmaidns.util
Class ConstPair

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

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

Class for immutable pairs of objects. This instantable class is useful for representation of a constant sortable pair of custom objects of any type (a container for two elements).

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

Field Summary
protected  java.lang.Object valueA
          The first custom object of this pair.
protected  java.lang.Object valueB
          The second custom object of this pair.
 
Constructor Summary
ConstPair(java.lang.Object valueA, java.lang.Object valueB)
          Constructs an immutable 'pair' container.
 
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.
 int length()
          Returns the number of elements in this container.
 java.lang.String toString()
          Converts container to its 'in-line' string representation.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

valueA

protected final java.lang.Object valueA
The first custom object of this pair.

See Also:
ConstPair(java.lang.Object, java.lang.Object), getAt(int)

valueB

protected final java.lang.Object valueB
The second custom object of this pair.

See Also:
ConstPair(java.lang.Object, java.lang.Object), getAt(int)
Constructor Detail

ConstPair

public ConstPair(java.lang.Object valueA,
                 java.lang.Object valueB)
Constructs an immutable 'pair' container.

Parameters:
valueA - the first custom object (may be null).
valueB - the second custom object (may be null).
See Also:
getAt(int), equals(java.lang.Object), greaterThan(java.lang.Object), toString()
Method Detail

length

public int length()
Returns the number of elements in this container. Here, the result is 2.

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

getAt

public java.lang.Object getAt(int index)
                       throws java.lang.ArrayIndexOutOfBoundsException
Returns value of the element at the specified 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:
ConstPair(java.lang.Object, java.lang.Object), length()

clone

public java.lang.Object clone()
Creates and returns a copy of this object. The result is the same as of new ConstPair(getAt(0), getAt(1)).

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:
ConstPair(java.lang.Object, java.lang.Object), getAt(int), 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 container 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 container class and all elements of this container are equal to the corresponding elements of obj.

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:
ConstPair(java.lang.Object, java.lang.Object), 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. Containers are compared in the element-by-element manner, starting at index 0. So, the first elements pair is tested for equality and then it (if equality test has failed) or (else) the second elements pair is compared through INSTANCE of GComparator class (and the result of this comparison is returned).

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 container to its 'in-line' string representation. Here, these two values 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, with non-zero length()) of this object.
Throws:
java.lang.OutOfMemoryError - if there is not enough memory.
See Also:
ConstPair(java.lang.Object, java.lang.Object), length(), getAt(int)