View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.tuple;
18  
19  /**
20   * <p>A mutable pair consisting of two {@code Object} elements.</p>
21   * 
22   * <p>Not #ThreadSafe#</p>
23   *
24   * @param <L> the left element type
25   * @param <R> the right element type
26   *
27   * @since Lang 3.0
28   * @version $Id: MutablePair.java 1436770 2013-01-22 07:09:45Z ggregory $
29   */
30  public class MutablePair<L, R> extends Pair<L, R> {
31  
32      /** Serialization version */
33      private static final long serialVersionUID = 4954918890077093841L;
34  
35      /** Left object */
36      public L left;
37      /** Right object */
38      public R right;
39  
40      /**
41       * <p>Obtains an immutable pair of from two objects inferring the generic types.</p>
42       * 
43       * <p>This factory allows the pair to be created using inference to
44       * obtain the generic types.</p>
45       * 
46       * @param <L> the left element type
47       * @param <R> the right element type
48       * @param left  the left element, may be null
49       * @param right  the right element, may be null
50       * @return a pair formed from the two parameters, not null
51       */
52      public static <L, R> MutablePair<L, R> of(final L left, final R right) {
53          return new MutablePair<L, R>(left, right);
54      }
55  
56      /**
57       * Create a new pair instance of two nulls.
58       */
59      public MutablePair() {
60          super();
61      }
62  
63      /**
64       * Create a new pair instance.
65       *
66       * @param left  the left value, may be null
67       * @param right  the right value, may be null
68       */
69      public MutablePair(final L left, final R right) {
70          super();
71          this.left = left;
72          this.right = right;
73      }
74  
75      //-----------------------------------------------------------------------
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public L getLeft() {
81          return left;
82      }
83  
84      /**
85       * Sets the left element of the pair.
86       * 
87       * @param left  the new value of the left element, may be null
88       */
89      public void setLeft(final L left) {
90          this.left = left;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public R getRight() {
98          return right;
99      }
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 }