From 72231250ed81e10d66bfe70701e64fa5fe50f712 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Wed, 15 Jun 2016 23:09:44 +0200 Subject: Publish --- .../main/java/com/yahoo/collections/Tuple2.java | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 vespajlib/src/main/java/com/yahoo/collections/Tuple2.java (limited to 'vespajlib/src/main/java/com/yahoo/collections/Tuple2.java') 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. + * + *

+ * 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.) + *

+ * + * @author Steinar Knutsen + */ +public final class Tuple2 { + + 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 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 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 + ")"; + } +} -- cgit v1.2.3