1/*
2 * Copyright (C) 2011 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.google.dexmaker;
18
19import com.android.dx.rop.cst.CstFieldRef;
20import com.android.dx.rop.cst.CstNat;
21import com.android.dx.rop.cst.CstString;
22
23/**
24 * Identifies a field.
25 *
26 * @param <D> the type declaring this field
27 * @param <V> the type of value this field holds
28 */
29public final class FieldId<D, V> {
30    final TypeId<D> declaringType;
31    final TypeId<V> type;
32    final String name;
33
34    /** cached converted state */
35    final CstNat nat;
36    final CstFieldRef constant;
37
38    FieldId(TypeId<D> declaringType, TypeId<V> type, String name) {
39        if (declaringType == null || type == null || name == null) {
40            throw new NullPointerException();
41        }
42        this.declaringType = declaringType;
43        this.type = type;
44        this.name = name;
45        this.nat = new CstNat(new CstString(name), new CstString(type.name));
46        this.constant = new CstFieldRef(declaringType.constant, nat);
47    }
48
49    public TypeId<D> getDeclaringType() {
50        return declaringType;
51    }
52
53    public TypeId<V> getType() {
54        return type;
55    }
56
57    public String getName() {
58        return name;
59    }
60
61    @Override public boolean equals(Object o) {
62        return o instanceof FieldId
63                && ((FieldId<?, ?>) o).declaringType.equals(declaringType)
64                && ((FieldId<?, ?>) o).name.equals(name);
65    }
66
67    @Override public int hashCode() {
68        return declaringType.hashCode() + 37 * name.hashCode();
69    }
70
71    @Override public String toString() {
72        return declaringType + "." + name;
73    }
74}
75