AnnotationsList.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
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.util.FixedSizeList;
20
21/**
22 * List of {@link Annotations} instances.
23 */
24public final class AnnotationsList
25        extends FixedSizeList {
26    /** {@code non-null;} immutable empty instance */
27    public static final AnnotationsList EMPTY = new AnnotationsList(0);
28
29    /**
30     * Constructs an immutable instance which is the combination of
31     * the two given instances. The two instances must each have the
32     * same number of elements, and each pair of elements must contain
33     * disjoint sets of types.
34     *
35     * @param list1 {@code non-null;} an instance
36     * @param list2 {@code non-null;} the other instance
37     * @return {@code non-null;} the combination
38     */
39    public static AnnotationsList combine(AnnotationsList list1,
40            AnnotationsList list2) {
41        int size = list1.size();
42
43        if (size != list2.size()) {
44            throw new IllegalArgumentException("list1.size() != list2.size()");
45        }
46
47        AnnotationsList result = new AnnotationsList(size);
48
49        for (int i = 0; i < size; i++) {
50            Annotations a1 = list1.get(i);
51            Annotations a2 = list2.get(i);
52            result.set(i, Annotations.combine(a1, a2));
53        }
54
55        result.setImmutable();
56        return result;
57    }
58
59    /**
60     * Constructs an instance. All indices initially contain {@code null}.
61     *
62     * @param size the size of the list
63     */
64    public AnnotationsList(int size) {
65        super(size);
66    }
67
68    /**
69     * Gets the element at the given index. It is an error to call
70     * this with the index for an element which was never set; if you
71     * do that, this will throw {@code NullPointerException}.
72     *
73     * @param n {@code >= 0, < size();} which index
74     * @return {@code non-null;} element at that index
75     */
76    public Annotations get(int n) {
77        return (Annotations) get0(n);
78    }
79
80    /**
81     * Sets the element at the given index. The given element must be
82     * immutable.
83     *
84     * @param n {@code >= 0, < size();} which index
85     * @param a {@code null-ok;} the element to set at {@code n}
86     */
87    public void set(int n, Annotations a) {
88        a.throwIfMutable();
89        set0(n, a);
90    }
91}
92