Commit | Line | Data |
---|---|---|
d4a8d935 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Philippe Sawicki (INF4990.A2010@gmail.com) - Initial API and implementation | |
11 | * Mathieu Denis (mathieu.denis55@gmail.com) - Refactored code | |
12 | * Bernd Hufmann - Integrated to TMF, fixed hashCode() and equals() methods | |
13 | *******************************************************************************/ | |
14 | package org.eclipse.linuxtools.tmf.core.util; | |
15 | ||
16 | /** | |
17 | * Pair utility class, encapsulates a pair of objects. | |
18 | * | |
19 | * @param <A> | |
20 | * The type of the first object. | |
21 | * @param <B> | |
22 | * The type of the second object. | |
23 | * | |
24 | * @author Philippe Sawicki | |
25 | * @since 2.0 | |
26 | */ | |
27 | public class Pair<A, B> { | |
28 | ||
29 | /** | |
30 | * A reference to the first object. | |
31 | */ | |
32 | protected A fFirst; | |
33 | /** | |
34 | * A reference to the second object. | |
35 | */ | |
36 | protected B fSecond; | |
37 | ||
38 | /** | |
39 | * Constructor. | |
40 | * @param first | |
41 | * The pair's first object. | |
42 | * @param second | |
43 | * The pair's second object. | |
44 | */ | |
45 | public Pair(A first, B second) { | |
46 | fFirst = first; | |
47 | fSecond = second; | |
48 | } | |
49 | ||
50 | /** | |
51 | * Constructor. | |
52 | */ | |
53 | public Pair() { | |
54 | this(null, null); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Pair hash code. | |
59 | */ | |
60 | @Override | |
61 | public int hashCode() { | |
62 | final int prime = 31; | |
63 | int result = 1; | |
64 | result = prime * result + ((fFirst == null) ? 0 : fFirst.hashCode()); | |
65 | result = prime * result + ((fSecond == null) ? 0 : fSecond.hashCode()); | |
66 | return result; | |
67 | } | |
68 | ||
69 | /** | |
70 | * Object comparison. | |
71 | */ | |
72 | @Override | |
73 | public boolean equals(Object obj) { | |
74 | if (this == obj) { | |
75 | return true; | |
76 | } | |
77 | ||
78 | if (obj == null) { | |
79 | return false; | |
80 | } | |
81 | ||
82 | if (getClass() != obj.getClass()) { | |
83 | return false; | |
84 | } | |
85 | ||
86 | Pair<?, ?> other = (Pair<?, ?>) obj; | |
87 | ||
88 | if (fFirst == null) { | |
89 | if (other.fFirst != null) { | |
90 | return false; | |
91 | } | |
92 | } else if (!fFirst.equals(other.fFirst)) { | |
93 | return false; | |
94 | } | |
95 | if (fSecond == null) { | |
96 | if (other.fSecond != null) { | |
97 | return false; | |
98 | } | |
99 | } else if (!fSecond.equals(other.fSecond)) { | |
100 | return false; | |
101 | } | |
102 | return true; | |
103 | } | |
104 | ||
105 | /** | |
106 | * Object to string. | |
107 | */ | |
108 | @Override | |
109 | public String toString() { | |
110 | return "(" + fFirst + ", " + fSecond + ")"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ | |
111 | } | |
112 | ||
113 | /** | |
114 | * Returns a reference to the pair's first object. | |
115 | * @return A reference to the pair's first object. | |
116 | */ | |
117 | public A getFirst() { | |
118 | return fFirst; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Sets the pair's first object. | |
123 | * @param first | |
124 | * The pair's first object. | |
125 | */ | |
126 | public void setFirst(A first) { | |
127 | fFirst = first; | |
128 | } | |
129 | ||
130 | /** | |
131 | * Returns a reference to the pair's second object. | |
132 | * @return A reference to the pair's second object. | |
133 | */ | |
134 | public B getSecond() { | |
135 | return fSecond; | |
136 | } | |
137 | ||
138 | /** | |
139 | * Sets the pair's second object. | |
140 | * @param second | |
141 | * The pair's second object. | |
142 | */ | |
143 | public void setSecond(B second) { | |
144 | fSecond = second; | |
145 | } | |
146 | } |