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 
17 package com.android.dexgen.rop.cst;
18 
19 import com.android.dexgen.rop.type.Type;
20 import com.android.dexgen.util.Hex;
21 
22 /**
23  * Constants of type {@code byte}.
24  */
25 public 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      */
make(byte value)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      */
make(int value)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      */
CstByte(byte value)64     private CstByte(byte value) {
65         super(value);
66     }
67 
68     /** {@inheritDoc} */
69     @Override
toString()70     public String toString() {
71         int value = getIntBits();
72         return "byte{0x" + Hex.u1(value) + " / " + value + '}';
73     }
74 
75     /** {@inheritDoc} */
getType()76     public Type getType() {
77         return Type.BYTE;
78     }
79 
80     /** {@inheritDoc} */
81     @Override
typeName()82     public String typeName() {
83         return "byte";
84     }
85 
86     /** {@inheritDoc} */
toHuman()87     public String toHuman() {
88         return Integer.toString(getIntBits());
89     }
90 
91     /**
92      * Gets the {@code byte} value.
93      *
94      * @return the value
95      */
getValue()96     public byte getValue() {
97         return (byte) getIntBits();
98     }
99 }
100