Skip to content

Instantly share code, notes, and snippets.

@danielgallegovico
Last active June 16, 2016 11:01
Show Gist options
  • Save danielgallegovico/ec4cb2338be77d263ec89d971a00ddb6 to your computer and use it in GitHub Desktop.
Save danielgallegovico/ec4cb2338be77d263ec89d971a00ddb6 to your computer and use it in GitHub Desktop.
Container to ease passing around a tuple of three objects.
/*
* Copyright (C) 2016 Daniel Gallego Vico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Objects;
/**
* Container to ease passing around a tuple of three objects.
* This object provides a sensible implementation of equals(),
* returning true if equals() is true on each of the contained objects.
*
* @param <F> first object type to store in the Triple
* @param <S> second object type to store in the Triple
* @param <T> third object type to store in the Triple
*/
public class Triple<F, S, T> {
public final F first;
public final S second;
public final T third;
/**
* Constructor for a Triple
*
* @param first the first object in the Triple
* @param second the second object in the Triple
* @param third the third object in the Triple
*/
public Triple(F first, S second, T third) {
this.first = first;
this.second = second;
this.third = third;
}
/**
* Checks the two Triple objects for equality by delegating to their respective
* {@link Object#equals(Object)} methods.
*
* @param o the {@link Triple} to which this one is to be checked for equality
* @return true if the underlying objects of the Triple are considered equal
*/
@Override
public boolean equals(Object o) {
if(!(o instanceof Triple)) {
return false;
}
Triple<?,?,?> t = (Triple<?,?,?>) o;
return Objects.equals(t.first, first)
&& Objects.equals(t.second, second)
&& Objects.equals(t.third, third);
}
/**
* Compute a hash code using the hash codes of the underlying objects.
*
* @return a hashcode on the Triple
*/
@Override
public int hashCode() {
return (first == null ? 0 : first.hashCode())
^ (second == null ? 0 : second.hashCode())
^ (third == null ? 0 : third.hashCode());
}
/**
* Convenience method for creating an appropriately typed triple.
*
* @param a the first object in the Triple
* @param b the second object in the Triple
* @param c the third object in the Triple
* @param <A> first object type of the Triple
* @param <B> second object type of the Triple
* @param <C> third object type of the Triple
*
* @return a Triple that is templatized with the types of a, b and c
*/
public static <A, B, C> Triple<A, B, C> create(A a, B b, C c) {
return new Triple<>(a, b, c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment