    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 *      https://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.util.Objects;
    020
    021/**
    022 * A mutable triple consisting of three {@link Object} elements.
    023 *
    024 * <p>Not #ThreadSafe#</p>
    025 *
    026 * @param <L> the left element type.
    027 * @param <M> the middle element type.
    028 * @param <R> the right element type.
    029 * @since 3.2
    030 */
    031public class MutableTriple<L, M, R> extends Triple<L, M, R> {
    032
    033    /**
    034     * The empty array singleton.
    035     * <p>
    036     * Consider using {@link #emptyArray()} to avoid generics warnings.
    037     * </p>
    038     *
    039     * @since 3.10
    040     */
    041    public static final MutableTriple<?, ?, ?>[] EMPTY_ARRAY = {};
    042
    043    /** Serialization version */
    044    private static final long serialVersionUID = 1L;
    045
    046    /**
    047     * Returns the empty array singleton that can be assigned without compiler warning.
    048     *
    049     * @param <L> the left element type.
    050     * @param <M> the middle element type.
    051     * @param <R> the right element type.
    052     * @return the empty array singleton that can be assigned without compiler warning.
    053     * @since 3.10
    054     */
    055    @SuppressWarnings("unchecked")
    056    public static <L, M, R> MutableTriple<L, M, R>[] emptyArray() {
    057        return (MutableTriple<L, M, R>[]) EMPTY_ARRAY;
    058    }
    059
    060    /**
    061     * Obtains a mutable triple of three objects inferring the generic types.
    062     *
    063     * <p>This factory allows the triple to be created using inference to
    064     * obtain the generic types.</p>
    065     *
    066     * @param <L> the left element type.
    067     * @param <M> the middle element type.
    068     * @param <R> the right element type.
    069     * @param left  the left element, may be null.
    070     * @param middle  the middle element, may be null.
    071     * @param right  the right element, may be null.
    072     * @return a triple formed from the three parameters, not null.
    073     */
    074    public static <L, M, R> MutableTriple<L, M, R> of(final L left, final M middle, final R right) {
    075        return new MutableTriple<>(left, middle, right);
    076    }
    077
    078    /**
    079     * Obtains a mutable triple of three non-null objects inferring the generic types.
    080     *
    081     * <p>This factory allows the triple to be created using inference to
    082     * obtain the generic types.</p>
    083     *
    084     * @param <L> the left element type.
    085     * @param <M> the middle element type.
    086     * @param <R> the right element type.
    087     * @param left  the left element, may not be null.
    088     * @param middle  the middle element, may not be null.
    089     * @param right  the right element, may not be null.
    090     * @return a triple formed from the three parameters, not null.
    091     * @throws NullPointerException if any input is null.
    092     * @since 3.13.0
    093     */
    094    public static <L, M, R> MutableTriple<L, M, R> ofNonNull(final L left, final M middle, final R right) {
    095        return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(middle, "middle"), Objects.requireNonNull(right, "right"));
    096    }
    097
    098    /** Left object. */
    099    public L left;
    100    /** Middle object. */
    101    public M middle;
    102
    103    /** Right object. */
    104    public R right;
    105
    106    /**
    107     * Create a new triple instance of three nulls.
    108     */
    109    public MutableTriple() {
    110    }
    111
    112    /**
    113     * Create a new triple instance.
    114     *
    115     * @param left  the left value, may be null.
    116     * @param middle  the middle value, may be null.
    117     * @param right  the right value, may be null.
    118     */
    119    public MutableTriple(final L left, final M middle, final R right) {
    120        this.left = left;
    121        this.middle = middle;
    122        this.right = right;
    123    }
    124
    125    /**
    126     * {@inheritDoc}
    127     */
    128    @Override
    129    public L getLeft() {
    130        return left;
    131    }
    132
    133    /**
    134     * {@inheritDoc}
    135     */
    136    @Override
    137    public M getMiddle() {
    138        return middle;
    139    }
    140
    141    /**
    142     * {@inheritDoc}
    143     */
    144    @Override
    145    public R getRight() {
    146        return right;
    147    }
    148
    149    /**
    150     * Sets the left element of the triple.
    151     *
    152     * @param left  the new value of the left element, may be null.
    153     */
    154    public void setLeft(final L left) {
    155        this.left = left;
    156    }
    157
    158    /**
    159     * Sets the middle element of the triple.
    160     *
    161     * @param middle  the new value of the middle element, may be null.
    162     */
    163    public void setMiddle(final M middle) {
    164        this.middle = middle;
    165    }
    166
    167    /**
    168     * Sets the right element of the triple.
    169     *
    170     * @param right  the new value of the right element, may be null.
    171     */
    172    public void setRight(final R right) {
    173        this.right = right;
    174    }
    175}
    176