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
095    /** Middle object. */
096    public M middle;
097
098    /** Right object. */
099    public R right;
100
101    /**
102     * Create a new triple instance of three nulls.
103     */
104    public MutableTriple() {
105    }
106
107    /**
108     * Create a new triple instance.
109     *
110     * @param left  the left value, may be null.
111     * @param middle  the middle value, may be null.
112     * @param right  the right value, may be null.
113     */
114    public MutableTriple(final L left, final M middle, final R right) {
115        this.left = left;
116        this.middle = middle;
117        this.right = right;
118    }
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public L getLeft() {
125        return left;
126    }
127
128    /**
129     * {@inheritDoc}
130     */
131    @Override
132    public M getMiddle() {
133        return middle;
134    }
135
136    /**
137     * {@inheritDoc}
138     */
139    @Override
140    public R getRight() {
141        return right;
142    }
143
144    /**
145     * Sets the left element of the triple.
146     *
147     * @param left  the new value of the left element, may be null.
148     */
149    public void setLeft(final L left) {
150        this.left = left;
151    }
152
153    /**
154     * Sets the middle element of the triple.
155     *
156     * @param middle  the new value of the middle element, may be null.
157     */
158    public void setMiddle(final M middle) {
159        this.middle = middle;
160    }
161
162    /**
163     * Sets the right element of the triple.
164     *
165     * @param right  the new value of the right element, may be null.
166     */
167    public void setRight(final R right) {
168        this.right = right;
169    }
170}
171