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