• 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.ProtobufList;
34 
35 import java.util.AbstractList;
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.RandomAccess;
39 
40 /**
41  * An abstract implementation of {@link ProtobufList} which manages mutability semantics. All mutate
42  * methods must check if the list is mutable before proceeding. Subclasses must invoke
43  * {@link #ensureIsMutable()} manually when overriding those methods.
44  * <p>
45  * This implementation assumes all subclasses are array based, supporting random access.
46  */
47 abstract class AbstractProtobufList<E> extends AbstractList<E> implements ProtobufList<E> {
48 
49   protected static final int DEFAULT_CAPACITY = 10;
50 
51   /**
52    * Whether or not this list is modifiable.
53    */
54   private boolean isMutable;
55 
56   /**
57    * Constructs a mutable list by default.
58    */
AbstractProtobufList()59   AbstractProtobufList() {
60     isMutable = true;
61   }
62 
63   @Override
equals(Object o)64   public boolean equals(Object o) {
65     if (o == this) {
66       return true;
67     }
68     if (!(o instanceof List)) {
69       return false;
70     }
71     // Handle lists that do not support RandomAccess as efficiently as possible by using an iterator
72     // based approach in our super class. Otherwise our index based approach will avoid those
73     // allocations.
74     if (!(o instanceof RandomAccess)) {
75       return super.equals(o);
76     }
77 
78     List<?> other = (List<?>) o;
79     final int size = size();
80     if (size != other.size()) {
81       return false;
82     }
83     for (int i = 0; i < size; i++) {
84       if (!get(i).equals(other.get(i))) {
85         return false;
86       }
87     }
88     return true;
89   }
90 
91   @Override
hashCode()92   public int hashCode() {
93     final int size = size();
94     int hashCode = 1;
95     for (int i = 0; i < size; i++) {
96       hashCode = (31 * hashCode) + get(i).hashCode();
97     }
98     return hashCode;
99   }
100 
101   @Override
add(E e)102   public boolean add(E e) {
103     ensureIsMutable();
104     return super.add(e);
105   }
106 
107   @Override
add(int index, E element)108   public void add(int index, E element) {
109     ensureIsMutable();
110     super.add(index, element);
111   }
112 
113   @Override
addAll(Collection<? extends E> c)114   public boolean addAll(Collection<? extends E> c) {
115     ensureIsMutable();
116     return super.addAll(c);
117   }
118 
119   @Override
addAll(int index, Collection<? extends E> c)120   public boolean addAll(int index, Collection<? extends E> c) {
121     ensureIsMutable();
122     return super.addAll(index, c);
123   }
124 
125   @Override
clear()126   public void clear() {
127     ensureIsMutable();
128     super.clear();
129   }
130 
131   @Override
isModifiable()132   public boolean isModifiable() {
133     return isMutable;
134   }
135 
136   @Override
makeImmutable()137   public final void makeImmutable() {
138     isMutable = false;
139   }
140 
141   @Override
remove(int index)142   public E remove(int index) {
143     ensureIsMutable();
144     return super.remove(index);
145   }
146 
147   @Override
remove(Object o)148   public boolean remove(Object o) {
149     ensureIsMutable();
150     return super.remove(o);
151   }
152 
153   @Override
removeAll(Collection<?> c)154   public boolean removeAll(Collection<?> c) {
155     ensureIsMutable();
156     return super.removeAll(c);
157   }
158 
159   @Override
retainAll(Collection<?> c)160   public boolean retainAll(Collection<?> c) {
161     ensureIsMutable();
162     return super.retainAll(c);
163   }
164 
165   @Override
set(int index, E element)166   public E set(int index, E element) {
167     ensureIsMutable();
168     return super.set(index, element);
169   }
170 
171   /**
172    * Throws an {@link UnsupportedOperationException} if the list is immutable. Subclasses are
173    * responsible for invoking this method on mutate operations.
174    */
ensureIsMutable()175   protected void ensureIsMutable() {
176     if (!isMutable) {
177       throw new UnsupportedOperationException();
178     }
179   }
180 }
181