Copyright header update, 2015 edition
[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 * @since 2.0
29 */
30 public class Pair<A, B> {
31
32 /**
33 * A reference to the first object.
34 */
35 private final A fFirst;
36 /**
37 * A reference to the second object.
38 */
39 private final B fSecond;
40
41 /**
42 * Constructor.
43 * @param first
44 * The pair's first object.
45 * @param second
46 * The pair's second object.
47 */
48 public Pair(A first, B second) {
49 fFirst = first;
50 fSecond = second;
51 }
52
53
54 /**
55 * Returns a reference to the pair's first object.
56 *
57 * @return A reference to the pair's first object.
58 */
59 public A getFirst() {
60 return fFirst;
61 }
62
63 /**
64 * Returns a reference to the pair's second object.
65 *
66 * @return A reference to the pair's second object.
67 */
68 public B getSecond() {
69 return fSecond;
70 }
71
72 @Override
73 public int hashCode() {
74 final int prime = 31;
75 int result = 1;
76 result = prime * result + fFirst.hashCode();
77 result = prime * result + fSecond.hashCode();
78 return result;
79 }
80
81 @Override
82 public boolean equals(@Nullable Object obj) {
83 if (this == obj) {
84 return true;
85 }
86
87 if (obj == null) {
88 return false;
89 }
90
91 if (getClass() != obj.getClass()) {
92 return false;
93 }
94
95 Pair<?, ?> other = (Pair<?, ?>) obj;
96
97 if (!fFirst.equals(other.fFirst)) {
98 return false;
99 }
100 if (!fSecond.equals(other.fSecond)) {
101 return false;
102 }
103 return true;
104 }
105
106 @Override
107 public String toString() {
108 return "(" + fFirst + ", " + fSecond + ")"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
109 }
110
111 }
This page took 0.039166 seconds and 6 git commands to generate.