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    *      https://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  import java.util.Map;
20  import java.util.Objects;
21  
22  /**
23   * A mutable pair consisting of two {@link Object} elements.
24   *
25   * <p>Not #ThreadSafe#</p>
26   *
27   * @param <L> the left element type.
28   * @param <R> the right element type.
29   * @since 3.0
30   */
31  public class MutablePair<L, R> extends Pair<L, R> {
32  
33      /**
34       * An empty array.
35       * <p>
36       * Consider using {@link #emptyArray()} to avoid generics warnings.
37       * </p>
38       *
39       * @since 3.10
40       */
41      public static final MutablePair<?, ?>[] EMPTY_ARRAY = {};
42  
43      /** Serialization version */
44      private static final long serialVersionUID = 4954918890077093841L;
45  
46      /**
47       * Returns the empty array singleton that can be assigned without compiler warning.
48       *
49       * @param <L> the left element type.
50       * @param <R> the right element type.
51       * @return the empty array singleton that can be assigned without compiler warning.
52       * @since 3.10
53       */
54      @SuppressWarnings("unchecked")
55      public static <L, R> MutablePair<L, R>[] emptyArray() {
56          return (MutablePair<L, R>[]) EMPTY_ARRAY;
57      }
58  
59      /**
60       * Creates a mutable pair of two objects inferring the generic types.
61       *
62       * @param <L> the left element type.
63       * @param <R> the right element type.
64       * @param left  the left element, may be null.
65       * @param right  the right element, may be null.
66       * @return a mutable pair formed from the two parameters, not null.
67       */
68      public static <L, R> MutablePair<L, R> of(final L left, final R right) {
69          return new MutablePair<>(left, right);
70      }
71  
72      /**
73       * Creates a mutable pair from a map entry.
74       *
75       * @param <L> the left element type.
76       * @param <R> the right element type.
77       * @param pair the existing map entry.
78       * @return a mutable pair formed from the map entry.
79       */
80      public static <L, R> MutablePair<L, R> of(final Map.Entry<L, R> pair) {
81          final L left;
82          final R right;
83          if (pair != null) {
84              left = pair.getKey();
85              right = pair.getValue();
86          } else {
87              left = null;
88              right = null;
89          }
90          return new MutablePair<>(left, right);
91      }
92  
93      /**
94       * Creates a mutable pair of two non-null objects inferring the generic types.
95       *
96       * @param <L> the left element type.
97       * @param <R> the right element type.
98       * @param left  the left element, may not be null.
99       * @param right  the right element, may not be null.
100      * @return a mutable pair formed from the two parameters, not null.
101      * @throws NullPointerException if any input is null.
102      * @since 3.13.0
103      */
104     public static <L, R> MutablePair<L, R> ofNonNull(final L left, final R right) {
105         return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right"));
106     }
107 
108     /**
109      * Creates a mutable pair from a map entry.
110      *
111      * @param <L> the left element type
112      * @param <R> the right element type
113      * @param pair the existing map entry.
114      * @return a mutable pair formed from the map entry
115      * @throws NullPointerException if the pair is null.
116      * @since 3.20
117      */
118     public static <L, R> MutablePair<L, R> ofNonNull(final Map.Entry<L, R> pair) {
119         return of(Objects.requireNonNull(pair, "pair"));
120     }
121 
122     /** Left object. */
123     public L left;
124 
125     /** Right object. */
126     public R right;
127 
128     /**
129      * Create a new pair instance of two nulls.
130      */
131     public MutablePair() {
132     }
133 
134     /**
135      * Create a new pair instance.
136      *
137      * @param left  the left value, may be null.
138      * @param right  the right value, may be null.
139      */
140     public MutablePair(final L left, final R right) {
141         this.left = left;
142         this.right = right;
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public L getLeft() {
150         return left;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public R getRight() {
158         return right;
159     }
160 
161     /**
162      * Sets the left element of the pair.
163      *
164      * @param left  the new value of the left element, may be null.
165      */
166     public void setLeft(final L left) {
167         this.left = left;
168     }
169 
170     /**
171      * Sets the right element of the pair.
172      *
173      * @param right  the new value of the right element, may be null.
174      */
175     public void setRight(final R right) {
176         this.right = right;
177     }
178 
179     /**
180      * Sets the {@code Map.Entry} value.
181      * This sets the right element of the pair.
182      *
183      * @param value  the right value to set, not null.
184      * @return the old value for the right element.
185      */
186     @Override
187     public R setValue(final R value) {
188         final R result = getRight();
189         setRight(value);
190         return result;
191     }
192 
193 }