File: PrimitivesInterning.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,840 kB
  • sloc: java: 145,910; xml: 839; sh: 518; makefile: 401; perl: 26
file content (123 lines) | stat: -rw-r--r-- 3,165 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.HashMap;
import java.util.Map;
import org.checkerframework.checker.interning.qual.*;

public class PrimitivesInterning {

    void test() {
        int a = 3;

        if (a == 3) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }

        if (a != 2) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }

        String name = "Interning";
        if ((name.indexOf('[') == -1) && (name.indexOf('(') == -1)) {
            System.out.println("has no open punctuation");
        } else {
            System.out.println("has open punctuation");
        }

        Number n = Integer.valueOf(22);
        boolean is_double = (n instanceof Double); // valid

        int index = 0;
        index = Integer.decode("22"); // valid: auto-unboxing conversion

        // auto-unboxing conversion again
        Map<String, Integer> m = new HashMap<>();
        if (m.get("hello") == 22) {
            System.out.println("hello maps to 22");
        }
    }

    public static int pow_fast(int base, int expt) throws ArithmeticException {
        if (expt < 0) {
            throw new ArithmeticException("Negative base passed to pow");
        }

        int this_square_pow = base;
        int result = 1;
        while (expt > 0) {
            if ((expt & 1) != 0) {
                result *= this_square_pow;
            }
            expt >>= 1;
            this_square_pow *= this_square_pow;
        }
        return result;
    }

    /** Return the greatest common divisor of the two arguments. */
    public static int gcd(int a, int b) {

        // Euclid's method
        if (b == 0) {
            return (Math.abs(a));
        }
        a = Math.abs(a);
        b = Math.abs(b);
        while (b != 0) {
            int tmp = b;
            b = a % b;
            a = tmp;
        }
        return a;
    }

    /** Return the greatest common divisor of the elements of int array a. */
    public static int gcd(int[] a) {
        // Euclid's method
        if (a.length == 0) {
            return 0;
        }
        int result = a[0];
        for (int i = 1; i < a.length; i++) {
            result = gcd(a[i], result);
            if ((result == 1) || (result == 0)) {
                return result;
            }
        }
        return result;
    }

    /**
     * Return the gcd (greatest common divisor) of the differences between the elements of int array
     * a.
     */
    public static int gcd_differences(int[] a) {
        // Euclid's method
        if (a.length < 2) {
            return 0;
        }
        int result = a[1] - a[0];
        for (int i = 2; i < a.length; i++) {
            result = gcd(a[i] - a[i - 1], result);
            if ((result == 1) || (result == 0)) {
                return result;
            }
        }
        return result;
    }

    void compounds() {
        int res = 0;
        res += 5;
        res /= 9;
    }

    // TODO: enable after boxing is improved in AST creation
    // void negation() {
    //   Boolean t = new Boolean(true);
    //   boolean b = !t;
    // }

}