summaryrefslogtreecommitdiff
path: root/dexgen/src/com/android/dexgen/rop/annotation/Annotations.java
blob: a7eca04966afa02f984d5e50bff48ef4e63444e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dexgen.rop.annotation;

import com.android.dexgen.rop.cst.CstType;
import com.android.dexgen.util.MutabilityControl;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.TreeMap;

/**
 * List of {@link Annotation} instances.
 */
public final class Annotations extends MutabilityControl
        implements Comparable<Annotations> {
    /** {@code non-null;} immutable empty instance */
    public static final Annotations EMPTY = new Annotations();

    static {
        EMPTY.setImmutable();
    }

    /** {@code non-null;} map from types to annotations */
    private final TreeMap<CstType, Annotation> annotations;

    /**
     * Constructs an immutable instance which is the combination of the
     * two given instances. The two instances must contain disjoint sets
     * of types.
     *
     * @param a1 {@code non-null;} an instance
     * @param a2 {@code non-null;} the other instance
     * @return {@code non-null;} the combination
     * @throws IllegalArgumentException thrown if there is a duplicate type
     */
    public static Annotations combine(Annotations a1, Annotations a2) {
        Annotations result = new Annotations();

        result.addAll(a1);
        result.addAll(a2);
        result.setImmutable();

        return result;
    }

    /**
     * Constructs an immutable instance which is the combination of the
     * given instance with the given additional annotation. The latter's
     * type must not already appear in the former.
     *
     * @param annotations {@code non-null;} the instance to augment
     * @param annotation {@code non-null;} the additional annotation
     * @return {@code non-null;} the combination
     * @throws IllegalArgumentException thrown if there is a duplicate type
     */
    public static Annotations combine(Annotations annotations,
            Annotation annotation) {
        Annotations result = new Annotations();

        result.addAll(annotations);
        result.add(annotation);
        result.setImmutable();

        return result;
    }

    /**
     * Constructs an empty instance.
     */
    public Annotations() {
        annotations = new TreeMap<CstType, Annotation>();
    }

    /** {@inheritDoc} */
    @Override
    public int hashCode() {
        return annotations.hashCode();
    }

    /** {@inheritDoc} */
    @Override
    public boolean equals(Object other) {
        if (! (other instanceof Annotations)) {
            return false;
        }

        Annotations otherAnnotations = (Annotations) other;

        return annotations.equals(otherAnnotations.annotations);
    }

    /** {@inheritDoc} */
    public int compareTo(Annotations other) {
        Iterator<Annotation> thisIter = annotations.values().iterator();
        Iterator<Annotation> otherIter = other.annotations.values().iterator();

        while (thisIter.hasNext() && otherIter.hasNext()) {
            Annotation thisOne = thisIter.next();
            Annotation otherOne = otherIter.next();

            int result = thisOne.compareTo(otherOne);
            if (result != 0) {
                return result;
            }
        }

        if (thisIter.hasNext()) {
            return 1;
        } else if (otherIter.hasNext()) {
            return -1;
        }

        return 0;
    }

    /** {@inheritDoc} */
    public String toString() {
        StringBuilder sb = new StringBuilder();
        boolean first = true;

        sb.append("annotations{");

        for (Annotation a : annotations.values()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append(a.toHuman());
        }

        sb.append("}");
        return sb.toString();
    }

    /**
     * Gets the number of elements in this instance.
     *
     * @return {@code >= 0;} the size
     */
    public int size() {
        return annotations.size();
    }

    /**
     * Adds an element to this instance. There must not already be an
     * element of the same type.
     *
     * @param annotation {@code non-null;} the element to add
     * @throws IllegalArgumentException thrown if there is a duplicate type
     */
    public void add(Annotation annotation) {
        throwIfImmutable();

        if (annotation == null) {
            throw new NullPointerException("annotation == null");
        }

        CstType type = annotation.getType();

        if (annotations.containsKey(type)) {
            throw new IllegalArgumentException("duplicate type: " +
                    type.toHuman());
        }

        annotations.put(type, annotation);
    }

    /**
     * Adds all of the elements of the given instance to this one. The
     * instances must not have any duplicate types.
     *
     * @param toAdd {@code non-null;} the annotations to add
     * @throws IllegalArgumentException thrown if there is a duplicate type
     */
    public void addAll(Annotations toAdd) {
        throwIfImmutable();

        if (toAdd == null) {
            throw new NullPointerException("toAdd == null");
        }

        for (Annotation a : toAdd.annotations.values()) {
            add(a);
        }
    }

    /**
     * Gets the set of annotations contained in this instance. The
     * result is always unmodifiable.
     *
     * @return {@code non-null;} the set of annotations
     */
    public Collection<Annotation> getAnnotations() {
        return Collections.unmodifiableCollection(annotations.values());
    }
}