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;
020
021import org.apache.commons.lang3.ObjectUtils;
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 from 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<L, M, R>(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    @SuppressWarnings( "deprecation" ) // ObjectUtils.equals(Object, Object) has been deprecated in 3.2
108    @Override
109    public boolean equals(final Object obj) {
110        if (obj == this) {
111            return true;
112        }
113        if (obj instanceof Triple<?, ?, ?>) {
114            final Triple<?, ?, ?> other = (Triple<?, ?, ?>) obj;
115            return ObjectUtils.equals(getLeft(), other.getLeft())
116                && ObjectUtils.equals(getMiddle(), other.getMiddle())
117                && ObjectUtils.equals(getRight(), other.getRight());
118        }
119        return false;
120    }
121
122    /**
123     * <p>Returns a suitable hash code.</p>
124     *
125     * @return the hash code
126     */
127    @Override
128    public int hashCode() {
129        return (getLeft() == null ? 0 : getLeft().hashCode()) ^
130            (getMiddle() == null ? 0 : getMiddle().hashCode()) ^
131            (getRight() == null ? 0 : getRight().hashCode());
132    }
133
134    /**
135     * <p>Returns a String representation of this triple using the format {@code ($left,$middle,$right)}.</p>
136     *
137     * @return a string describing this object, not null
138     */
139    @Override
140    public String toString() {
141        return "(" + getLeft() + "," + getMiddle() + "," + getRight() + ")";
142    }
143
144    /**
145     * <p>Formats the receiver using the given format.</p>
146     *
147     * <p>This uses {@link java.util.Formattable} to perform the formatting. Three variables may
148     * be used to embed the left and right elements. Use {@code %1$s} for the left
149     * element, {@code %2$s} for the middle and {@code %3$s} for the right element.
150     * The default format used by {@code toString()} is {@code (%1$s,%2$s,%3$s)}.</p>
151     *
152     * @param format  the format string, optionally containing {@code %1$s}, {@code %2$s} and {@code %3$s}, not null
153     * @return the formatted string, not null
154     */
155    public String toString(final String format) {
156        return String.format(format, getLeft(), getMiddle(), getRight());
157    }
158
159}
160