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 import java.util.AbstractList; 35 import java.util.Collection; 36 import java.util.List; 37 import java.util.RandomAccess; 38 39 /** 40 * An abstract implementation of {@link ProtobufList} which manages mutability semantics. All mutate 41 * methods must check if the list is mutable before proceeding. Subclasses must invoke {@link 42 * #ensureIsMutable()} manually when overriding those methods. 43 * 44 * <p>This implementation assumes all subclasses are array based, supporting random access. 45 */ 46 abstract class AbstractProtobufList<E> extends AbstractList<E> implements ProtobufList<E> { 47 48 protected static final int DEFAULT_CAPACITY = 10; 49 50 /** Whether or not this list is modifiable. */ 51 private boolean isMutable; 52 53 /** Constructs a mutable list by default. */ AbstractProtobufList()54 AbstractProtobufList() { 55 isMutable = true; 56 } 57 58 @Override equals(Object o)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 hashCode()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 add(E e)97 public boolean add(E e) { 98 ensureIsMutable(); 99 return super.add(e); 100 } 101 102 @Override add(int index, E element)103 public void add(int index, E element) { 104 ensureIsMutable(); 105 super.add(index, element); 106 } 107 108 @Override addAll(Collection<? extends E> c)109 public boolean addAll(Collection<? extends E> c) { 110 ensureIsMutable(); 111 return super.addAll(c); 112 } 113 114 @Override addAll(int index, Collection<? extends E> c)115 public boolean addAll(int index, Collection<? extends E> c) { 116 ensureIsMutable(); 117 return super.addAll(index, c); 118 } 119 120 @Override clear()121 public void clear() { 122 ensureIsMutable(); 123 super.clear(); 124 } 125 126 @Override isModifiable()127 public boolean isModifiable() { 128 return isMutable; 129 } 130 131 @Override makeImmutable()132 public final void makeImmutable() { 133 isMutable = false; 134 } 135 136 @Override remove(int index)137 public E remove(int index) { 138 ensureIsMutable(); 139 return super.remove(index); 140 } 141 142 @Override remove(Object o)143 public boolean remove(Object o) { 144 ensureIsMutable(); 145 return super.remove(o); 146 } 147 148 @Override removeAll(Collection<?> c)149 public boolean removeAll(Collection<?> c) { 150 ensureIsMutable(); 151 return super.removeAll(c); 152 } 153 154 @Override retainAll(Collection<?> c)155 public boolean retainAll(Collection<?> c) { 156 ensureIsMutable(); 157 return super.retainAll(c); 158 } 159 160 @Override set(int index, E element)161 public E set(int index, E element) { 162 ensureIsMutable(); 163 return super.set(index, element); 164 } 165 166 /** 167 * Throws an {@link UnsupportedOperationException} if the list is immutable. Subclasses are 168 * responsible for invoking this method on mutate operations. 169 */ ensureIsMutable()170 protected void ensureIsMutable() { 171 if (!isMutable) { 172 throw new UnsupportedOperationException(); 173 } 174 } 175 } 176