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 * The Universal Permissive License (UPL), Version 1.0 6 * 7 * Subject to the condition set forth below, permission is hereby granted to any 8 * person obtaining a copy of this software, associated documentation and/or 9 * data (collectively the "Software"), free of charge and under any and all 10 * copyright rights in the Software, and any and all patent rights owned or 11 * freely licensable by each licensor hereunder covering either (i) the 12 * unmodified Software as contributed to or provided by such licensor, or (ii) 13 * the Larger Works (as defined below), to deal in both 14 * 15 * (a) the Software, and 16 * 17 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if 18 * one is included with the Software each a "Larger Work" to which the Software 19 * is contributed by such licensors), 20 * 21 * without restriction, including without limitation the rights to copy, create 22 * derivative works of, display, perform, and distribute the Software and make, 23 * use, sell, offer for sale, import, export, have made, and have sold the 24 * Software and the Larger Work(s), and to sublicense the foregoing rights on 25 * either these or other terms. 26 * 27 * This license is subject to the following condition: 28 * 29 * The above copyright notice and either this complete permission notice or at a 30 * minimum a reference to the UPL must be included in all copies or substantial 31 * portions of the Software. 32 * 33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 39 * SOFTWARE. 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 }