-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tuple.java
46 lines (39 loc) · 820 Bytes
/
Tuple.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package q5128376;
import java.util.Arrays;
public class Tuple<T>
{
private T[] values;
private int hashCode;
private Tuple(T... values)
{
this.values = values;
this.hashCode = hashCode(values);
}
public static <T> Tuple<T> create(T... values)
{
return new Tuple<T>(values);
}
private static <T> int hashCode(T... values)
{
return 31 * Arrays.hashCode(values);
}
@Override
public int hashCode()
{
return hashCode;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Tuple<?>))
return false;
Tuple<?> other = (Tuple<?>) obj;
if (!Arrays.equals(values, other.values))
return false;
return true;
}
}