aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/collections/Tuple2.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/src/main/java/com/yahoo/collections/Tuple2.java
Publish
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/collections/Tuple2.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/collections/Tuple2.java65
1 files changed, 65 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/Tuple2.java b/vespajlib/src/main/java/com/yahoo/collections/Tuple2.java
new file mode 100644
index 00000000000..4a817381f9c
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/collections/Tuple2.java
@@ -0,0 +1,65 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.collections;
+
+/**
+ * A representation of a pair of values, typically of different types.
+ *
+ * <p>
+ * This class is to avoid littering a class with thin wrapper objects for
+ * passing around e.g. the state of an operation and the result value. Using
+ * this class may be correct, but it is a symptom that you may want to redesign
+ * your code. (Should you pass mutable objects to the method instead? Create a
+ * new class and do the work inside that class instead? Etc.)
+ * </p>
+ *
+ * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
+ */
+public final class Tuple2<T1, T2> {
+
+ public final T1 first;
+ public final T2 second;
+
+ public Tuple2(final T1 first, final T2 second) {
+ this.first = first;
+ this.second = second;
+ }
+
+ /**
+ * hashCode() will always throw UnsupportedOperationException. The reason is
+ * this class is not meant for being put in Container implementation or
+ * similar use where Java generics will lead to a type unsafe maintenance
+ * nightmare.
+ *
+ * @throws UnsupportedOperationException
+ * will always throw this when invoked
+ */
+ @Override
+ public int hashCode() {
+ throw new UnsupportedOperationException(
+ "com.yahoo.collections.Tuple2<T1, T2> does not support equals(Object) by design. Refer to JavaDoc for details.");
+ }
+
+ /**
+ * equals(Object) will always throw UnsupportedOperationException. The
+ * intention is always using the objects contained in the tuple directly.
+ *
+ * @param obj
+ * ignored
+ * @throws UnsupportedOperationException
+ * will always throw this when invoked
+ */
+ @Override
+ public boolean equals(final Object obj) {
+ throw new UnsupportedOperationException(
+ "com.yahoo.collections.Tuple2<T1, T2> does not support equals(Object) by design. Refer to JavaDoc for details.");
+ }
+
+ /**
+ * Human readable string representation which invokes the contained
+ * instances' toString() implementation.
+ */
+ @Override
+ public String toString() {
+ return "Tuple2(" + first + ", " + second + ")";
+ }
+}