commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Axel Fontaine (JIRA)" <j...@apache.org>
Subject [jira] Created: (LANG-675) Add Triplet class (similar to Pair)
Date Tue, 01 Feb 2011 09:45:29 GMT
Add Triplet class (similar to Pair)
-----------------------------------

                 Key: LANG-675
                 URL: https://issues.apache.org/jira/browse/LANG-675
             Project: Commons Lang
          Issue Type: New Feature
          Components: lang.*
    Affects Versions: 3.0
            Reporter: Axel Fontaine


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.

-- 
This message is automatically generated by JIRA.
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message