File: Heuristics.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (86 lines) | stat: -rw-r--r-- 2,705 bytes parent folder | download | duplicates (3)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.Comparator;

public class Heuristics implements Comparable<Heuristics> {

    public static final class MyComparator implements Comparator<String> {
        // Using == is OK if it's the first statement in the compare method,
        // it's comparing the arguments, and the return value is 0.
        public int compare(String s1, String s2) {
            if (s1 == s2) {
                return 0;
            }
            return String.CASE_INSENSITIVE_ORDER.compare(s1, s2);
        }
    }

    @Override
    @org.checkerframework.dataflow.qual.Pure
    public boolean equals(Object o) {
        // Using == is OK if it's the first statement in the equals method
        // and it compares "this" against the argument.
        if (this == o) {
            return true;
        }
        // Not the first statement in the method.
        // :: error: (not.interned)
        if (o == this) {
            return true;
        }
        return false;
    }

    @Override
    @org.checkerframework.dataflow.qual.Pure
    public int compareTo(Heuristics o) {
        // Using == is OK if it's the first statement in the equals method
        // and it compares "this" against the argument.

        if (o == this) {
            return 0;
        }
        // Not the first statement in the method.
        // :: error: (not.interned)
        if (this == o) {
            return 0;
        }
        return 0;
    }

    public boolean optimizeEqualsClient(Object a, Object b, Object[] arr) {
        // Using == is OK if it's the left-hand side of an || whose right-hand
        // side is a call to equals with the same arguments.
        if (a == b || a.equals(b)) {
            System.out.println("one");
        }
        if (a == b || b.equals(a)) {
            System.out.println("two");
        }

        boolean c = (a == b || a.equals(b));
        c = (a == b || b.equals(a));

        boolean d = (a == b) || (a != null ? a.equals(b) : false);

        boolean e = (a == b || (a != null && a.equals(b)));

        boolean f = (arr[0] == a || arr[0].equals(a));

        return (a == b || a.equals(b));
    }

    public <T extends Comparable<T>> boolean optimizeCompareToClient(T a, T b) {
        // Using == is OK if it's the left-hand side of an || whose right-hand
        // side is a call to compareTo with the same arguments.
        if (a == b || a.compareTo(b) == 0) {
            System.out.println("one");
        }
        if (a == b || b.compareTo(a) == 0) {
            System.out.println("two");
        }

        boolean c = (a == b || a.compareTo(b) == 0);
        c = (a == b || a.compareTo(b) == 0);

        return (a == b || a.compareTo(b) == 0);
    }
}