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