aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/Pair.java
blob: 0d8a862f089e48791bd1628aa07231d88edb4d1f (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package org.checkerframework.javacutil;

/*>>>
import org.checkerframework.dataflow.qual.Pure;
import org.checkerframework.dataflow.qual.SideEffectFree;
*/

/**
 * Simple pair class for multiple returns.
 *
 * TODO: as class is immutable, use @Covariant annotation.
 */
public class Pair<V1, V2> {
    public final V1 first;
    public final V2 second;

    private Pair(V1 v1, V2 v2) {
        this.first = v1;
        this.second = v2;
    }

    public static <V1, V2> Pair<V1, V2> of(V1 v1, V2 v2) {
        return new Pair<V1, V2>(v1, v2);
    }

    /*@SideEffectFree*/
    @Override
    public String toString() {
        return "Pair(" + first + ", " + second + ")";
    }

    private int hashCode = -1;

    /*@Pure*/
    @Override
    public int hashCode() {
        if (hashCode == -1) {
            hashCode = 31;
            if (first != null) {
                hashCode += 17 * first.hashCode();
            }
            if (second != null) {
                hashCode += 17 * second.hashCode();
            }
        }
        return hashCode;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Pair)) {
            return false;
        }
        @SuppressWarnings("unchecked")
        Pair<V1, V2> other = (Pair<V1, V2>) o;
        if (this.first == null) {
            if (other.first != null) return false;
        } else {
            if (!this.first.equals(other.first)) return false;
        }
        if (this.second == null) {
            if (other.second != null) return false;
        } else {
            if (!this.second.equals(other.second)) return false;
        }

        return true;
    }
}