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