Commit | Line | Data |
---|---|---|
d4a8d935 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2012, 2015 Ericsson |
d4a8d935 BH |
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 | |
e2659565 | 13 | * Alexandre Montplaisir - Made non-null and immutable |
d4a8d935 | 14 | *******************************************************************************/ |
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.core.util; |
d4a8d935 | 16 | |
e2659565 | 17 | import org.eclipse.jdt.annotation.Nullable; |
5b3fe39a | 18 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
e2659565 | 19 | |
d4a8d935 BH |
20 | /** |
21 | * Pair utility class, encapsulates a pair of objects. | |
22 | * | |
23 | * @param <A> | |
24 | * The type of the first object. | |
25 | * @param <B> | |
26 | * The type of the second object. | |
27 | * | |
28 | * @author Philippe Sawicki | |
d4a8d935 BH |
29 | */ |
30 | public class Pair<A, B> { | |
31 | ||
32 | /** | |
33 | * A reference to the first object. | |
34 | */ | |
e2659565 | 35 | private final A fFirst; |
d4a8d935 BH |
36 | /** |
37 | * A reference to the second object. | |
38 | */ | |
e2659565 | 39 | private final B fSecond; |
d4a8d935 BH |
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 | ||
e2659565 | 53 | |
d4a8d935 | 54 | /** |
e2659565 AM |
55 | * Returns a reference to the pair's first object. |
56 | * | |
57 | * @return A reference to the pair's first object. | |
d4a8d935 | 58 | */ |
e2659565 AM |
59 | public A getFirst() { |
60 | return fFirst; | |
d4a8d935 BH |
61 | } |
62 | ||
63 | /** | |
e2659565 AM |
64 | * Returns a reference to the pair's second object. |
65 | * | |
66 | * @return A reference to the pair's second object. | |
d4a8d935 | 67 | */ |
e2659565 AM |
68 | public B getSecond() { |
69 | return fSecond; | |
70 | } | |
71 | ||
5b3fe39a | 72 | |
d4a8d935 BH |
73 | @Override |
74 | public int hashCode() { | |
75 | final int prime = 31; | |
76 | int result = 1; | |
5b3fe39a AM |
77 | result = prime * result + hashFromNullable(fFirst); |
78 | result = prime * result + hashFromNullable(fSecond); | |
d4a8d935 BH |
79 | return result; |
80 | } | |
81 | ||
5b3fe39a AM |
82 | private static int hashFromNullable(@Nullable Object o) { |
83 | return o == null ? 0 : o.hashCode(); | |
84 | } | |
85 | ||
d4a8d935 | 86 | @Override |
e2659565 | 87 | public boolean equals(@Nullable Object obj) { |
d4a8d935 BH |
88 | if (this == obj) { |
89 | return true; | |
90 | } | |
d4a8d935 BH |
91 | if (obj == null) { |
92 | return false; | |
93 | } | |
d4a8d935 BH |
94 | if (getClass() != obj.getClass()) { |
95 | return false; | |
96 | } | |
d4a8d935 | 97 | Pair<?, ?> other = (Pair<?, ?>) obj; |
5b3fe39a | 98 | if (!NonNullUtils.equalsNullable(other.fFirst, fFirst)) { |
d4a8d935 BH |
99 | return false; |
100 | } | |
5b3fe39a | 101 | if (!NonNullUtils.equalsNullable(other.fSecond, fSecond)) { |
d4a8d935 BH |
102 | return false; |
103 | } | |
104 | return true; | |
105 | } | |
106 | ||
d4a8d935 BH |
107 | @Override |
108 | public String toString() { | |
109 | return "(" + fFirst + ", " + fSecond + ")"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ | |
110 | } | |
d4a8d935 | 111 | } |