1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.utils; 17 18 import static software.amazon.awssdk.utils.Validate.paramNotNull; 19 20 import java.util.function.BiFunction; 21 import software.amazon.awssdk.annotations.SdkProtectedApi; 22 23 /** 24 * Simple struct of two values, possibly of different types. 25 * 26 * @param <LeftT> Left type 27 * @param <RightT> Right Type 28 */ 29 @SdkProtectedApi 30 public final class Pair<LeftT, RightT> { 31 32 private final LeftT left; 33 private final RightT right; 34 Pair(LeftT left, RightT right)35 private Pair(LeftT left, RightT right) { 36 this.left = paramNotNull(left, "left"); 37 this.right = paramNotNull(right, "right"); 38 } 39 40 /** 41 * @return Left value 42 */ left()43 public LeftT left() { 44 return this.left; 45 } 46 47 /** 48 * @return Right value 49 */ right()50 public RightT right() { 51 return this.right; 52 } 53 54 /** 55 * Apply the function to both the left and right values and return the transformed result. 56 * 57 * @param function Function to apply on the {@link Pair} 58 * @param <ReturnT> Transformed return type of {@link BiFunction}. 59 * @return Result of {@link BiFunction} applied on left and right values of the pair. 60 */ apply(BiFunction<LeftT, RightT, ReturnT> function)61 public <ReturnT> ReturnT apply(BiFunction<LeftT, RightT, ReturnT> function) { 62 return function.apply(left, right); 63 } 64 65 @Override equals(Object obj)66 public boolean equals(Object obj) { 67 if (!(obj instanceof Pair)) { 68 return false; 69 } 70 71 Pair other = (Pair) obj; 72 return other.left.equals(left) && other.right.equals(right); 73 } 74 75 @Override hashCode()76 public int hashCode() { 77 return getClass().hashCode() + left.hashCode() + right.hashCode(); 78 } 79 80 @Override toString()81 public String toString() { 82 return "Pair(left=" + left + ", right=" + right + ")"; 83 } 84 of(LeftT left, RightT right)85 public static <LeftT, RightT> Pair<LeftT, RightT> of(LeftT left, RightT right) { 86 return new Pair<>(left, right); 87 } 88 } 89