Pair.java

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

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.util;


  22. import java.util.Objects;

  23. /**
  24.  * Generic pair.
  25.  * <p>
  26.  * Although the instances of this class are immutable, it is impossible
  27.  * to ensure that the references passed to the constructor will not be
  28.  * modified by the caller.
  29.  *
  30.  * @param <K> Key type.
  31.  * @param <V> Value type.
  32.  */
  33. public class Pair<K, V> {
  34.     /** Key. */
  35.     private final K key;
  36.     /** Value. */
  37.     private final V value;

  38.     /**
  39.      * Create an entry representing a mapping from the specified key to the
  40.      * specified value.
  41.      *
  42.      * @param k Key (first element of the pair).
  43.      * @param v Value (second element of the pair).
  44.      */
  45.     public Pair(K k, V v) {
  46.         key = k;
  47.         value = v;
  48.     }

  49.     /**
  50.      * Create an entry representing the same mapping as the specified entry.
  51.      *
  52.      * @param entry Entry to copy.
  53.      */
  54.     public Pair(Pair<? extends K, ? extends V> entry) {
  55.         this(entry.getKey(), entry.getValue());
  56.     }

  57.     /**
  58.      * Get the key.
  59.      *
  60.      * @return the key (first element of the pair).
  61.      */
  62.     public K getKey() {
  63.         return key;
  64.     }

  65.     /**
  66.      * Get the value.
  67.      *
  68.      * @return the value (second element of the pair).
  69.      */
  70.     public V getValue() {
  71.         return value;
  72.     }

  73.     /**
  74.      * Get the first element of the pair.
  75.      *
  76.      * @return the first element of the pair.
  77.      */
  78.     public K getFirst() {
  79.         return key;
  80.     }

  81.     /**
  82.      * Get the second element of the pair.
  83.      *
  84.      * @return the second element of the pair.
  85.      */
  86.     public V getSecond() {
  87.         return value;
  88.     }

  89.     /**
  90.      * Compare the specified object with this entry for equality.
  91.      *
  92.      * @param o Object.
  93.      * @return {@code true} if the given object is also a map entry and
  94.      * the two entries represent the same mapping.
  95.      */
  96.     @Override
  97.     public boolean equals(Object o) {
  98.         if (this == o) {
  99.             return true;
  100.         }
  101.         if (!(o instanceof Pair)) {
  102.             return false;
  103.         }
  104.         Pair<?, ?> other = (Pair<?, ?>) o;
  105.         return (Objects.equals(key, other.key)) &&
  106.                (Objects.equals(value, other.value));
  107.     }

  108.     /**
  109.      * Compute a hash code.
  110.      *
  111.      * @return the hash code value.
  112.      */
  113.     @Override
  114.     public int hashCode() {
  115.         int result = key == null ? 0 : key.hashCode();

  116.         final int h = value == null ? 0 : value.hashCode();
  117.         result = 37 * result + h ^ (h >>> 16);

  118.         return result;
  119.     }

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public String toString() {
  123.         return "[" + getKey() + ", " + getValue() + "]";
  124.     }

  125.     /**
  126.      * Convenience factory method that calls the
  127.      * {@link #Pair(Object, Object) constructor}.
  128.      *
  129.      * @param <K> the key type
  130.      * @param <V> the value type
  131.      * @param k First element of the pair.
  132.      * @param v Second element of the pair.
  133.      * @return a new {@code Pair} containing {@code k} and {@code v}.
  134.      */
  135.     public static <K, V> Pair<K, V> create(K k, V v) {
  136.         return new Pair<>(k, v);
  137.     }
  138. }