KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > Pair


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch;
25
26 import net.fenyo.gnetwatch.targets.Target;
27
28 /**
29  * This utility class groups objects by pairs.
30  * @author Alexandre Fenyo
31  * @version $Id: Pair.java,v 1.6 2007/03/03 00:38:19 fenyo Exp $
32  */

33
34 public class Pair<E, F> {
35   private final E e;
36   private final F f;
37
38   /**
39    * Constructor.
40    * @param e first object.
41    * @param f last object.
42    */

43   public Pair(final E e, final F f) {
44     this.e = e;
45     this.f = f;
46   }
47
48   /**
49    * Returns the first object.
50    * @param none.
51    * @return E first object.
52    */

53   public E former() {
54     return e;
55   }
56
57   /**
58    * Returns the last object.
59    * @param none.
60    * @return F last object.
61    */

62   public F latter() {
63     return f;
64   }
65
66   /**
67    * Two pairs are equal if their respective first and last objects are equal.
68    * @param o another pair.
69    * @return boolean true in case of equality.
70    */

71   public boolean equals(final Object JavaDoc o) {
72     if (this == o) return true;
73     if ((o == null) || (o.getClass() != getClass())) return false;
74     final Pair<E, F> pair = (Pair<E, F>) o;
75     if (e == null && f == null) return pair.former() == null && pair.latter() == null;
76     if (e == null) return pair.former() == null && f.equals(pair.latter());
77     if (f == null) return pair.latter() == null && e.equals(pair.former());
78     return e.equals(pair.former()) && f.equals(pair.latter());
79   }
80
81   /**
82    * Returns a hashcode.
83    * @param none.
84    * @return int hashcode.
85    */

86   public int hashCode() {
87     if (e == null && f == null) return 0;
88     if (e != null) return e.hashCode();
89     if (f != null) return f.hashCode();
90     return e.hashCode() ^ f.hashCode();
91   }
92 }
93
Popular Tags