home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / Pair.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  2.0 KB  |  102 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package COM.objectspace.jgl;
  5.  
  6. import java.io.Serializable;
  7.  
  8. /**
  9.  * A Pair is an object that contains two other objects. It is
  10.  * most commonly used for conveniently storing and passing pairs
  11.  * of objects.
  12.  * <p>
  13.  * @version 2.0.2
  14.  * @author ObjectSpace, Inc.
  15.  */
  16.  
  17. public class Pair implements Serializable
  18.   {
  19.   /**
  20.    * The first object.
  21.    */
  22.   public Object first;
  23.  
  24.   /**
  25.    * The second object.
  26.    */
  27.   public Object second;
  28.  
  29.   /**
  30.    * Construct myself to hold a pair of objects.
  31.    * @param x The first object.
  32.    * @param y The second object.
  33.    */
  34.   public Pair( Object x, Object y )
  35.     {
  36.     first = x;
  37.     second = y;
  38.     }
  39.  
  40.   /**
  41.    * Construct myself to hold a pair of objects initially null.
  42.    */
  43.   public Pair()
  44.     {
  45.     first = null;
  46.     second = null;
  47.     }
  48.  
  49.   /**
  50.    * Construct myself to be a copy of an existing Pair.
  51.    * @param pair The Pair to copy.
  52.    */
  53.   public Pair( Pair pair )
  54.     {
  55.     first = pair.first;
  56.     second = pair.second;
  57.     }
  58.  
  59.   /**
  60.    * Return my hash code.
  61.    */
  62.   public int hashCode()
  63.     {
  64.     int h = first == null ? 0 : first.hashCode();
  65.     if ( second != null )
  66.       h ^= second.hashCode();
  67.     return h;
  68.     }
  69.  
  70.   /**
  71.    * Return a string that describes me.
  72.    */
  73.   public String toString()
  74.     {
  75.     return "Pair( " + first + ", " + second + " )";
  76.     }
  77.  
  78.   public boolean equals( Object object )
  79.     {
  80.     return object instanceof Pair && equals( (Pair)object );
  81.     }
  82.  
  83.   public boolean equals( Pair pair )
  84.     {
  85.     if ( pair == null )
  86.       return false;
  87.  
  88.     return
  89.       ( first == null ? pair.first == null : first.equals( pair.first ) )
  90.       && ( second == null ? pair.second == null : second.equals( pair.second ) );
  91.     }
  92.  
  93.   /**
  94.    * Return a copy of myself.
  95.    */
  96.   public synchronized Object clone()
  97.     {
  98.     return new Pair( this );
  99.     }
  100.  
  101.   }
  102.