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 
     21 /**
     22  * Constants of type {@code CONSTANT_String_info}.
     23  */
     24 public final class CstString
     25         extends TypedConstant {
     26     /** {@code non-null;} the string value */
     27     private final CstUtf8 string;
     28 
     29     /**
     30      * Constructs an instance.
     31      *
     32      * @param string {@code non-null;} the string value
     33      */
     34     public CstString(CstUtf8 string) {
     35         if (string == null) {
     36             throw new NullPointerException("string == null");
     37         }
     38 
     39         this.string = string;
     40     }
     41 
     42     /**
     43      * Constructs an instance.
     44      *
     45      * @param string {@code non-null;} the string value
     46      */
     47     public CstString(String string) {
     48         this(new CstUtf8(string));
     49     }
     50 
     51     /** {@inheritDoc} */
     52     @Override
     53     public boolean equals(Object other) {
     54         if (!(other instanceof CstString)) {
     55             return false;
     56         }
     57 
     58         return string.equals(((CstString) other).string);
     59     }
     60 
     61     /** {@inheritDoc} */
     62     @Override
     63     public int hashCode() {
     64         return string.hashCode();
     65     }
     66 
     67     /** {@inheritDoc} */
     68     @Override
     69     protected int compareTo0(Constant other) {
     70         return string.compareTo(((CstString) other).string);
     71     }
     72 
     73     /** {@inheritDoc} */
     74     @Override
     75     public String toString() {
     76         return "string{" + toHuman() + '}';
     77     }
     78 
     79     /** {@inheritDoc} */
     80     public Type getType() {
     81         return Type.STRING;
     82     }
     83 
     84     /** {@inheritDoc} */
     85     @Override
     86     public String typeName() {
     87         return "string";
     88     }
     89 
     90     /** {@inheritDoc} */
     91     @Override
     92     public boolean isCategory2() {
     93         return false;
     94     }
     95 
     96     /** {@inheritDoc} */
     97     public String toHuman() {
     98         return string.toQuoted();
     99     }
    100 
    101     /**
    102      * Gets the string value.
    103      *
    104      * @return {@code non-null;} the string value
    105      */
    106     public CstUtf8 getString() {
    107         return string;
    108     }
    109 }
    110