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
17package com.android.dx.rop.annotation;
18
19import com.android.dx.rop.cst.Constant;
20import com.android.dx.rop.cst.CstString;
21
22/**
23 * A (name, value) pair. These are used as the contents of an annotation.
24 */
25public 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    public String toString() {
53        return name.toHuman() + ":" + value;
54    }
55
56    /** {@inheritDoc} */
57    public int hashCode() {
58        return name.hashCode() * 31 + value.hashCode();
59    }
60
61    /** {@inheritDoc} */
62    public boolean equals(Object other) {
63        if (! (other instanceof NameValuePair)) {
64            return false;
65        }
66
67        NameValuePair otherPair = (NameValuePair) other;
68
69        return name.equals(otherPair.name)
70            && value.equals(otherPair.value);
71    }
72
73    /**
74     * {@inheritDoc}
75     *
76     * <p>Instances of this class compare in name-major and value-minor
77     * order.</p>
78     */
79    public int compareTo(NameValuePair other) {
80        int result = name.compareTo(other.name);
81
82        if (result != 0) {
83            return result;
84        }
85
86        return value.compareTo(other.value);
87    }
88
89    /**
90     * Gets the name.
91     *
92     * @return {@code non-null;} the name
93     */
94    public CstString getName() {
95        return name;
96    }
97
98    /**
99     * Gets the value.
100     *
101     * @return {@code non-null;} the value
102     */
103    public Constant getValue() {
104        return value;
105    }
106}
107