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   */
29  public class MutablePair<L, R> extends Pair<L, R> {
30  
31      /** Serialization version */
32      private static final long serialVersionUID = 4954918890077093841L;
33  
34      /** Left object */
35      public L left;
36      /** Right object */
37      public R right;
38  
39      /**
40       * <p>Obtains an immutable pair of from two objects inferring the generic types.</p>
41       * 
42       * <p>This factory allows the pair to be created using inference to
43       * obtain the generic types.</p>
44       * 
45       * @param <L> the left element type
46       * @param <R> the right element type
47       * @param left  the left element, may be null
48       * @param right  the right element, may be null
49       * @return a pair formed from the two parameters, not null
50       */
51      public static <L, R> MutablePair<L, R> of(final L left, final R right) {
52          return new MutablePair<L, R>(left, right);
53      }
54  
55      /**
56       * Create a new pair instance of two nulls.
57       */
58      public MutablePair() {
59          super();
60      }
61  
62      /**
63       * Create a new pair instance.
64       *
65       * @param left  the left value, may be null
66       * @param right  the right value, may be null
67       */
68      public MutablePair(final L left, final R right) {
69          super();
70          this.left = left;
71          this.right = right;
72      }
73  
74      //-----------------------------------------------------------------------
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public L getLeft() {
80          return left;
81      }
82  
83      /**
84       * Sets the left element of the pair.
85       * 
86       * @param left  the new value of the left element, may be null
87       */
88      public void setLeft(final L left) {
89          this.left = left;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public R getRight() {
97          return right;
98      }
99  
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 }