source: src/main/java/agents/anac/y2019/harddealer/math3/util/Pair.java

Last change on this file was 200, checked in by Katsuhide Fujita, 5 years ago

Add ANAC 2019 agents

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