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