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 import java.io.Serializable; 20 import java.util.Map; 21 import java.util.Objects; 22 23 import org.apache.commons.lang3.builder.CompareToBuilder; 24 import org.apache.commons.lang3.function.FailableBiConsumer; 25 import org.apache.commons.lang3.function.FailableBiFunction; 26 27 /** 28 * A pair consisting of two elements. 29 * 30 * <p>This class is an abstract implementation defining the basic API. 31 * It refers to the elements as 'left' and 'right'. It also implements the 32 * {@code Map.Entry} interface where the key is 'left' and the value is 'right'.</p> 33 * 34 * <p>Subclass implementations may be mutable or immutable. 35 * However, there is no restriction on the type of the stored objects that may be stored. 36 * If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.</p> 37 * 38 * @param <L> the left element type 39 * @param <R> the right element type 40 * 41 * @since 3.0 42 */ 43 public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L, R>>, Serializable { 44 45 /** Serialization version */ 46 private static final long serialVersionUID = 4954918890077093841L; 47 48 /** 49 * An empty array. 50 * <p> 51 * Consider using {@link #emptyArray()} to avoid generics warnings. 52 * </p> 53 * 54 * @since 3.10. 55 */ 56 public static final Pair<?, ?>[] EMPTY_ARRAY = {}; 57 58 /** 59 * Returns the empty array singleton that can be assigned without compiler warning. 60 * 61 * @param <L> the left element type 62 * @param <R> the right element type 63 * @return the empty array singleton that can be assigned without compiler warning. 64 * 65 * @since 3.10. 66 */ 67 @SuppressWarnings("unchecked") 68 public static <L, R> Pair<L, R>[] emptyArray() { 69 return (Pair<L, R>[]) EMPTY_ARRAY; 70 } 71 72 /** 73 * Creates an immutable pair of two objects inferring the generic types. 74 * 75 * <p>This factory allows the pair to be created using inference to 76 * obtain the generic types.</p> 77 * 78 * @param <L> the left element type 79 * @param <R> the right element type 80 * @param left the left element, may be null 81 * @param right the right element, may be null 82 * @return a pair formed from the two parameters, not null 83 */ 84 public static <L, R> Pair<L, R> of(final L left, final R right) { 85 return ImmutablePair.of(left, right); 86 } 87 88 /** 89 * Creates an immutable pair from a map entry. 90 * 91 * <p>This factory allows the pair to be created using inference to 92 * obtain the generic types.</p> 93 * 94 * @param <L> the left element type 95 * @param <R> the right element type 96 * @param pair the map entry. 97 * @return a pair formed from the map entry 98 * @since 3.10 99 */ 100 public static <L, R> Pair<L, R> of(final Map.Entry<L, R> pair) { 101 return ImmutablePair.of(pair); 102 } 103 104 /** 105 * Creates an immutable pair of two non-null objects inferring the generic types. 106 * 107 * <p>This factory allows the pair to be created using inference to 108 * obtain the generic types.</p> 109 * 110 * @param <L> the left element type 111 * @param <R> the right element type 112 * @param left the left element, may not be null 113 * @param right the right element, may not be null 114 * @return a pair formed from the two parameters, not null 115 * @throws NullPointerException if any input is null 116 * @since 3.13.0 117 */ 118 public static <L, R> Pair<L, R> ofNonNull(final L left, final R right) { 119 return ImmutablePair.ofNonNull(left, right); 120 } 121 122 /** 123 * Constructs a new instance. 124 */ 125 public Pair() { 126 // empty 127 } 128 129 /** 130 * Accepts this key and value as arguments to the given consumer. 131 * 132 * @param <E> The kind of thrown exception or error. 133 * @param consumer the consumer to call. 134 * @throws E Thrown when the consumer fails. 135 * @since 3.13.0 136 */ 137 public <E extends Throwable> void accept(final FailableBiConsumer<L, R, E> consumer) throws E { 138 consumer.accept(getKey(), getValue()); 139 } 140 141 /** 142 * Applies this key and value as arguments to the given function. 143 * 144 * @param <V> The function return type. 145 * @param <E> The kind of thrown exception or error. 146 * @param function the consumer to call. 147 * @return the function's return value. 148 * @throws E Thrown when the consumer fails. 149 * @since 3.13.0 150 */ 151 public <V, E extends Throwable> V apply(final FailableBiFunction<L, R, V, E> function) throws E { 152 return function.apply(getKey(), getValue()); 153 } 154 155 /** 156 * Compares the pair based on the left element followed by the right element. 157 * The types must be {@link Comparable}. 158 * 159 * @param other the other pair, not null 160 * @return negative if this is less, zero if equal, positive if greater 161 */ 162 @Override 163 public int compareTo(final Pair<L, R> other) { 164 return new CompareToBuilder().append(getLeft(), other.getLeft()) 165 .append(getRight(), other.getRight()).toComparison(); 166 } 167 168 /** 169 * Compares this pair to another based on the two elements. 170 * 171 * @param obj the object to compare to, null returns false 172 * @return true if the elements of the pair are equal 173 */ 174 @Override 175 public boolean equals(final Object obj) { 176 if (obj == this) { 177 return true; 178 } 179 if (obj instanceof Map.Entry<?, ?>) { 180 final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj; 181 return Objects.equals(getKey(), other.getKey()) 182 && Objects.equals(getValue(), other.getValue()); 183 } 184 return false; 185 } 186 187 /** 188 * Gets the key from this pair. 189 * 190 * <p>This method implements the {@code Map.Entry} interface returning the 191 * left element as the key.</p> 192 * 193 * @return the left element as the key, may be null 194 */ 195 @Override 196 public final L getKey() { 197 return getLeft(); 198 } 199 200 /** 201 * Gets the left element from this pair. 202 * 203 * <p>When treated as a key-value pair, this is the key.</p> 204 * 205 * @return the left element, may be null 206 */ 207 public abstract L getLeft(); 208 209 /** 210 * Gets the right element from this pair. 211 * 212 * <p>When treated as a key-value pair, this is the value.</p> 213 * 214 * @return the right element, may be null 215 */ 216 public abstract R getRight(); 217 218 /** 219 * Gets the value from this pair. 220 * 221 * <p>This method implements the {@code Map.Entry} interface returning the 222 * right element as the value.</p> 223 * 224 * @return the right element as the value, may be null 225 */ 226 @Override 227 public R getValue() { 228 return getRight(); 229 } 230 231 /** 232 * Returns a suitable hash code. 233 * The hash code follows the definition in {@code Map.Entry}. 234 * 235 * @return the hash code 236 */ 237 @Override 238 public int hashCode() { 239 // see Map.Entry API specification 240 return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue()); 241 } 242 243 /** 244 * Returns a String representation of this pair using the format {@code ($left,$right)}. 245 * 246 * @return a string describing this object, not null 247 */ 248 @Override 249 public String toString() { 250 return "(" + getLeft() + ',' + getRight() + ')'; 251 } 252 253 /** 254 * Formats the receiver using the given format. 255 * 256 * <p>This uses {@link java.util.Formattable} to perform the formatting. Two variables may 257 * be used to embed the left and right elements. Use {@code %1$s} for the left 258 * element (key) and {@code %2$s} for the right element (value). 259 * The default format used by {@code toString()} is {@code (%1$s,%2$s)}.</p> 260 * 261 * @param format the format string, optionally containing {@code %1$s} and {@code %2$s}, not null 262 * @return the formatted string, not null 263 */ 264 public String toString(final String format) { 265 return String.format(format, getLeft(), getRight()); 266 } 267 268 }