Class Pair

java.lang.Object
com.portal.pfc.util.Pair
All Implemented Interfaces:
Comparable

public class Pair extends Object implements Comparable
Utility class to hold a pair of objects that are related to each other. A typical use of this is where a Map or Set cannot be used to hold these pairs due to duplication of the key (really the primary value in the context of this class). Instead, a List can be used to hold instances of this class. Allows null values for both the primary and secondary objects.

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 Details

    • Pair

      public Pair()
      Constructs a Pair with primary and secondary objects set to null.
    • Pair

      public Pair(Object primary, Object secondary)
      Constructs a Pair with the given primary and secondary values.
      Parameters:
      primary - Primary value
      secondary - Secondary value
  • Method Details

    • getPrimary

      public Object getPrimary()
      Returns the primary object
      Returns:
      Primary object
    • getPrimaryAsString

      public String 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

      public void setPrimary(Object primary)
      Sets the primary object to the given value
      Parameters:
      primary - Primary object
    • getSecondary

      public Object getSecondary()
      Returns the secondary object
      Returns:
      Secondary object
    • getSecondaryAsString

      public String 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

      public void setSecondary(Object secondary)
      Sets the secondary object to the given value
      Parameters:
      secondary - Secondary object
    • equals

      public boolean equals(Object object)
      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.
      Overrides:
      equals in class Object
      Parameters:
      object - Object to compare this instance to.
      Returns:
      true if the objects are equal, false otherwise.
    • compareTo

      public int compareTo(Object another)
      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 interface Comparable
      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 null
      ClassCastException - 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.
      Overrides:
      hashCode in class Object
      Returns:
      Hash code value for the object.
    • toString

      public String toString()
      Returns a string form of the primary and secondary objects. Invokes the toString() method of each object.
      Overrides:
      toString in class Object
      Returns:
      String containing the values of primary and secondary objects