Home | History | Annotate | Download | only in annotation
      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.annotation;
     18 
     19 import com.android.dx.rop.cst.Constant;
     20 import com.android.dx.rop.cst.CstString;
     21 import com.android.dx.rop.cst.CstUtf8;
     22 
     23 /**
     24  * A (name, value) pair. These are used as the contents of an annotation.
     25  */
     26 public final class NameValuePair implements Comparable<NameValuePair> {
     27     /** {@code non-null;} the name */
     28     private final CstUtf8 name;
     29 
     30     /** {@code non-null;} the value */
     31     private final Constant value;
     32 
     33     /**
     34      * Construct an instance.
     35      *
     36      * @param name {@code non-null;} the name
     37      * @param value {@code non-null;} the value
     38      */
     39     public NameValuePair(CstUtf8 name, Constant value) {
     40         if (name == null) {
     41             throw new NullPointerException("name == null");
     42         }
     43 
     44         if (value == null) {
     45             throw new NullPointerException("value == null");
     46         }
     47 
     48         // Reject CstUtf8 values. (They should be CstStrings.)
     49         if (value instanceof CstUtf8) {
     50             throw new IllegalArgumentException("bad value: " + value);
     51         }
     52 
     53         this.name = name;
     54         this.value = value;
     55     }
     56 
     57     /** {@inheritDoc} */
     58     public String toString() {
     59         return name.toHuman() + ":" + value;
     60     }
     61 
     62     /** {@inheritDoc} */
     63     public int hashCode() {
     64         return name.hashCode() * 31 + value.hashCode();
     65     }
     66 
     67     /** {@inheritDoc} */
     68     public boolean equals(Object other) {
     69         if (! (other instanceof NameValuePair)) {
     70             return false;
     71         }
     72 
     73         NameValuePair otherPair = (NameValuePair) other;
     74 
     75         return name.equals(otherPair.name)
     76             && value.equals(otherPair.value);
     77     }
     78 
     79     /**
     80      * {@inheritDoc}
     81      *
     82      * <p>Instances of this class compare in name-major and value-minor
     83      * order.</p>
     84      */
     85     public int compareTo(NameValuePair other) {
     86         int result = name.compareTo(other.name);
     87 
     88         if (result != 0) {
     89             return result;
     90         }
     91 
     92         return value.compareTo(other.value);
     93     }
     94 
     95     /**
     96      * Gets the name.
     97      *
     98      * @return {@code non-null;} the name
     99      */
    100     public CstUtf8 getName() {
    101         return name;
    102     }
    103 
    104     /**
    105      * Gets the value.
    106      *
    107      * @return {@code non-null;} the value
    108      */
    109     public Constant getValue() {
    110         return value;
    111     }
    112 }
    113