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.cst;
18
19import com.android.dx.rop.type.Type;
20import com.android.dx.util.FixedSizeList;
21
22/**
23 * Constant type to represent a fixed array of other constants.
24 */
25public final class CstArray extends Constant {
26    /** {@code non-null;} the actual list of contents */
27    private final List list;
28
29    /**
30     * Constructs an instance.
31     *
32     * @param list {@code non-null;} the actual list of contents
33     */
34    public CstArray(List list) {
35        if (list == null) {
36            throw new NullPointerException("list == null");
37        }
38
39        list.throwIfMutable();
40
41        this.list = list;
42    }
43
44    /** {@inheritDoc} */
45    @Override
46    public boolean equals(Object other) {
47        if (! (other instanceof CstArray)) {
48            return false;
49        }
50
51        return list.equals(((CstArray) other).list);
52    }
53
54    /** {@inheritDoc} */
55    @Override
56    public int hashCode() {
57        return list.hashCode();
58    }
59
60    /** {@inheritDoc} */
61    @Override
62    protected int compareTo0(Constant other) {
63        return list.compareTo(((CstArray) other).list);
64    }
65
66    /** {@inheritDoc} */
67    @Override
68    public String toString() {
69        return list.toString("array{", ", ", "}");
70    }
71
72    /** {@inheritDoc} */
73    @Override
74    public String typeName() {
75        return "array";
76    }
77
78    /** {@inheritDoc} */
79    @Override
80    public boolean isCategory2() {
81        return false;
82    }
83
84    /** {@inheritDoc} */
85    public String toHuman() {
86        return list.toHuman("{", ", ", "}");
87    }
88
89    /**
90     * Get the underlying list.
91     *
92     * @return {@code non-null;} the list
93     */
94    public List getList() {
95        return list;
96    }
97
98    /**
99     * List of {@link Constant} instances.
100     */
101    public static final class List
102            extends FixedSizeList implements Comparable<List> {
103        /**
104         * Constructs an instance. All indices initially contain
105         * {@code null}.
106         *
107         * @param size the size of the list
108         */
109        public List(int size) {
110            super(size);
111        }
112
113        /** {@inheritDoc} */
114        public int compareTo(List other) {
115            int thisSize = size();
116            int otherSize = other.size();
117            int compareSize = (thisSize < otherSize) ? thisSize : otherSize;
118
119            for (int i = 0; i < compareSize; i++) {
120                Constant thisItem = (Constant) get0(i);
121                Constant otherItem = (Constant) other.get0(i);
122                int compare = thisItem.compareTo(otherItem);
123                if (compare != 0) {
124                    return compare;
125                }
126            }
127
128            if (thisSize < otherSize) {
129                return -1;
130            } else if (thisSize > otherSize) {
131                return 1;
132            }
133
134            return 0;
135        }
136
137        /**
138         * Gets the element at the given index. It is an error to call
139         * this with the index for an element which was never set; if you
140         * do that, this will throw {@code NullPointerException}.
141         *
142         * @param n {@code >= 0, < size();} which index
143         * @return {@code non-null;} element at that index
144         */
145        public Constant get(int n) {
146            return (Constant) get0(n);
147        }
148
149        /**
150         * Sets the element at the given index.
151         *
152         * @param n {@code >= 0, < size();} which index
153         * @param a {@code null-ok;} the element to set at {@code n}
154         */
155        public void set(int n, Constant a) {
156            set0(n, a);
157        }
158    }
159}
160