1 /* 2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package jdk.internal.vm.compiler.collections; 42 43 import java.util.Objects; 44 45 /** 46 * Utility class representing a pair of values. 47 * 48 * @since 1.0 49 */ 50 public final class Pair<L, R> { 51 52 private static final Pair<Object, Object> EMPTY = new Pair<>(null, null); 53 54 private final L left; 55 private final R right; 56 57 /** 58 * Returns an empty pair. 59 * 60 * @since 1.0 61 */ 62 @SuppressWarnings("unchecked") 63 public static <L, R> Pair<L, R> empty() { 64 return (Pair<L, R>) EMPTY; 65 } 66 67 /** 68 * Constructs a pair with its left value being {@code left}, or returns an empty pair if 69 * {@code left} is null. 70 * 71 * @return the constructed pair or an empty pair if {@code left} is null. 72 * @since 1.0 73 */ 74 public static <L, R> Pair<L, R> createLeft(L left) { 75 if (left == null) { 76 return empty(); 77 } else { 78 return new Pair<>(left, null); 79 } 80 } 81 82 /** 83 * Constructs a pair with its right value being {@code right}, or returns an empty pair if 84 * {@code right} is null. 85 * 86 * @return the constructed pair or an empty pair if {@code right} is null. 87 * @since 1.0 88 */ 89 public static <L, R> Pair<L, R> createRight(R right) { 90 if (right == null) { 91 return empty(); 92 } else { 93 return new Pair<>(null, right); 94 } 95 } 96 97 /** 98 * Constructs a pair with its left value being {@code left}, and its right value being 99 * {@code right}, or returns an empty pair if both inputs are null. 100 * 101 * @return the constructed pair or an empty pair if both inputs are null. 102 * @since 1.0 103 */ 104 public static <L, R> Pair<L, R> create(L left, R right) { 105 if (right == null && left == null) { 106 return empty(); 107 } else { 108 return new Pair<>(left, right); 109 } 110 } 111 112 private Pair(L left, R right) { 113 this.left = left; 114 this.right = right; 115 } 116 117 /** 118 * Returns the left value of this pair. 119 * 120 * @since 1.0 121 */ 122 public L getLeft() { 123 return left; 124 } 125 126 /** 127 * Returns the right value of this pair. 128 * 129 * @since 1.0 130 */ 131 public R getRight() { 132 return right; 133 } 134 135 /** 136 * {@inheritDoc} 137 * 138 * @since 1.0 139 */ 140 @Override 141 public int hashCode() { 142 return Objects.hashCode(left) + 31 * Objects.hashCode(right); 143 } 144 145 /** 146 * {@inheritDoc} 147 * 148 * @since 1.0 149 */ 150 @SuppressWarnings("unchecked") 151 @Override 152 public boolean equals(Object obj) { 153 if (obj == this) { 154 return true; 155 } 156 157 if (obj instanceof Pair) { 158 Pair<L, R> pair = (Pair<L, R>) obj; 159 return Objects.equals(left, pair.left) && Objects.equals(right, pair.right); 160 } 161 162 return false; 163 } 164 165 /** 166 * {@inheritDoc} 167 * 168 * @since 1.0 169 */ 170 @Override 171 public String toString() { 172 return String.format("(%s, %s)", left, right); 173 } 174 }