/*******************************************************************************
- * Copyright (c) 2012 Ericsson
+ * Copyright (c) 2012, 2015 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* Philippe Sawicki (INF4990.A2010@gmail.com) - Initial API and implementation
* Mathieu Denis (mathieu.denis55@gmail.com) - Refactored code
* Bernd Hufmann - Integrated to TMF, fixed hashCode() and equals() methods
+ * Alexandre Montplaisir - Made non-null and immutable
*******************************************************************************/
package org.eclipse.tracecompass.tmf.core.util;
+import org.eclipse.jdt.annotation.Nullable;
+
/**
* Pair utility class, encapsulates a pair of objects.
*
* The type of the second object.
*
* @author Philippe Sawicki
- * @since 2.0
*/
public class Pair<A, B> {
/**
* A reference to the first object.
*/
- protected A fFirst;
+ private final A fFirst;
/**
* A reference to the second object.
*/
- protected B fSecond;
+ private final B fSecond;
/**
* Constructor.
fSecond = second;
}
+
/**
- * Constructor.
+ * Returns a reference to the pair's first object.
+ *
+ * @return A reference to the pair's first object.
*/
- public Pair() {
- this(null, null);
+ public A getFirst() {
+ return fFirst;
}
/**
- * Pair hash code.
+ * Returns a reference to the pair's second object.
+ *
+ * @return A reference to the pair's second object.
*/
+ public B getSecond() {
+ return fSecond;
+ }
+
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((fFirst == null) ? 0 : fFirst.hashCode());
- result = prime * result + ((fSecond == null) ? 0 : fSecond.hashCode());
+ result = prime * result + fFirst.hashCode();
+ result = prime * result + fSecond.hashCode();
return result;
}
- /**
- * Object comparison.
- */
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
Pair<?, ?> other = (Pair<?, ?>) obj;
- if (fFirst == null) {
- if (other.fFirst != null) {
- return false;
- }
- } else if (!fFirst.equals(other.fFirst)) {
+ if (!fFirst.equals(other.fFirst)) {
return false;
}
- if (fSecond == null) {
- if (other.fSecond != null) {
- return false;
- }
- } else if (!fSecond.equals(other.fSecond)) {
+ if (!fSecond.equals(other.fSecond)) {
return false;
}
return true;
}
- /**
- * Object to string.
- */
@Override
public String toString() {
return "(" + fFirst + ", " + fSecond + ")"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
}
- /**
- * Returns a reference to the pair's first object.
- * @return A reference to the pair's first object.
- */
- public A getFirst() {
- return fFirst;
- }
-
- /**
- * Sets the pair's first object.
- * @param first
- * The pair's first object.
- */
- public void setFirst(A first) {
- fFirst = first;
- }
-
- /**
- * Returns a reference to the pair's second object.
- * @return A reference to the pair's second object.
- */
- public B getSecond() {
- return fSecond;
- }
-
- /**
- * Sets the pair's second object.
- * @param second
- * The pair's second object.
- */
- public void setSecond(B second) {
- fSecond = second;
- }
}
\ No newline at end of file