• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.dx.rop.cst;
18 
19 import com.android.dx.util.FixedSizeList;
20 
21 /**
22  * Constant type to represent a fixed array of other constants.
23  */
24 public 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      */
CstArray(List list)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
equals(Object other)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
hashCode()55     public int hashCode() {
56         return list.hashCode();
57     }
58 
59     /** {@inheritDoc} */
60     @Override
compareTo0(Constant other)61     protected int compareTo0(Constant other) {
62         return list.compareTo(((CstArray) other).list);
63     }
64 
65     /** {@inheritDoc} */
66     @Override
toString()67     public String toString() {
68         return list.toString("array{", ", ", "}");
69     }
70 
71     /** {@inheritDoc} */
72     @Override
typeName()73     public String typeName() {
74         return "array";
75     }
76 
77     /** {@inheritDoc} */
78     @Override
isCategory2()79     public boolean isCategory2() {
80         return false;
81     }
82 
83     /** {@inheritDoc} */
84     @Override
toHuman()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      */
getList()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          */
List(int size)109         public List(int size) {
110             super(size);
111         }
112 
113         /** {@inheritDoc} */
114         @Override
compareTo(List other)115         public int compareTo(List other) {
116             int thisSize = size();
117             int otherSize = other.size();
118             int compareSize = (thisSize < otherSize) ? thisSize : otherSize;
119 
120             for (int i = 0; i < compareSize; i++) {
121                 Constant thisItem = (Constant) get0(i);
122                 Constant otherItem = (Constant) other.get0(i);
123                 int compare = thisItem.compareTo(otherItem);
124                 if (compare != 0) {
125                     return compare;
126                 }
127             }
128 
129             if (thisSize < otherSize) {
130                 return -1;
131             } else if (thisSize > otherSize) {
132                 return 1;
133             }
134 
135             return 0;
136         }
137 
138         /**
139          * Gets the element at the given index. It is an error to call
140          * this with the index for an element which was never set; if you
141          * do that, this will throw {@code NullPointerException}.
142          *
143          * @param n {@code >= 0, < size();} which index
144          * @return {@code non-null;} element at that index
145          */
get(int n)146         public Constant get(int n) {
147             return (Constant) get0(n);
148         }
149 
150         /**
151          * Sets the element at the given index.
152          *
153          * @param n {@code >= 0, < size();} which index
154          * @param a {@code null-ok;} the element to set at {@code n}
155          */
set(int n, Constant a)156         public void set(int n, Constant a) {
157             set0(n, a);
158         }
159     }
160 }
161