01: /*
02: * GNetWatch
03: * Copyright 2006, 2007 Alexandre Fenyo
04: * gnetwatch@fenyo.net
05: *
06: * This file is part of GNetWatch.
07: *
08: * GNetWatch is free software; you can redistribute it and/or modify
09: * it under the terms of the GNU General Public License as published by
10: * the Free Software Foundation; either version 2 of the License, or
11: * (at your option) any later version.
12: *
13: * GNetWatch is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: * You should have received a copy of the GNU General Public License
19: * along with GNetWatch; if not, write to the Free Software
20: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21: */
22:
23: package net.fenyo.gnetwatch;
24:
25: import net.fenyo.gnetwatch.targets.Target;
26:
27: /**
28: * This utility class groups objects by pairs.
29: * @author Alexandre Fenyo
30: * @version $Id: Pair.java,v 1.6 2007/03/03 00:38:19 fenyo Exp $
31: */
32:
33: public class Pair<E, F> {
34: private final E e;
35: private final F f;
36:
37: /**
38: * Constructor.
39: * @param e first object.
40: * @param f last object.
41: */
42: public Pair(final E e, final F f) {
43: this .e = e;
44: this .f = f;
45: }
46:
47: /**
48: * Returns the first object.
49: * @param none.
50: * @return E first object.
51: */
52: public E former() {
53: return e;
54: }
55:
56: /**
57: * Returns the last object.
58: * @param none.
59: * @return F last object.
60: */
61: public F latter() {
62: return f;
63: }
64:
65: /**
66: * Two pairs are equal if their respective first and last objects are equal.
67: * @param o another pair.
68: * @return boolean true in case of equality.
69: */
70: public boolean equals(final Object o) {
71: if (this == o)
72: return true;
73: if ((o == null) || (o.getClass() != getClass()))
74: return false;
75: final Pair<E, F> pair = (Pair<E, F>) o;
76: if (e == null && f == null)
77: return pair.former() == null && pair.latter() == null;
78: if (e == null)
79: return pair.former() == null && f.equals(pair.latter());
80: if (f == null)
81: return pair.latter() == null && e.equals(pair.former());
82: return e.equals(pair.former()) && f.equals(pair.latter());
83: }
84:
85: /**
86: * Returns a hashcode.
87: * @param none.
88: * @return int hashcode.
89: */
90: public int hashCode() {
91: if (e == null && f == null)
92: return 0;
93: if (e != null)
94: return e.hashCode();
95: if (f != null)
96: return f.hashCode();
97: return e.hashCode() ^ f.hashCode();
98: }
99: }
|