    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
    019/**
    020 * <p>An immutable triple consisting of three {@code Object} elements.</p>
    021 *
    022 * <p>Although the implementation is immutable, there is no restriction on the objects
    023 * that may be stored. If mutable objects are stored in the triple, then the triple
    024 * itself effectively becomes mutable. The class is also {@code final}, so a subclass
    025 * can not add undesirable behavior.</p>
    026 *
    027 * <p>#ThreadSafe# if all three objects are thread-safe</p>
    028 *
    029 * @param <L> the left element type
    030 * @param <M> the middle element type
    031 * @param <R> the right element type
    032 *
    033 * @since 3.2
    034 */
    035public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> {
    036
    037    /**
    038     * An empty array.
    039     * <p>
    040     * Consider using {@link #emptyArray()} to avoid generics warnings.
    041     * </p>
    042     *
    043     * @since 3.10.
    044     */
    045    public static final ImmutableTriple<?, ?, ?>[] EMPTY_ARRAY = new ImmutableTriple[0];
    046
    047    /**
    048     * An immutable triple of nulls.
    049     */
    050    // This is not defined with generics to avoid warnings in call sites.
    051    @SuppressWarnings("rawtypes")
    052    private static final ImmutableTriple NULL = of(null, null, null);
    053
    054    /** Serialization version */
    055    private static final long serialVersionUID = 1L;
    056
    057    /**
    058     * Returns the empty array singleton that can be assigned without compiler warning.
    059     *
    060     * @param <L> the left element type
    061     * @param <M> the middle element type
    062     * @param <R> the right element type
    063     * @return the empty array singleton that can be assigned without compiler warning.
    064     *
    065     * @since 3.10.
    066     */
    067    @SuppressWarnings("unchecked")
    068    public static <L, M, R> ImmutableTriple<L, M, R>[] emptyArray() {
    069        return (ImmutableTriple<L, M, R>[]) EMPTY_ARRAY;
    070    }
    071
    072    /**
    073     * Returns an immutable triple of nulls.
    074     *
    075     * @param <L> the left element of this triple. Value is {@code null}.
    076     * @param <M> the middle element of this triple. Value is {@code null}.
    077     * @param <R> the right element of this triple. Value is {@code null}.
    078     * @return an immutable triple of nulls.
    079     * @since 3.6
    080     */
    081    public static <L, M, R> ImmutableTriple<L, M, R> nullTriple() {
    082        return NULL;
    083    }
    084
    085    /**
    086     * <p>Obtains an immutable triple of three objects inferring the generic types.</p>
    087     *
    088     * <p>This factory allows the triple to be created using inference to
    089     * obtain the generic types.</p>
    090     *
    091     * @param <L> the left element type
    092     * @param <M> the middle element type
    093     * @param <R> the right element type
    094     * @param left  the left element, may be null
    095     * @param middle  the middle element, may be null
    096     * @param right  the right element, may be null
    097     * @return a triple formed from the three parameters, not null
    098     */
    099    public static <L, M, R> ImmutableTriple<L, M, R> of(final L left, final M middle, final R right) {
    100        return new ImmutableTriple<>(left, middle, right);
    101    }
    102    /** Left object */
    103    public final L left;
    104    /** Middle object */
    105    public final M middle;
    106
    107    /** Right object */
    108    public final R right;
    109
    110    /**
    111     * Create a new triple instance.
    112     *
    113     * @param left  the left value, may be null
    114     * @param middle the middle value, may be null
    115     * @param right  the right value, may be null
    116     */
    117    public ImmutableTriple(final L left, final M middle, final R right) {
    118        this.left = left;
    119        this.middle = middle;
    120        this.right = right;
    121    }
    122
    123    //-----------------------------------------------------------------------
    124    /**
    125     * {@inheritDoc}
    126     */
    127    @Override
    128    public L getLeft() {
    129        return left;
    130    }
    131
    132    /**
    133     * {@inheritDoc}
    134     */
    135    @Override
    136    public M getMiddle() {
    137        return middle;
    138    }
    139
    140    /**
    141     * {@inheritDoc}
    142     */
    143    @Override
    144    public R getRight() {
    145        return right;
    146    }
    147}
    148