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.Map;
020import java.util.Objects;
021
022/**
023 * A mutable pair consisting of two {@link Object} elements.
024 *
025 * <p>
026 * Not #ThreadSafe#
027 * </p>
028 *
029 * @param <L> The left element type.
030 * @param <R> The right element type.
031 * @since 3.0
032 */
033public class MutablePair<L, R> extends Pair<L, R> {
034
035    /**
036     * An empty array.
037     * <p>
038     * Consider using {@link #emptyArray()} to avoid generics warnings.
039     * </p>
040     *
041     * @since 3.10
042     */
043    public static final MutablePair<?, ?>[] EMPTY_ARRAY = {};
044
045    /** Serialization version */
046    private static final long serialVersionUID = 4954918890077093841L;
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 <R> The right element type.
053     * @return The empty array singleton that can be assigned without compiler warning.
054     * @since 3.10
055     */
056    @SuppressWarnings("unchecked")
057    public static <L, R> MutablePair<L, R>[] emptyArray() {
058        return (MutablePair<L, R>[]) EMPTY_ARRAY;
059    }
060
061    /**
062     * Creates a mutable pair of two objects inferring the generic types.
063     *
064     * @param <L> The left element type.
065     * @param <R> The right element type.
066     * @param left  The left element, may be null.
067     * @param right  The right element, may be null.
068     * @return A mutable pair formed from the two parameters, not null.
069     */
070    public static <L, R> MutablePair<L, R> of(final L left, final R right) {
071        return new MutablePair<>(left, right);
072    }
073
074    /**
075     * Creates a mutable pair from a map entry.
076     *
077     * @param <L> The left element type.
078     * @param <R> The right element type.
079     * @param pair The existing map entry.
080     * @return A mutable pair formed from the map entry.
081     */
082    public static <L, R> MutablePair<L, R> of(final Map.Entry<L, R> pair) {
083        final L left;
084        final R right;
085        if (pair != null) {
086            left = pair.getKey();
087            right = pair.getValue();
088        } else {
089            left = null;
090            right = null;
091        }
092        return new MutablePair<>(left, right);
093    }
094
095    /**
096     * Creates a mutable pair of two non-null objects inferring the generic types.
097     *
098     * @param <L> The left element type.
099     * @param <R> The right element type.
100     * @param left  The left element, may not be null.
101     * @param right  The right element, may not be null.
102     * @return A mutable pair formed from the two parameters, not null.
103     * @throws NullPointerException Thrown if any input is null.
104     * @since 3.13.0
105     */
106    public static <L, R> MutablePair<L, R> ofNonNull(final L left, final R right) {
107        return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right"));
108    }
109
110    /**
111     * Creates a mutable pair from a map entry.
112     *
113     * @param <L> The left element type
114     * @param <R> The right element type
115     * @param pair The existing map entry.
116     * @return A mutable pair formed from the map entry
117     * @throws NullPointerException Thrown if the pair is null.
118     * @since 3.20
119     */
120    public static <L, R> MutablePair<L, R> ofNonNull(final Map.Entry<L, R> pair) {
121        return of(Objects.requireNonNull(pair, "pair"));
122    }
123
124    /** Left object. */
125    public L left;
126
127    /** Right object. */
128    public R right;
129
130    /**
131     * Create a new pair instance of two nulls.
132     */
133    public MutablePair() {
134    }
135
136    /**
137     * Create a new pair instance.
138     *
139     * @param left  The left value, may be null.
140     * @param right  The right value, may be null.
141     */
142    public MutablePair(final L left, final R right) {
143        this.left = left;
144        this.right = right;
145    }
146
147    /**
148     * {@inheritDoc}
149     */
150    @Override
151    public L getLeft() {
152        return left;
153    }
154
155    /**
156     * {@inheritDoc}
157     */
158    @Override
159    public R getRight() {
160        return right;
161    }
162
163    /**
164     * Sets the left element of the pair.
165     *
166     * @param left  The new value of the left element, may be null.
167     */
168    public void setLeft(final L left) {
169        this.left = left;
170    }
171
172    /**
173     * Sets the right element of the pair.
174     *
175     * @param right  The new value of the right element, may be null.
176     */
177    public void setRight(final R right) {
178        this.right = right;
179    }
180
181    /**
182     * Sets the {@code Map.Entry} value.
183     * This sets the right element of the pair.
184     *
185     * @param value  The right value to set, not null.
186     * @return The old value for the right element.
187     */
188    @Override
189    public R setValue(final R value) {
190        final R result = getRight();
191        setRight(value);
192        return result;
193    }
194
195}