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.dx.rop.cst;
18
19import com.android.dx.rop.type.Type;
20import com.android.dx.util.Hex;
21
22/**
23 * Constants of type {@code byte}.
24 */
25public final class CstByte
26        extends CstLiteral32 {
27    /** {@code non-null;} the value {@code 0} as an instance of this class */
28    public static final CstByte VALUE_0 = make((byte) 0);
29
30    /**
31     * Makes an instance for the given value. This may (but does not
32     * necessarily) return an already-allocated instance.
33     *
34     * @param value the {@code byte} value
35     */
36    public static CstByte make(byte value) {
37        return new CstByte(value);
38    }
39
40    /**
41     * Makes an instance for the given {@code int} value. This
42     * may (but does not necessarily) return an already-allocated
43     * instance.
44     *
45     * @param value the value, which must be in range for a {@code byte}
46     * @return {@code non-null;} the appropriate instance
47     */
48    public static CstByte make(int value) {
49        byte cast = (byte) value;
50
51        if (cast != value) {
52            throw new IllegalArgumentException("bogus byte value: " +
53                    value);
54        }
55
56        return make(cast);
57    }
58
59    /**
60     * Constructs an instance. This constructor is private; use {@link #make}.
61     *
62     * @param value the {@code byte} value
63     */
64    private CstByte(byte value) {
65        super(value);
66    }
67
68    /** {@inheritDoc} */
69    @Override
70    public String toString() {
71        int value = getIntBits();
72        return "byte{0x" + Hex.u1(value) + " / " + value + '}';
73    }
74
75    /** {@inheritDoc} */
76    @Override
77    public Type getType() {
78        return Type.BYTE;
79    }
80
81    /** {@inheritDoc} */
82    @Override
83    public String typeName() {
84        return "byte";
85    }
86
87    /** {@inheritDoc} */
88    @Override
89    public String toHuman() {
90        return Integer.toString(getIntBits());
91    }
92
93    /**
94     * Gets the {@code byte} value.
95     *
96     * @return the value
97     */
98    public byte getValue() {
99        return (byte) getIntBits();
100    }
101}
102