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.math3.util; 18 19 /** 20 * Generic pair. <br> 21 * Although the instances of this class are immutable, it is impossible to ensure that the 22 * references passed to the constructor will not be modified by the caller. 23 * 24 * @param <K> Key type. 25 * @param <V> Value type. 26 * @since 3.0 27 */ 28 public class Pair<K, V> { 29 /** Key. */ 30 private final K key; 31 32 /** Value. */ 33 private final V value; 34 35 /** 36 * Create an entry representing a mapping from the specified key to the specified value. 37 * 38 * @param k Key (first element of the pair). 39 * @param v Value (second element of the pair). 40 */ Pair(K k, V v)41 public Pair(K k, V v) { 42 key = k; 43 value = v; 44 } 45 46 /** 47 * Create an entry representing the same mapping as the specified entry. 48 * 49 * @param entry Entry to copy. 50 */ Pair(Pair<? extends K, ? extends V> entry)51 public Pair(Pair<? extends K, ? extends V> entry) { 52 this(entry.getKey(), entry.getValue()); 53 } 54 55 /** 56 * Get the key. 57 * 58 * @return the key (first element of the pair). 59 */ getKey()60 public K getKey() { 61 return key; 62 } 63 64 /** 65 * Get the value. 66 * 67 * @return the value (second element of the pair). 68 */ getValue()69 public V getValue() { 70 return value; 71 } 72 73 /** 74 * Get the first element of the pair. 75 * 76 * @return the first element of the pair. 77 * @since 3.1 78 */ getFirst()79 public K getFirst() { 80 return key; 81 } 82 83 /** 84 * Get the second element of the pair. 85 * 86 * @return the second element of the pair. 87 * @since 3.1 88 */ getSecond()89 public V getSecond() { 90 return value; 91 } 92 93 /** 94 * Compare the specified object with this entry for equality. 95 * 96 * @param o Object. 97 * @return {@code true} if the given object is also a map entry and the two entries represent 98 * the same mapping. 99 */ 100 @Override equals(Object o)101 public boolean equals(Object o) { 102 if (this == o) { 103 return true; 104 } 105 if (!(o instanceof Pair)) { 106 return false; 107 } else { 108 Pair<?, ?> oP = (Pair<?, ?>) o; 109 return (key == null ? oP.key == null : key.equals(oP.key)) 110 && (value == null ? oP.value == null : value.equals(oP.value)); 111 } 112 } 113 114 /** 115 * Compute a hash code. 116 * 117 * @return the hash code value. 118 */ 119 @Override hashCode()120 public int hashCode() { 121 int result = key == null ? 0 : key.hashCode(); 122 123 final int h = value == null ? 0 : value.hashCode(); 124 result = 37 * result + h ^ (h >>> 16); 125 126 return result; 127 } 128 129 /** {@inheritDoc} */ 130 @Override toString()131 public String toString() { 132 return "[" + getKey() + ", " + getValue() + "]"; 133 } 134 135 /** 136 * Convenience factory method that calls the {@link #Pair(Object, Object) constructor}. 137 * 138 * @param <K> the key type 139 * @param <V> the value type 140 * @param k First element of the pair. 141 * @param v Second element of the pair. 142 * @return a new {@code Pair} containing {@code k} and {@code v}. 143 * @since 3.3 144 */ create(K k, V v)145 public static <K, V> Pair<K, V> create(K k, V v) { 146 return new Pair<K, V>(k, v); 147 } 148 } 149