Home | History | Annotate | Download | only in code
      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.code;
     18 
     19 import com.android.dx.rop.cst.CstString;
     20 
     21 /**
     22  * A local variable item: either a name or a signature or both.
     23  */
     24 public class LocalItem implements Comparable<LocalItem> {
     25     /** {@code null-ok;} local variable name */
     26     private final CstString name;
     27 
     28     /** {@code null-ok;} local variable signature */
     29     private final CstString signature;
     30 
     31     /**
     32      * Make a new item. If both name and signature are null, null is returned.
     33      *
     34      * TODO: intern these
     35      *
     36      * @param name {@code null-ok;} local variable name
     37      * @param signature {@code null-ok;} local variable signature
     38      * @return {@code non-null;} appropriate instance.
     39      */
     40     public static LocalItem make(CstString name, CstString signature) {
     41         if (name == null && signature == null) {
     42             return null;
     43         }
     44 
     45         return new LocalItem (name, signature);
     46     }
     47 
     48     /**
     49      * Constructs instance.
     50      *
     51      * @param name {@code null-ok;} local variable name
     52      * @param signature {@code null-ok;} local variable signature
     53      */
     54     private LocalItem(CstString name, CstString signature) {
     55         this.name = name;
     56         this.signature = signature;
     57     }
     58 
     59     /** {@inheritDoc} */
     60     @Override
     61     public boolean equals(Object other) {
     62         if (!(other instanceof LocalItem)) {
     63             return false;
     64         }
     65 
     66         LocalItem local = (LocalItem) other;
     67 
     68         return 0 == compareTo(local);
     69     }
     70 
     71     /**
     72      * Compares two strings like String.compareTo(), excepts treats a null
     73      * as the least-possible string value.
     74      *
     75      * @return negative integer, zero, or positive integer in accordance
     76      * with Comparable.compareTo()
     77      */
     78     private static int compareHandlesNulls(CstString a, CstString b) {
     79         if (a == b) {
     80             return 0;
     81         } else if (a == null) {
     82             return -1;
     83         } else if (b == null) {
     84             return 1;
     85         } else {
     86             return a.compareTo(b);
     87         }
     88     }
     89 
     90     /** {@inheritDoc} */
     91     public int compareTo(LocalItem local) {
     92         int ret;
     93 
     94         ret = compareHandlesNulls(name, local.name);
     95 
     96         if (ret != 0) {
     97             return ret;
     98         }
     99 
    100         ret = compareHandlesNulls(signature, local.signature);
    101 
    102         return ret;
    103     }
    104 
    105 
    106     /** {@inheritDoc} */
    107     @Override
    108     public int hashCode() {
    109         return (name == null ? 0 : name.hashCode()) * 31
    110                 + (signature == null ? 0 : signature.hashCode());
    111     }
    112 
    113     /** {@inheritDoc} */
    114     @Override
    115     public String toString() {
    116         if (name != null && signature == null) {
    117             return name.toQuoted();
    118         } else if (name == null && signature == null) {
    119             return "";
    120         }
    121 
    122         return "[" + (name == null ? "" : name.toQuoted())
    123                 + "|" + (signature == null ? "" : signature.toQuoted());
    124     }
    125 
    126     /**
    127      * Gets name.
    128      *
    129      * @return {@code null-ok;} name
    130      */
    131     public CstString getName() {
    132         return name;
    133     }
    134 
    135     /**
    136      * Gets signature.
    137      *
    138      * @return {@code null-ok;} signature
    139      */
    140     public CstString getSignature() {
    141         return signature;
    142     }
    143 }
    144