Make Pair class immutable when user supplies immutable objects
- Make fields private
- Add getters
Change-Id: I4bdc63316e21a88f9026cd42e424bf94873d02bb
diff --git a/src/main/java/net/onrc/onos/core/util/Pair.java b/src/main/java/net/onrc/onos/core/util/Pair.java
index 6fe8101..c5ab775 100644
--- a/src/main/java/net/onrc/onos/core/util/Pair.java
+++ b/src/main/java/net/onrc/onos/core/util/Pair.java
@@ -4,10 +4,16 @@
/**
* A generic class representing a pair of two values.
+ *
+ * If a user supplies immutable objects, the pair become immutable.
+ * Otherwise, the pair become mutable.
+ *
+ * @param <F> the type of the first value
+ * @param <S> the type type of the second value
*/
public class Pair<F, S> {
- public F first; // The first value in the pair
- public S second; // The second value in the pair
+ private final F first; // The first value in the pair
+ private final S second; // The second value in the pair
/**
* Constructor for a pair of two values.
@@ -20,6 +26,24 @@
this.second = second;
}
+ /**
+ * Get the first value of the Pair.
+ *
+ * @return the first value of the Pair.
+ */
+ public F getFirst() {
+ return first;
+ }
+
+ /**
+ * Get the second value of the Pair.
+ *
+ * @return the second value of the Pair.
+ */
+ public S getSecond() {
+ return second;
+ }
+
@Override
public String toString() {
return String.format("<%s, %s>", first, second);