Home | History | Annotate | Download | only in cst
      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 char}.
     24  */
     25 public final class CstChar
     26         extends CstLiteral32 {
     27     /** {@code non-null;} the value {@code 0} 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} value
     35      */
     36     public static CstChar make(char value) {
     37         return new CstChar(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 char}
     46      * @return {@code non-null;} the appropriate instance
     47      */
     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} value
     63      */
     64     private CstChar(char value) {
     65         super(value);
     66     }
     67 
     68     /** {@inheritDoc} */
     69     @Override
     70     public String toString() {
     71         int value = getIntBits();
     72         return "char{0x" + Hex.u2(value) + " / " + value + '}';
     73     }
     74 
     75     /** {@inheritDoc} */
     76     public Type getType() {
     77         return Type.CHAR;
     78     }
     79 
     80     /** {@inheritDoc} */
     81     @Override
     82     public String typeName() {
     83         return "char";
     84     }
     85 
     86     /** {@inheritDoc} */
     87     public String toHuman() {
     88         return Integer.toString(getIntBits());
     89     }
     90 
     91     /**
     92      * Gets the {@code char} value.
     93      *
     94      * @return the value
     95      */
     96     public char getValue() {
     97         return (char) getIntBits();
     98     }
     99 }
    100