Pair.java

  1. /*
  2.  * Copyright (c) 2016, Stein Eldar johnsen
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements. See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership. The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License. You may obtain a copy of the License at
  11.  *
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied. See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  */
  21. package net.morimekta.collect.util;

  22. import java.util.Objects;

  23. /**
  24.  * Container to ease passing around a tuple of two objects. This object
  25.  * provides a sensible implementation of equals(), returning true if equals()
  26.  * is true on each of the contained objects.
  27.  */
  28. public final class Pair<F, S> {
  29.     /**
  30.      * The first value.
  31.      */
  32.     public final F first;

  33.     /**
  34.      * The second value.
  35.      */
  36.     public final S second;

  37.     /**
  38.      * Constructor for a Pair.
  39.      *
  40.      * @param first First part of pair.
  41.      * @param second Second part of pair.
  42.      */
  43.     public Pair(F first, S second) {
  44.         this.first = first;
  45.         this.second = second;
  46.     }

  47.     /**
  48.      * Convenience method for creating an appropriately typed pair.
  49.      *
  50.      * @param <A> First type.
  51.      * @param <B> Second type.
  52.      * @param a First part.
  53.      * @param b Second part.
  54.      * @return The resulting pair.
  55.      */
  56.     public static <A, B> Pair<A, B> pairOf(A a, B b) {
  57.         return new Pair<>(a, b);
  58.     }

  59.     /**
  60.      * Convenience method to get the first value.
  61.      *
  62.      * @return The first value.
  63.      */
  64.     public F getFirst() {
  65.         return first;
  66.     }

  67.     /**
  68.      * Convenience method to get the second value.
  69.      *
  70.      * @return The second value.
  71.      */
  72.     public S getSecond() {
  73.         return second;
  74.     }

  75.     @Override
  76.     public boolean equals(Object o) {
  77.         if (o == this) {
  78.             return true;
  79.         }
  80.         if (!(o instanceof Pair)) {
  81.             return false;
  82.         }
  83.         Pair<?, ?> other = (Pair<?, ?>) o;

  84.         return (Objects.equals(first, other.first) &&
  85.                 Objects.equals(second, other.second));
  86.     }

  87.     @Override
  88.     public int hashCode() {
  89.         return Objects.hash(first, second);
  90.     }

  91.     @Override
  92.     public String toString() {
  93.         return String.format("(%s,%s)", first, second);
  94.     }
  95. }