c805ece46e26e00b0648cb2cd68e71c427df6fc0
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / util / Pair.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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 * Alexandre Montplaisir - Made non-null and immutable
14 *******************************************************************************/
15 package org.eclipse.tracecompass.tmf.core.util;
16
17 import org.eclipse.jdt.annotation.Nullable;
18
19 /**
20 * Pair utility class, encapsulates a pair of objects.
21 *
22 * @param <A>
23 * The type of the first object.
24 * @param <B>
25 * The type of the second object.
26 *
27 * @author Philippe Sawicki
28 */
29 public class Pair<A, B> {
30
31 /**
32 * A reference to the first object.
33 */
34 private final A fFirst;
35 /**
36 * A reference to the second object.
37 */
38 private final B fSecond;
39
40 /**
41 * Constructor.
42 * @param first
43 * The pair's first object.
44 * @param second
45 * The pair's second object.
46 */
47 public Pair(A first, B second) {
48 fFirst = first;
49 fSecond = second;
50 }
51
52
53 /**
54 * Returns a reference to the pair's first object.
55 *
56 * @return A reference to the pair's first object.
57 */
58 public A getFirst() {
59 return fFirst;
60 }
61
62 /**
63 * Returns a reference to the pair's second object.
64 *
65 * @return A reference to the pair's second object.
66 */
67 public B getSecond() {
68 return fSecond;
69 }
70
71 @Override
72 public int hashCode() {
73 final int prime = 31;
74 int result = 1;
75 result = prime * result + fFirst.hashCode();
76 result = prime * result + fSecond.hashCode();
77 return result;
78 }
79
80 @Override
81 public boolean equals(@Nullable Object obj) {
82 if (this == obj) {
83 return true;
84 }
85
86 if (obj == null) {
87 return false;
88 }
89
90 if (getClass() != obj.getClass()) {
91 return false;
92 }
93
94 Pair<?, ?> other = (Pair<?, ?>) obj;
95
96 if (!fFirst.equals(other.fFirst)) {
97 return false;
98 }
99 if (!fSecond.equals(other.fSecond)) {
100 return false;
101 }
102 return true;
103 }
104
105 @Override
106 public String toString() {
107 return "(" + fFirst + ", " + fSecond + ")"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
108 }
109
110 }
This page took 0.04481 seconds and 4 git commands to generate.