• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import static com.google.protobuf.Internal.checkNotNull;
34 
35 import com.google.protobuf.Internal.IntList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.RandomAccess;
39 
40 /**
41  * An implementation of {@link IntList} on top of a primitive array.
42  *
43  * @author dweis@google.com (Daniel Weis)
44  */
45 final class IntArrayList extends AbstractProtobufList<Integer>
46     implements IntList, RandomAccess, PrimitiveNonBoxingCollection {
47 
48   private static final IntArrayList EMPTY_LIST = new IntArrayList(new int[0], 0);
49 
50   static {
EMPTY_LIST.makeImmutable()51     EMPTY_LIST.makeImmutable();
52   }
53 
emptyList()54   public static IntArrayList emptyList() {
55     return EMPTY_LIST;
56   }
57 
58   /** The backing store for the list. */
59   private int[] array;
60 
61   /**
62    * The size of the list distinct from the length of the array. That is, it is the number of
63    * elements set in the list.
64    */
65   private int size;
66 
67   /** Constructs a new mutable {@code IntArrayList} with default capacity. */
IntArrayList()68   IntArrayList() {
69     this(new int[DEFAULT_CAPACITY], 0);
70   }
71 
72   /**
73    * Constructs a new mutable {@code IntArrayList} containing the same elements as {@code other}.
74    */
IntArrayList(int[] other, int size)75   private IntArrayList(int[] other, int size) {
76     array = other;
77     this.size = size;
78   }
79 
80   @Override
removeRange(int fromIndex, int toIndex)81   protected void removeRange(int fromIndex, int toIndex) {
82     ensureIsMutable();
83     if (toIndex < fromIndex) {
84       throw new IndexOutOfBoundsException("toIndex < fromIndex");
85     }
86 
87     System.arraycopy(array, toIndex, array, fromIndex, size - toIndex);
88     size -= (toIndex - fromIndex);
89     modCount++;
90   }
91 
92   @Override
equals(Object o)93   public boolean equals(Object o) {
94     if (this == o) {
95       return true;
96     }
97     if (!(o instanceof IntArrayList)) {
98       return super.equals(o);
99     }
100     IntArrayList other = (IntArrayList) o;
101     if (size != other.size) {
102       return false;
103     }
104 
105     final int[] arr = other.array;
106     for (int i = 0; i < size; i++) {
107       if (array[i] != arr[i]) {
108         return false;
109       }
110     }
111 
112     return true;
113   }
114 
115   @Override
hashCode()116   public int hashCode() {
117     int result = 1;
118     for (int i = 0; i < size; i++) {
119       result = (31 * result) + array[i];
120     }
121     return result;
122   }
123 
124   @Override
mutableCopyWithCapacity(int capacity)125   public IntList mutableCopyWithCapacity(int capacity) {
126     if (capacity < size) {
127       throw new IllegalArgumentException();
128     }
129     return new IntArrayList(Arrays.copyOf(array, capacity), size);
130   }
131 
132   @Override
get(int index)133   public Integer get(int index) {
134     return getInt(index);
135   }
136 
137   @Override
getInt(int index)138   public int getInt(int index) {
139     ensureIndexInRange(index);
140     return array[index];
141   }
142 
143   @Override
size()144   public int size() {
145     return size;
146   }
147 
148   @Override
set(int index, Integer element)149   public Integer set(int index, Integer element) {
150     return setInt(index, element);
151   }
152 
153   @Override
setInt(int index, int element)154   public int setInt(int index, int element) {
155     ensureIsMutable();
156     ensureIndexInRange(index);
157     int previousValue = array[index];
158     array[index] = element;
159     return previousValue;
160   }
161 
162   @Override
add(int index, Integer element)163   public void add(int index, Integer element) {
164     addInt(index, element);
165   }
166 
167   /** Like {@link #add(Integer)} but more efficient in that it doesn't box the element. */
168   @Override
addInt(int element)169   public void addInt(int element) {
170     addInt(size, element);
171   }
172 
173   /** Like {@link #add(int, Integer)} but more efficient in that it doesn't box the element. */
addInt(int index, int element)174   private void addInt(int index, int element) {
175     ensureIsMutable();
176     if (index < 0 || index > size) {
177       throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
178     }
179 
180     if (size < array.length) {
181       // Shift everything over to make room
182       System.arraycopy(array, index, array, index + 1, size - index);
183     } else {
184       // Resize to 1.5x the size
185       int length = ((size * 3) / 2) + 1;
186       int[] newArray = new int[length];
187 
188       // Copy the first part directly
189       System.arraycopy(array, 0, newArray, 0, index);
190 
191       // Copy the rest shifted over by one to make room
192       System.arraycopy(array, index, newArray, index + 1, size - index);
193       array = newArray;
194     }
195 
196     array[index] = element;
197     size++;
198     modCount++;
199   }
200 
201   @Override
addAll(Collection<? extends Integer> collection)202   public boolean addAll(Collection<? extends Integer> collection) {
203     ensureIsMutable();
204 
205     checkNotNull(collection);
206 
207     // We specialize when adding another IntArrayList to avoid boxing elements.
208     if (!(collection instanceof IntArrayList)) {
209       return super.addAll(collection);
210     }
211 
212     IntArrayList list = (IntArrayList) collection;
213     if (list.size == 0) {
214       return false;
215     }
216 
217     int overflow = Integer.MAX_VALUE - size;
218     if (overflow < list.size) {
219       // We can't actually represent a list this large.
220       throw new OutOfMemoryError();
221     }
222 
223     int newSize = size + list.size;
224     if (newSize > array.length) {
225       array = Arrays.copyOf(array, newSize);
226     }
227 
228     System.arraycopy(list.array, 0, array, size, list.size);
229     size = newSize;
230     modCount++;
231     return true;
232   }
233 
234   @Override
remove(Object o)235   public boolean remove(Object o) {
236     ensureIsMutable();
237     for (int i = 0; i < size; i++) {
238       if (o.equals(array[i])) {
239         System.arraycopy(array, i + 1, array, i, size - i - 1);
240         size--;
241         modCount++;
242         return true;
243       }
244     }
245     return false;
246   }
247 
248   @Override
remove(int index)249   public Integer remove(int index) {
250     ensureIsMutable();
251     ensureIndexInRange(index);
252     int value = array[index];
253     if (index < size - 1) {
254       System.arraycopy(array, index + 1, array, index, size - index - 1);
255     }
256     size--;
257     modCount++;
258     return value;
259   }
260 
261   /**
262    * Ensures that the provided {@code index} is within the range of {@code [0, size]}. Throws an
263    * {@link IndexOutOfBoundsException} if it is not.
264    *
265    * @param index the index to verify is in range
266    */
ensureIndexInRange(int index)267   private void ensureIndexInRange(int index) {
268     if (index < 0 || index >= size) {
269       throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
270     }
271   }
272 
makeOutOfBoundsExceptionMessage(int index)273   private String makeOutOfBoundsExceptionMessage(int index) {
274     return "Index:" + index + ", Size:" + size;
275   }
276 }
277