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.cf.code;
18
19import com.android.dx.util.Hex;
20import com.android.dx.util.LabeledList;
21
22/**
23 * List of {@link ByteBlock} instances.
24 */
25public final class ByteBlockList extends LabeledList {
26
27    /**
28     * Constructs an instance.
29     *
30     * @param size {@code >= 0;} the number of elements to be in the list
31     */
32    public ByteBlockList(int size) {
33        super(size);
34    }
35
36    /**
37     * Gets the indicated element. It is an error to call this with the
38     * index for an element which was never set; if you do that, this
39     * will throw {@code NullPointerException}.
40     *
41     * @param n {@code >= 0, < size();} which element
42     * @return {@code non-null;} the indicated element
43     */
44    public ByteBlock get(int n) {
45        return (ByteBlock) get0(n);
46    }
47
48    /**
49     * Gets the block with the given label.
50     *
51     * @param label the label to look for
52     * @return {@code non-null;} the block with the given label
53     */
54    public ByteBlock labelToBlock(int label) {
55        int idx = indexOfLabel(label);
56
57        if (idx < 0) {
58            throw new IllegalArgumentException("no such label: "
59                    + Hex.u2(label));
60        }
61
62        return get(idx);
63    }
64
65    /**
66     * Sets the element at the given index.
67     *
68     * @param n {@code >= 0, < size();} which element
69     * @param bb {@code null-ok;} the value to store
70     */
71    public void set(int n, ByteBlock bb) {
72        super.set(n, bb);
73    }
74}
75