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     * @param <L> the left element type.
064     * @param <M> the middle element type.
065     * @param <R> the right element type.
066     * @param left  the left element, may be null.
067     * @param middle  the middle element, may be null.
068     * @param right  the right element, may be null.
069     * @return a mutable triple formed from the three parameters, not null.
070     */
071    public static <L, M, R> MutableTriple<L, M, R> of(final L left, final M middle, final R right) {
072        return new MutableTriple<>(left, middle, right);
073    }
074
075    /**
076     * Obtains a mutable triple of three non-null objects inferring the generic types.
077     *
078     * @param <L> the left element type.
079     * @param <M> the middle element type.
080     * @param <R> the right element type.
081     * @param left  the left element, may not be null.
082     * @param middle  the middle element, may not be null.
083     * @param right  the right element, may not be null.
084     * @return a mutable triple formed from the three parameters, not null.
085     * @throws NullPointerException if any input is null.
086     * @since 3.13.0
087     */
088    public static <L, M, R> MutableTriple<L, M, R> ofNonNull(final L left, final M middle, final R right) {
089        return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(middle, "middle"), Objects.requireNonNull(right, "right"));
090    }
091
092    /** Left object. */
093    public L left;
094    /** Middle object. */
095    public M middle;
096
097    /** Right object. */
098    public R right;
099
100    /**
101     * Create a new triple instance of three nulls.
102     */
103    public MutableTriple() {
104    }
105
106    /**
107     * Create a new triple instance.
108     *
109     * @param left  the left value, may be null.
110     * @param middle  the middle value, may be null.
111     * @param right  the right value, may be null.
112     */
113    public MutableTriple(final L left, final M middle, final R right) {
114        this.left = left;
115        this.middle = middle;
116        this.right = right;
117    }
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public L getLeft() {
124        return left;
125    }
126
127    /**
128     * {@inheritDoc}
129     */
130    @Override
131    public M getMiddle() {
132        return middle;
133    }
134
135    /**
136     * {@inheritDoc}
137     */
138    @Override
139    public R getRight() {
140        return right;
141    }
142
143    /**
144     * Sets the left element of the triple.
145     *
146     * @param left  the new value of the left element, may be null.
147     */
148    public void setLeft(final L left) {
149        this.left = left;
150    }
151
152    /**
153     * Sets the middle element of the triple.
154     *
155     * @param middle  the new value of the middle element, may be null.
156     */
157    public void setMiddle(final M middle) {
158        this.middle = middle;
159    }
160
161    /**
162     * Sets the right element of the triple.
163     *
164     * @param right  the new value of the right element, may be null.
165     */
166    public void setRight(final R right) {
167        this.right = right;
168    }
169}
170