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
019/**
020 * <p>A mutable pair consisting of two {@code Object} elements.</p>
021 *
022 * <p>Not #ThreadSafe#</p>
023 *
024 * @param <L> the left element type
025 * @param <R> the right element type
026 *
027 * @since Lang 3.0
028 */
029public class MutablePair<L, R> extends Pair<L, R> {
030
031    /** Serialization version */
032    private static final long serialVersionUID = 4954918890077093841L;
033
034    /** Left object */
035    public L left;
036    /** Right object */
037    public R right;
038
039    /**
040     * <p>Obtains an immutable pair of from two objects inferring the generic types.</p>
041     *
042     * <p>This factory allows the pair to be created using inference to
043     * obtain the generic types.</p>
044     *
045     * @param <L> the left element type
046     * @param <R> the right element type
047     * @param left  the left element, may be null
048     * @param right  the right element, may be null
049     * @return a pair formed from the two parameters, not null
050     */
051    public static <L, R> MutablePair<L, R> of(final L left, final R right) {
052        return new MutablePair<>(left, right);
053    }
054
055    /**
056     * Create a new pair instance of two nulls.
057     */
058    public MutablePair() {
059        super();
060    }
061
062    /**
063     * Create a new pair instance.
064     *
065     * @param left  the left value, may be null
066     * @param right  the right value, may be null
067     */
068    public MutablePair(final L left, final R right) {
069        super();
070        this.left = left;
071        this.right = right;
072    }
073
074    //-----------------------------------------------------------------------
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public L getLeft() {
080        return left;
081    }
082
083    /**
084     * Sets the left element of the pair.
085     *
086     * @param left  the new value of the left element, may be null
087     */
088    public void setLeft(final L left) {
089        this.left = left;
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public R getRight() {
097        return right;
098    }
099
100    /**
101     * Sets the right element of the pair.
102     *
103     * @param right  the new value of the right element, may be null
104     */
105    public void setRight(final R right) {
106        this.right = right;
107    }
108
109    /**
110     * Sets the {@code Map.Entry} value.
111     * This sets the right element of the pair.
112     *
113     * @param value  the right value to set, not null
114     * @return the old value for the right element
115     */
116    @Override
117    public R setValue(final R value) {
118        final R result = getRight();
119        setRight(value);
120        return result;
121    }
122
123}