• 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 com.google.protobuf.Internal.BooleanList;
34 
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.RandomAccess;
38 
39 /**
40  * An implementation of {@link BooleanList} on top of a primitive array.
41  *
42  * @author dweis@google.com (Daniel Weis)
43  */
44 final class BooleanArrayList
45     extends AbstractProtobufList<Boolean>
46     implements BooleanList, RandomAccess {
47 
48   private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList();
49   static {
EMPTY_LIST.makeImmutable()50     EMPTY_LIST.makeImmutable();
51   }
52 
emptyList()53   public static BooleanArrayList emptyList() {
54     return EMPTY_LIST;
55   }
56 
57   /**
58    * The backing store for the list.
59    */
60   private boolean[] array;
61 
62   /**
63    * The size of the list distinct from the length of the array. That is, it is the number of
64    * elements set in the list.
65    */
66   private int size;
67 
68   /**
69    * Constructs a new mutable {@code BooleanArrayList} with default capacity.
70    */
BooleanArrayList()71   BooleanArrayList() {
72     this(new boolean[DEFAULT_CAPACITY], 0);
73   }
74 
75   /**
76    * Constructs a new mutable {@code BooleanArrayList}
77    * containing the same elements as {@code other}.
78    */
BooleanArrayList(boolean[] other, int size)79   private BooleanArrayList(boolean[] other, int size) {
80     array = other;
81     this.size = size;
82   }
83 
84   @Override
equals(Object o)85   public boolean equals(Object o) {
86     if (this == o) {
87       return true;
88     }
89     if (!(o instanceof BooleanArrayList)) {
90       return super.equals(o);
91     }
92     BooleanArrayList other = (BooleanArrayList) o;
93     if (size != other.size) {
94       return false;
95     }
96 
97     final boolean[] arr = other.array;
98     for (int i = 0; i < size; i++) {
99       if (array[i] != arr[i]) {
100         return false;
101       }
102     }
103 
104     return true;
105   }
106 
107   @Override
hashCode()108   public int hashCode() {
109     int result = 1;
110     for (int i = 0; i < size; i++) {
111       result = (31 * result) + Internal.hashBoolean(array[i]);
112     }
113     return result;
114   }
115 
116   @Override
mutableCopyWithCapacity(int capacity)117   public BooleanList mutableCopyWithCapacity(int capacity) {
118     if (capacity < size) {
119       throw new IllegalArgumentException();
120     }
121     return new BooleanArrayList(Arrays.copyOf(array, capacity), size);
122   }
123 
124   @Override
get(int index)125   public Boolean get(int index) {
126     return getBoolean(index);
127   }
128 
129   @Override
getBoolean(int index)130   public boolean getBoolean(int index) {
131     ensureIndexInRange(index);
132     return array[index];
133   }
134 
135   @Override
size()136   public int size() {
137     return size;
138   }
139 
140   @Override
set(int index, Boolean element)141   public Boolean set(int index, Boolean element) {
142     return setBoolean(index, element);
143   }
144 
145   @Override
setBoolean(int index, boolean element)146   public boolean setBoolean(int index, boolean element) {
147     ensureIsMutable();
148     ensureIndexInRange(index);
149     boolean previousValue = array[index];
150     array[index] = element;
151     return previousValue;
152   }
153 
154   @Override
add(int index, Boolean element)155   public void add(int index, Boolean element) {
156     addBoolean(index, element);
157   }
158 
159   /**
160    * Like {@link #add(Boolean)} but more efficient in that it doesn't box the element.
161    */
162   @Override
addBoolean(boolean element)163   public void addBoolean(boolean element) {
164     addBoolean(size, element);
165   }
166 
167   /**
168    * Like {@link #add(int, Boolean)} but more efficient in that it doesn't box the element.
169    */
addBoolean(int index, boolean element)170   private void addBoolean(int index, boolean element) {
171     ensureIsMutable();
172     if (index < 0 || index > size) {
173       throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
174     }
175 
176     if (size < array.length) {
177       // Shift everything over to make room
178       System.arraycopy(array, index, array, index + 1, size - index);
179     } else {
180       // Resize to 1.5x the size
181       int length = ((size * 3) / 2) + 1;
182       boolean[] newArray = new boolean[length];
183 
184       // Copy the first part directly
185       System.arraycopy(array, 0, newArray, 0, index);
186 
187       // Copy the rest shifted over by one to make room
188       System.arraycopy(array, index, newArray, index + 1, size - index);
189       array = newArray;
190     }
191 
192     array[index] = element;
193     size++;
194     modCount++;
195   }
196 
197   @Override
addAll(Collection<? extends Boolean> collection)198   public boolean addAll(Collection<? extends Boolean> collection) {
199     ensureIsMutable();
200 
201     if (collection == null) {
202       throw new NullPointerException();
203     }
204 
205     // We specialize when adding another BooleanArrayList to avoid boxing elements.
206     if (!(collection instanceof BooleanArrayList)) {
207       return super.addAll(collection);
208     }
209 
210     BooleanArrayList list = (BooleanArrayList) collection;
211     if (list.size == 0) {
212       return false;
213     }
214 
215     int overflow = Integer.MAX_VALUE - size;
216     if (overflow < list.size) {
217       // We can't actually represent a list this large.
218       throw new OutOfMemoryError();
219     }
220 
221     int newSize = size + list.size;
222     if (newSize > array.length) {
223       array = Arrays.copyOf(array, newSize);
224     }
225 
226     System.arraycopy(list.array, 0, array, size, list.size);
227     size = newSize;
228     modCount++;
229     return true;
230   }
231 
232   @Override
remove(Object o)233   public boolean remove(Object o) {
234     ensureIsMutable();
235     for (int i = 0; i < size; i++) {
236       if (o.equals(array[i])) {
237         System.arraycopy(array, i + 1, array, i, size - i);
238         size--;
239         modCount++;
240         return true;
241       }
242     }
243     return false;
244   }
245 
246   @Override
remove(int index)247   public Boolean remove(int index) {
248     ensureIsMutable();
249     ensureIndexInRange(index);
250     boolean value = array[index];
251     System.arraycopy(array, index + 1, array, index, size - index);
252     size--;
253     modCount++;
254     return value;
255   }
256 
257   /**
258    * Ensures that the provided {@code index} is within the range of {@code [0, size]}. Throws an
259    * {@link IndexOutOfBoundsException} if it is not.
260    *
261    * @param index the index to verify is in range
262    */
ensureIndexInRange(int index)263   private void ensureIndexInRange(int index) {
264     if (index < 0 || index >= size) {
265       throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
266     }
267   }
268 
makeOutOfBoundsExceptionMessage(int index)269   private String makeOutOfBoundsExceptionMessage(int index) {
270     return "Index:" + index + ", Size:" + size;
271   }
272 }
273