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.  *      http://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. package org.apache.commons.math4.legacy.core;

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

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

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

  54.     /**
  55.      * Get the key.
  56.      *
  57.      * @return the key (first element of the pair).
  58.      */
  59.     public K getKey() {
  60.         return getFirst();
  61.     }

  62.     /**
  63.      * Get the value.
  64.      *
  65.      * @return the value (second element of the pair).
  66.      */
  67.     public V getValue() {
  68.         return getSecond();
  69.     }

  70.     /**
  71.      * Get the first element of the pair.
  72.      *
  73.      * @return the first element of the pair.
  74.      * @since 3.1
  75.      */
  76.     public K getFirst() {
  77.         return key;
  78.     }

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

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

  112.     /**
  113.      * Compute a hash code.
  114.      *
  115.      * @return the hash code value.
  116.      */
  117.     @Override
  118.     public int hashCode() {
  119.         int result = key == null ? 0 : key.hashCode();

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

  122.         return result;
  123.     }

  124.     /** {@inheritDoc} */
  125.     @Override
  126.     public String toString() {
  127.         return "[" + getKey() + ", " + getValue() + "]";
  128.     }

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