Skip to content

Instantly share code, notes, and snippets.

@danielgallegovico
Created June 16, 2016 11:04
Show Gist options
  • Save danielgallegovico/1e01fb733a799369546439ceb25a0b9c to your computer and use it in GitHub Desktop.
Save danielgallegovico/1e01fb733a799369546439ceb25a0b9c to your computer and use it in GitHub Desktop.
Container to ease passing around a tuple of four 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 four objects.
* This object provides a sensible implementation of equals(),
* returning true if equals() is true on each of the contained objects.
*
* @param <A> first object type to store in the Quadruple
* @param <B> second object type to store in the Quadruple
* @param <C> third object type to store in the Quadruple
* @param <D> fourth object type to store in the Quadruple
*/
public class Quadruple<A, B, C, D> {
public final A first;
public final B second;
public final C third;
public final D fourth;
/**
* Constructor for a Quadruple
*
* @param first - the first object in the Quadruple
* @param second - the second object in the Quadruple
* @param third - the third object in the Quadruple
* @param fourth - the fourth object in the Quadruple
*/
public Quadruple(A first, B second, C third, D fourth) {
this.first = first;
this.second = second;
this.third = third;
this.fourth = fourth;
}
/**
* Checks the two objects for equality by delegating to their respective
* {@link Object#equals(Object)} methods.
*
* @param o the {@link Quadruple} to which this one is to be checked for equality
* @return true if the underlying objects of the Quadruple are considered equal
*/
@Override
public boolean equals(Object o) {
if(!(o instanceof Quadruple)) {
return false;
}
Quadruple<?,?,?,?> q = (Quadruple<?,?,?,?>) o;
return Objects.equals(q.first, first)
&& Objects.equals(q.second, second)
&& Objects.equals(q.third, third)
&& Objects.equals(q.fourth, fourth);
}
/**
* Compute a hash code using the hash codes of the underlying objects
*
* @return a hashcode on the Quadruple
*/
@Override
public int hashCode() {
return (first == null ? 0 : first.hashCode())
^ (second == null ? 0 : second.hashCode())
^ (third == null ? 0 : third.hashCode())
^ (fourth == null ? 0 : fourth.hashCode());
}
/**
* Convenience method for creating an appropriately typed Quadruple.
*
* @param a the first object in the Quadruple
* @param b the second object in the Quadruple
* @param c the third object in the Quadruple
* @param d the fourth object in the Quadruple
* @param <A> first object type of the Quadruple
* @param <B> second object type of the Quadruple
* @param <C> third object type of the Quadruple
* @param <D> fourth object type of the Quadruple
*
* @return a Quadruple that is templatized with the types of a, b, c and d
*/
public static <A, B, C, D> Quadruple<A, B, C, D> create(A a, B b, C c, D d) {
return new Quadruple<>(a, b, c, d);
}
}
@IzanRodrigo
Copy link

Kotlin:

data class Quadruple<A, B, C, D>(val a: A, val b: B, val c: C, val d: D)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment