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