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 * <p>This factory allows the pair to be created using inference to 74 * obtain the generic types.</p> 75 * 76 * @param <L> the left element type 77 * @param <R> the right element type 78 * @param left the left element, may be null 79 * @return a pair formed from the two parameters, not null 80 * @since 3.11 81 */ 82 public static <L, R> Pair<L, R> left(final L left) { 83 return of(left, null); 84 } 85 86 /** 87 * Returns an immutable pair of nulls. 88 * 89 * @param <L> the left element of this pair. Value is {@code null}. 90 * @param <R> the right element of this pair. Value is {@code null}. 91 * @return an immutable pair of nulls. 92 * @since 3.6 93 */ 94 @SuppressWarnings("unchecked") 95 public static <L, R> ImmutablePair<L, R> nullPair() { 96 return NULL; 97 } 98 99 /** 100 * Creates an immutable pair of two objects inferring the generic types. 101 * 102 * <p>This factory allows the pair to be created using inference to 103 * obtain the generic types.</p> 104 * 105 * @param <L> the left element type 106 * @param <R> the right element type 107 * @param left the left element, may be null 108 * @param right the right element, may be null 109 * @return a pair formed from the two parameters, not null 110 */ 111 public static <L, R> ImmutablePair<L, R> of(final L left, final R right) { 112 return left != null || right != null ? new ImmutablePair<>(left, right) : nullPair(); 113 } 114 115 /** 116 * Creates an immutable pair from a map entry. 117 * 118 * <p>This factory allows the pair to be created using inference to 119 * obtain the generic types.</p> 120 * 121 * @param <L> the left element type 122 * @param <R> the right element type 123 * @param pair the existing map entry. 124 * @return a pair formed from the map entry 125 * @since 3.10 126 */ 127 public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) { 128 return pair != null ? new ImmutablePair<>(pair.getKey(), pair.getValue()) : nullPair(); 129 } 130 131 /** 132 * Creates an immutable pair of two non-null objects inferring the generic types. 133 * 134 * <p>This factory allows the pair to be created using inference to 135 * obtain the generic types.</p> 136 * 137 * @param <L> the left element type 138 * @param <R> the right element type 139 * @param left the left element, may not be null 140 * @param right the right element, may not be null 141 * @return a pair formed from the two parameters, not null 142 * @throws NullPointerException if any input is null 143 * @since 3.13.0 144 */ 145 public static <L, R> ImmutablePair<L, R> ofNonNull(final L left, final R right) { 146 return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right")); 147 } 148 149 /** 150 * Creates an immutable pair of two objects inferring the generic types. 151 * 152 * <p>This factory allows the pair to be created using inference to 153 * obtain the generic types.</p> 154 * 155 * @param <L> the left element type 156 * @param <R> the right element type 157 * @param right the right element, may be null 158 * @return a pair formed from the two parameters, not null 159 * @since 3.11 160 */ 161 public static <L, R> Pair<L, R> right(final R right) { 162 return of(null, right); 163 } 164 165 /** Left object */ 166 public final L left; 167 168 /** Right object */ 169 public final R right; 170 171 /** 172 * Create a new pair instance. 173 * 174 * @param left the left value, may be null 175 * @param right the right value, may be null 176 */ 177 public ImmutablePair(final L left, final R right) { 178 this.left = left; 179 this.right = right; 180 } 181 182 /** 183 * {@inheritDoc} 184 */ 185 @Override 186 public L getLeft() { 187 return left; 188 } 189 190 /** 191 * {@inheritDoc} 192 */ 193 @Override 194 public R getRight() { 195 return right; 196 } 197 198 /** 199 * Throws {@link UnsupportedOperationException}. 200 * 201 * <p>This pair is immutable, so this operation is not supported.</p> 202 * 203 * @param value the value to set 204 * @return never 205 * @throws UnsupportedOperationException as this operation is not supported 206 */ 207 @Override 208 public R setValue(final R value) { 209 throw new UnsupportedOperationException(); 210 } 211 212 }