1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dexgen.rop.cst;
18
19import com.android.dexgen.rop.type.Type;
20import com.android.dexgen.util.Hex;
21
22/**
23 * Constants of type {@code CONSTANT_Integer_info}.
24 */
25public final class CstInteger
26        extends CstLiteral32 {
27    /** {@code non-null;} array of cached instances */
28    private static final CstInteger[] cache = new CstInteger[511];
29
30    /** {@code non-null;} instance representing {@code -1} */
31    public static final CstInteger VALUE_M1 = make(-1);
32
33    /** {@code non-null;} instance representing {@code 0} */
34    public static final CstInteger VALUE_0 = make(0);
35
36    /** {@code non-null;} instance representing {@code 1} */
37    public static final CstInteger VALUE_1 = make(1);
38
39    /** {@code non-null;} instance representing {@code 2} */
40    public static final CstInteger VALUE_2 = make(2);
41
42    /** {@code non-null;} instance representing {@code 3} */
43    public static final CstInteger VALUE_3 = make(3);
44
45    /** {@code non-null;} instance representing {@code 4} */
46    public static final CstInteger VALUE_4 = make(4);
47
48    /** {@code non-null;} instance representing {@code 5} */
49    public static final CstInteger VALUE_5 = make(5);
50
51    /**
52     * Makes an instance for the given value. This may (but does not
53     * necessarily) return an already-allocated instance.
54     *
55     * @param value the {@code int} value
56     * @return {@code non-null;} the appropriate instance
57     */
58    public static CstInteger make(int value) {
59        /*
60         * Note: No need to synchronize, since we don't make any sort
61         * of guarantee about ==, and it's okay to overwrite existing
62         * entries too.
63         */
64        int idx = (value & 0x7fffffff) % cache.length;
65        CstInteger obj = cache[idx];
66
67        if ((obj != null) && (obj.getValue() == value)) {
68            return obj;
69        }
70
71        obj = new CstInteger(value);
72        cache[idx] = obj;
73        return obj;
74    }
75
76    /**
77     * Constructs an instance. This constructor is private; use {@link #make}.
78     *
79     * @param value the {@code int} value
80     */
81    private CstInteger(int value) {
82        super(value);
83    }
84
85    /** {@inheritDoc} */
86    @Override
87    public String toString() {
88        int value = getIntBits();
89        return "int{0x" + Hex.u4(value) + " / " + value + '}';
90    }
91
92    /** {@inheritDoc} */
93    public Type getType() {
94        return Type.INT;
95    }
96
97    /** {@inheritDoc} */
98    @Override
99    public String typeName() {
100        return "int";
101    }
102
103    /** {@inheritDoc} */
104    public String toHuman() {
105        return Integer.toString(getIntBits());
106    }
107
108    /**
109     * Gets the {@code int} value.
110     *
111     * @return the value
112     */
113    public int getValue() {
114        return getIntBits();
115    }
116}
117