KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > Pair


1 package jfun.parsec;
2
3 import jfun.util.Misc;
4
5 /**
6  * This is a simple Java Bean for a pair of objects.
7  * <p>
8  * @author Ben Yu
9  * Apr 24, 2006 1:41:52 PM
10  * @param <A>
11  * @param <B>
12  * @since version 1.1
13  */

14 public class Pair<A, B> {
15   private A value1;
16   private B value2;
17   public Pair(){}
18   /**
19    * To create a Pair object.
20    * @param a the first object.
21    * @param b the second object.
22    */

23   public Pair(A a, B b) {
24     this.value1 = a;
25     this.value2 = b;
26   }
27   /**
28    * Get the first value.
29    */

30   public A getValue1() {
31     return value1;
32   }
33   /**
34    * Set the first value.
35    */

36   public void setValue1(A value1) {
37     this.value1 = value1;
38   }
39   /**
40    * Get the second value.
41    */

42   public B getValue2() {
43     return value2;
44   }
45   /**
46    * Set the second value.
47    */

48   public void setValue2(B value2) {
49     this.value2 = value2;
50   }
51   public boolean equals(Pair other){
52     return Misc.equals(value1, other.value1) && Misc.equals(value2, other.value2);
53   }
54   @Override JavaDoc
55   public boolean equals(Object JavaDoc obj) {
56     if(obj instanceof Pair){
57       return equals((Pair)obj);
58     }
59     else return false;
60   }
61   @Override JavaDoc
62   public int hashCode() {
63     return Misc.hashcode(value1)*31+Misc.hashcode(value2);
64   }
65   @Override JavaDoc
66   public String JavaDoc toString() {
67     return "("+value1+","+value2+")";
68   }
69   
70 }
71
Popular Tags