• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.dx.rop.cst;
18 
19 import com.android.dx.rop.type.Type;
20 import com.android.dx.util.Hex;
21 
22 /**
23  * Constants of type <code>char</code>.
24  */
25 public final class CstChar
26         extends CstLiteral32 {
27     /** non-null; the value <code>0</code> as an instance of this class */
28     public static final CstChar VALUE_0 = make((char) 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>char</code> value
35      */
make(char value)36     public static CstChar make(char value) {
37         return new CstChar(value);
38     }
39 
40     /**
41      * Makes an instance for the given <code>int</code> 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>char</code>
46      * @return non-null; the appropriate instance
47      */
make(int value)48     public static CstChar make(int value) {
49         char cast = (char) value;
50 
51         if (cast != value) {
52             throw new IllegalArgumentException("bogus char 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>char</code> value
63      */
CstChar(char value)64     private CstChar(char value) {
65         super(value);
66     }
67 
68     /** {@inheritDoc} */
69     @Override
toString()70     public String toString() {
71         int value = getIntBits();
72         return "char{0x" + Hex.u2(value) + " / " + value + '}';
73     }
74 
75     /** {@inheritDoc} */
getType()76     public Type getType() {
77         return Type.CHAR;
78     }
79 
80     /** {@inheritDoc} */
81     @Override
typeName()82     public String typeName() {
83         return "char";
84     }
85 
86     /** {@inheritDoc} */
toHuman()87     public String toHuman() {
88         return Integer.toString(getIntBits());
89     }
90 
91     /**
92      * Gets the <code>char</code> value.
93      *
94      * @return the value
95      */
getValue()96     public char getValue() {
97         return (char) getIntBits();
98     }
99 }
100