src/jdk.internal.vm.compiler/share/classes/org.graalvm.util/src/org/graalvm/util/Pair.java
changeset 49091 0fa50be70f7a
parent 49090 82c1fe23c469
parent 48909 54b423e1c4cf
child 49092 6dc5e0cdb44c
equal deleted inserted replaced
49090:82c1fe23c469 49091:0fa50be70f7a
     1 /*
       
     2  * Copyright (c) 2017, 2017, 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 package org.graalvm.util;
       
    24 
       
    25 import java.util.Objects;
       
    26 
       
    27 /**
       
    28  * Utility class representing a pair of values.
       
    29  */
       
    30 public final class Pair<L, R> {
       
    31     private static final Pair<Object, Object> EMPTY = new Pair<>(null, null);
       
    32 
       
    33     private final L left;
       
    34     private final R right;
       
    35 
       
    36     @SuppressWarnings("unchecked")
       
    37     public static <L, R> Pair<L, R> empty() {
       
    38         return (Pair<L, R>) EMPTY;
       
    39     }
       
    40 
       
    41     public static <L, R> Pair<L, R> createLeft(L left) {
       
    42         if (left == null) {
       
    43             return empty();
       
    44         } else {
       
    45             return new Pair<>(left, null);
       
    46         }
       
    47     }
       
    48 
       
    49     public static <L, R> Pair<L, R> createRight(R right) {
       
    50         if (right == null) {
       
    51             return empty();
       
    52         } else {
       
    53             return new Pair<>(null, right);
       
    54         }
       
    55     }
       
    56 
       
    57     public static <L, R> Pair<L, R> create(L left, R right) {
       
    58         if (right == null && left == null) {
       
    59             return empty();
       
    60         } else {
       
    61             return new Pair<>(left, right);
       
    62         }
       
    63     }
       
    64 
       
    65     private Pair(L left, R right) {
       
    66         this.left = left;
       
    67         this.right = right;
       
    68     }
       
    69 
       
    70     public L getLeft() {
       
    71         return left;
       
    72     }
       
    73 
       
    74     public R getRight() {
       
    75         return right;
       
    76     }
       
    77 
       
    78     @Override
       
    79     public int hashCode() {
       
    80         return Objects.hashCode(left) + 31 * Objects.hashCode(right);
       
    81     }
       
    82 
       
    83     @SuppressWarnings("unchecked")
       
    84     @Override
       
    85     public boolean equals(Object obj) {
       
    86         if (obj == this) {
       
    87             return true;
       
    88         }
       
    89 
       
    90         if (obj instanceof Pair) {
       
    91             Pair<L, R> pair = (Pair<L, R>) obj;
       
    92             return Objects.equals(left, pair.left) && Objects.equals(right, pair.right);
       
    93         }
       
    94 
       
    95         return false;
       
    96     }
       
    97 
       
    98     @Override
       
    99     public String toString() {
       
   100         return String.format("(%s, %s)", left, right);
       
   101     }
       
   102 }