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