001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math3.util;
018    
019    /**
020     * Generic pair.
021     * <br/>
022     * Although the instances of this class are immutable, it is impossible
023     * to ensure that the references passed to the constructor will not be
024     * modified by the caller.
025     *
026     * @param <K> Key type.
027     * @param <V> Value type.
028     *
029     * @since 3.0
030     * @version $Id: Pair.java 1422313 2012-12-15 18:53:41Z psteitz $
031     */
032    public class Pair<K, V> {
033        /** Key. */
034        private final K key;
035        /** Value. */
036        private final V value;
037    
038        /**
039         * Create an entry representing a mapping from the specified key to the
040         * specified value.
041         *
042         * @param k Key (first element of the pair).
043         * @param v Value (second element of the pair).
044         */
045        public Pair(K k, V v) {
046            key = k;
047            value = v;
048        }
049    
050        /**
051         * Create an entry representing the same mapping as the specified entry.
052         *
053         * @param entry Entry to copy.
054         */
055        public Pair(Pair<? extends K, ? extends V> entry) {
056            this(entry.getKey(), entry.getValue());
057        }
058    
059        /**
060         * Get the key.
061         *
062         * @return the key (first element of the pair).
063         */
064        public K getKey() {
065            return key;
066        }
067    
068        /**
069         * Get the value.
070         *
071         * @return the value (second element of the pair).
072         */
073        public V getValue() {
074            return value;
075        }
076    
077        /**
078         * Get the first element of the pair.
079         *
080         * @return the first element of the pair.
081         * @since 3.1
082         */
083        public K getFirst() {
084            return key;
085        }
086    
087        /**
088         * Get the second element of the pair.
089         *
090         * @return the second element of the pair.
091         * @since 3.1
092         */
093        public V getSecond() {
094            return value;
095        }
096    
097        /**
098         * Compare the specified object with this entry for equality.
099         *
100         * @param o Object.
101         * @return {@code true} if the given object is also a map entry and
102         * the two entries represent the same mapping.
103         */
104        @Override
105        public boolean equals(Object o) {
106            if (this == o) {
107                return true;
108            }
109            if (!(o instanceof Pair)) {
110                return false;
111            } else {
112                Pair<?, ?> oP = (Pair<?, ?>) o;
113                return (key == null ?
114                        oP.key == null :
115                        key.equals(oP.key)) &&
116                    (value == null ?
117                     oP.value == null :
118                     value.equals(oP.value));
119            }
120        }
121    
122        /**
123         * Compute a hash code.
124         *
125         * @return the hash code value.
126         */
127        @Override
128        public int hashCode() {
129            int result = key == null ? 0 : key.hashCode();
130    
131            final int h = value == null ? 0 : value.hashCode();
132            result = 37 * result + h ^ (h >>> 16);
133    
134            return result;
135        }
136    }