Class Pair
- All Implemented Interfaces:
Comparable
An example use of this class could be to represent classification of hardware systems as an OS/Version pair of String objects.
Note: If instances of this class will be used in collection classes, you must make sure that the encapsulated primary and secondary objects implement the Comparable interface. In addition, it is recommended to have these classes also override equals() and hashCode() methods defined in class Object.
- Author:
- Madhu Narasimhan
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
Compares this object with the specified object for order.boolean
Compares this Pair to the specified object.Returns the primary objectIf the primary object is a String returns the String.Returns the secondary objectIf the secondary object is a String returns the String.int
hashCode()
Returns a hash code value for the object.void
setPrimary
(Object primary) Sets the primary object to the given valuevoid
setSecondary
(Object secondary) Sets the secondary object to the given valuetoString()
Returns a string form of the primary and secondary objects.
-
Constructor Details
-
Pair
public Pair()Constructs a Pair with primary and secondary objects set to null. -
Pair
Constructs a Pair with the given primary and secondary values.- Parameters:
primary
- Primary valuesecondary
- Secondary value
-
-
Method Details
-
getPrimary
Returns the primary object- Returns:
- Primary object
-
getPrimaryAsString
If the primary object is a String returns the String.- Parameters:
Primary
- object as String or null if it's not a string.
-
setPrimary
Sets the primary object to the given value- Parameters:
primary
- Primary object
-
getSecondary
Returns the secondary object- Returns:
- Secondary object
-
getSecondaryAsString
If the secondary object is a String returns the String.- Parameters:
Secondary
- object as String or null if it's not a string.
-
setSecondary
Sets the secondary object to the given value- Parameters:
secondary
- Secondary object
-
equals
Compares this Pair to the specified object. The result is true if and only if the argument is not null and is a Pair object whose primary and secondary objects are the same as those of this object. For comparing corresponding primary and secondary objects of the 2 instances, the equals() method of those objects will be invoked. -
compareTo
Compares this object with the specified object for order. Returns a negative integer, zero or a positive integer as this object is less than, equal to or greater than the specified object.First invokes the compareTo() method on the primary member of this object to compare it with the primary member of the passed object. If the result is non-zero, it returns with this result. If the result is zero, it then invokes the same method on the secondary member of this object to compare that with the secondary member of the passed object. Returns the result of this second comparison.
In essence, the ordering is based on the primary object. The primary object being the same, the ordering is then based on the secondary object. A null primary/secondary object is assumed to be less than a non-null primary/secondary object.
Note: If you will be using instances of this class in collection classes, then you must make sure that both primary and secondary objects are instances of classes that implement the Comparable interface. You must also make sure that all Pair instances in the collection have primary objects of the same type and secondary objects of the same type.
- Specified by:
compareTo
in interfaceComparable
- Parameters:
another
- Object to compare this instance to.- Returns:
- A negative integer, zero or a positive integer as this object is less than, equal to or greater than the specified object.
- Throws:
NullPointerException
- If the passed object is nullClassCastException
- If the specified object's type prevents it from being compared to this instance or if at least one of the primary and secondary objects of the two compared Pair instances does not implement the Comparable interface.- See Also:
-
hashCode
public int hashCode()Returns a hash code value for the object. This hash code is computed as the average hash code of the primary and secondary objects. Though this is not a very efficient method, it does rely on the hashCode method implementation of the contained objects. -
toString
Returns a string form of the primary and secondary objects. Invokes the toString() method of each object.
-