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