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