Description
Similar to Pair, a Triplet is a common need.
Here is an adaptation of the commons-lang Pair that also takes a middle argument:
import java.io.Serializable;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
- Similar to Pair, using 3 instead of 2 Objects.
- @param <L>
- The "left" object of the triplet.
- @param <M>
- The "middle" object of the triplet.
- @param <R>
- The "right" object of the triplet.
*/
public final class Triplet<L, M, R> implements Serializable {
/** Serialization version */
private static final long serialVersionUID = -8359217847916721612L;
/**
- Static fluent creation method for a Pair<L, R>: <code>Pair.of(left, right)</code>
- @param <L>
- The "left" object of the triplet.
- @param <M>
- The "middle" object of the triplet.
- @param <R>
- The "right" object of the triplet.
- @param left
- The "left" object of the triplet.
- @param middle
- The "middle" object of the triplet.
- @param right
- The "right" object of the triplet.
- @return The newly created triplet.
*/
public static <L, M, R> Triplet<L, M, R> of(final L left, final M middle, final R right) { return new Triplet<L, M, R>(left, middle, right); }
/** Left object */
public final L left;
/** Middle object */
public final M middle;
/** Right object */
public final R right;
/**
- Create a new Triplet instance
- @param left
- the left object
- @param middle
- The "middle" object of the triplet.
- @param right
- the right object
*/
public Triplet(final L left, final M middle, final R right) { this.left = left; this.middle = middle; this.right = right; }
/**
- {@inheritDoc}
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
if (obj == this) { return true; }
if (obj instanceof Triplet<?, ?, ?> == false) { return false; }
final Triplet<?, ?, ?> other = (Triplet<?, ?, ?>) obj;
return ObjectUtils.equals(this.left, other.left) && ObjectUtils.equals(this.middle, other.middle)
&& ObjectUtils.equals(this.right, other.right);
}
/**
* {@inheritDoc} - @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() { return new HashCodeBuilder().append(this.left).append(this.middle).append(this.right).toHashCode(); }
/**
- Returns a String representation of the Pair in the form: (L,R)
- {@inheritDoc}
- @see java.lang.Object#toString()
*/
@Override
public String toString() { return new StringBuilder().append("(").append(this.left).append(",").append(this.middle).append(",") .append(this.right).append(")").toString(); }
Feel free to use it if you wish to include it.