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  import java.util.Objects;
26  
27  /**
28   * Generic pair.
29   * <p>
30   * Although the instances of this class are immutable, it is impossible
31   * to ensure that the references passed to the constructor will not be
32   * modified by the caller.
33   *
34   * @param <K> Key type.
35   * @param <V> Value type.
36   */
37  public class Pair<K, V> {
38      /** Key. */
39      private final K key;
40      /** Value. */
41      private final V value;
42  
43      /**
44       * Create an entry representing a mapping from the specified key to the
45       * specified value.
46       *
47       * @param k Key (first element of the pair).
48       * @param v Value (second element of the pair).
49       */
50      public Pair(K k, V v) {
51          key = k;
52          value = v;
53      }
54  
55      /**
56       * Create an entry representing the same mapping as the specified entry.
57       *
58       * @param entry Entry to copy.
59       */
60      public Pair(Pair<? extends K, ? extends V> entry) {
61          this(entry.getKey(), entry.getValue());
62      }
63  
64      /**
65       * Get the key.
66       *
67       * @return the key (first element of the pair).
68       */
69      public K getKey() {
70          return key;
71      }
72  
73      /**
74       * Get the value.
75       *
76       * @return the value (second element of the pair).
77       */
78      public V getValue() {
79          return value;
80      }
81  
82      /**
83       * Get the first element of the pair.
84       *
85       * @return the first element of the pair.
86       */
87      public K getFirst() {
88          return key;
89      }
90  
91      /**
92       * Get the second element of the pair.
93       *
94       * @return the second element of the pair.
95       */
96      public V getSecond() {
97          return value;
98      }
99  
100     /**
101      * Compare the specified object with this entry for equality.
102      *
103      * @param o Object.
104      * @return {@code true} if the given object is also a map entry and
105      * the two entries represent the same mapping.
106      */
107     @Override
108     public boolean equals(Object o) {
109         if (this == o) {
110             return true;
111         }
112         if (!(o instanceof Pair)) {
113             return false;
114         }
115         Pair<?, ?> other = (Pair<?, ?>) o;
116         return (Objects.equals(key, other.key)) &&
117                (Objects.equals(value, 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);
153     }
154 }