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