AbstractProtobufList.java
Go to the documentation of this file.
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 
34 import java.util.AbstractList;
35 import java.util.Collection;
36 import java.util.List;
37 import java.util.RandomAccess;
38 
46 abstract class AbstractProtobufList<E> extends AbstractList<E> implements ProtobufList<E> {
47 
48  protected static final int DEFAULT_CAPACITY = 10;
49 
51  private boolean isMutable;
52 
54  AbstractProtobufList() {
55  isMutable = true;
56  }
57 
58  @Override
59  public boolean equals(Object o) {
60  if (o == this) {
61  return true;
62  }
63  if (!(o instanceof List)) {
64  return false;
65  }
66  // Handle lists that do not support RandomAccess as efficiently as possible by using an iterator
67  // based approach in our super class. Otherwise our index based approach will avoid those
68  // allocations.
69  if (!(o instanceof RandomAccess)) {
70  return super.equals(o);
71  }
72 
73  List<?> other = (List<?>) o;
74  final int size = size();
75  if (size != other.size()) {
76  return false;
77  }
78  for (int i = 0; i < size; i++) {
79  if (!get(i).equals(other.get(i))) {
80  return false;
81  }
82  }
83  return true;
84  }
85 
86  @Override
87  public int hashCode() {
88  final int size = size();
89  int hashCode = 1;
90  for (int i = 0; i < size; i++) {
91  hashCode = (31 * hashCode) + get(i).hashCode();
92  }
93  return hashCode;
94  }
95 
96  @Override
97  public boolean add(E e) {
98  ensureIsMutable();
99  return super.add(e);
100  }
101 
102  @Override
103  public void add(int index, E element) {
104  ensureIsMutable();
105  super.add(index, element);
106  }
107 
108  @Override
109  public boolean addAll(Collection<? extends E> c) {
110  ensureIsMutable();
111  return super.addAll(c);
112  }
113 
114  @Override
115  public boolean addAll(int index, Collection<? extends E> c) {
116  ensureIsMutable();
117  return super.addAll(index, c);
118  }
119 
120  @Override
121  public void clear() {
122  ensureIsMutable();
123  super.clear();
124  }
125 
126  @Override
127  public boolean isModifiable() {
128  return isMutable;
129  }
130 
131  @Override
132  public final void makeImmutable() {
133  isMutable = false;
134  }
135 
136  @Override
137  public E remove(int index) {
138  ensureIsMutable();
139  return super.remove(index);
140  }
141 
142  @Override
143  public boolean remove(Object o) {
144  ensureIsMutable();
145  return super.remove(o);
146  }
147 
148  @Override
149  public boolean removeAll(Collection<?> c) {
150  ensureIsMutable();
151  return super.removeAll(c);
152  }
153 
154  @Override
155  public boolean retainAll(Collection<?> c) {
156  ensureIsMutable();
157  return super.retainAll(c);
158  }
159 
160  @Override
161  public E set(int index, E element) {
162  ensureIsMutable();
163  return super.set(index, element);
164  }
165 
170  protected void ensureIsMutable() {
171  if (!isMutable) {
172  throw new UnsupportedOperationException();
173  }
174  }
175 }
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
size
#define size
Definition: glcorearb.h:2944
i
int i
Definition: gmock-matchers_test.cc:764
java
com.google.protobuf.Internal.ProtobufList
Definition: Internal.java:570
size
GLsizeiptr size
Definition: glcorearb.h:2943
com.google
com
com.google.protobuf.Internal
Definition: Internal.java:54
index
GLuint index
Definition: glcorearb.h:3055


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:47