    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.tuple;
    018
    019import java.io.Serializable;
    020import java.util.Objects;
    021
    022import org.apache.commons.lang3.builder.CompareToBuilder;
    023
    024/**
    025 * <p>A triple consisting of three elements.</p>
    026 *
    027 * <p>This class is an abstract implementation defining the basic API.
    028 * It refers to the elements as 'left', 'middle' and 'right'.</p>
    029 *
    030 * <p>Subclass implementations may be mutable or immutable.
    031 * However, there is no restriction on the type of the stored objects that may be stored.
    032 * If mutable objects are stored in the triple, then the triple itself effectively becomes mutable.</p>
    033 *
    034 * @param <L> the left element type
    035 * @param <M> the middle element type
    036 * @param <R> the right element type
    037 *
    038 * @since 3.2
    039 */
    040public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Serializable {
    041
    042    /** Serialization version */
    043    private static final long serialVersionUID = 1L;
    044
    045    /**
    046     * <p>Obtains an immutable triple of three objects inferring the generic types.</p>
    047     *
    048     * <p>This factory allows the triple to be created using inference to
    049     * obtain the generic types.</p>
    050     *
    051     * @param <L> the left element type
    052     * @param <M> the middle element type
    053     * @param <R> the right element type
    054     * @param left  the left element, may be null
    055     * @param middle the middle element, may be null
    056     * @param right  the right element, may be null
    057     * @return a triple formed from the three parameters, not null
    058     */
    059    public static <L, M, R> Triple<L, M, R> of(final L left, final M middle, final R right) {
    060        return new ImmutableTriple<>(left, middle, right);
    061    }
    062
    063    //-----------------------------------------------------------------------
    064    /**
    065     * <p>Gets the left element from this triple.</p>
    066     *
    067     * @return the left element, may be null
    068     */
    069    public abstract L getLeft();
    070
    071    /**
    072     * <p>Gets the middle element from this triple.</p>
    073     *
    074     * @return the middle element, may be null
    075     */
    076    public abstract M getMiddle();
    077
    078    /**
    079     * <p>Gets the right element from this triple.</p>
    080     *
    081     * @return the right element, may be null
    082     */
    083    public abstract R getRight();
    084
    085    //-----------------------------------------------------------------------
    086    /**
    087     * <p>Compares the triple based on the left element, followed by the middle element,
    088     * finally the right element.
    089     * The types must be {@code Comparable}.</p>
    090     *
    091     * @param other  the other triple, not null
    092     * @return negative if this is less, zero if equal, positive if greater
    093     */
    094    @Override
    095    public int compareTo(final Triple<L, M, R> other) {
    096      return new CompareToBuilder().append(getLeft(), other.getLeft())
    097          .append(getMiddle(), other.getMiddle())
    098          .append(getRight(), other.getRight()).toComparison();
    099    }
    100
    101    /**
    102     * <p>Compares this triple to another based on the three elements.</p>
    103     *
    104     * @param obj  the object to compare to, null returns false
    105     * @return true if the elements of the triple are equal
    106     */
    107    @Override
    108    public boolean equals(final Object obj) {
    109        if (obj == this) {
    110            return true;
    111        }
    112        if (obj instanceof Triple<?, ?, ?>) {
    113            final Triple<?, ?, ?> other = (Triple<?, ?, ?>) obj;
    114            return Objects.equals(getLeft(), other.getLeft())
    115                && Objects.equals(getMiddle(), other.getMiddle())
    116                && Objects.equals(getRight(), other.getRight());
    117        }
    118        return false;
    119    }
    120
    121    /**
    122     * <p>Returns a suitable hash code.</p>
    123     *
    124     * @return the hash code
    125     */
    126    @Override
    127    public int hashCode() {
    128        return (getLeft() == null ? 0 : getLeft().hashCode()) ^
    129            (getMiddle() == null ? 0 : getMiddle().hashCode()) ^
    130            (getRight() == null ? 0 : getRight().hashCode());
    131    }
    132
    133    /**
    134     * <p>Returns a String representation of this triple using the format {@code ($left,$middle,$right)}.</p>
    135     *
    136     * @return a string describing this object, not null
    137     */
    138    @Override
    139    public String toString() {
    140        return "(" + getLeft() + "," + getMiddle() + "," + getRight() + ")";
    141    }
    142
    143    /**
    144     * <p>Formats the receiver using the given format.</p>
    145     *
    146     * <p>This uses {@link java.util.Formattable} to perform the formatting. Three variables may
    147     * be used to embed the left and right elements. Use {@code %1$s} for the left
    148     * element, {@code %2$s} for the middle and {@code %3$s} for the right element.
    149     * The default format used by {@code toString()} is {@code (%1$s,%2$s,%3$s)}.</p>
    150     *
    151     * @param format  the format string, optionally containing {@code %1$s}, {@code %2$s} and {@code %3$s}, not null
    152     * @return the formatted string, not null
    153     */
    154    public String toString(final String format) {
    155        return String.format(format, getLeft(), getMiddle(), getRight());
    156    }
    157
    158}
    159