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 java.util.AbstractList; 34 import java.util.Collection; 35 import java.util.Collections; 36 import java.util.Iterator; 37 import java.util.List; 38 import java.util.ListIterator; 39 import java.util.RandomAccess; 40 41 /** 42 * An implementation of {@link LazyStringList} that wraps another 43 * {@link LazyStringList} such that it cannot be modified via the wrapper. 44 * 45 * @author jonp@google.com (Jon Perlow) 46 */ 47 public class UnmodifiableLazyStringList extends AbstractList<String> 48 implements LazyStringList, RandomAccess { 49 50 private final LazyStringList list; 51 UnmodifiableLazyStringList(LazyStringList list)52 public UnmodifiableLazyStringList(LazyStringList list) { 53 this.list = list; 54 } 55 56 @Override get(int index)57 public String get(int index) { 58 return list.get(index); 59 } 60 61 @Override getRaw(int index)62 public Object getRaw(int index) { 63 return list.getRaw(index); 64 } 65 66 @Override size()67 public int size() { 68 return list.size(); 69 } 70 71 @Override getByteString(int index)72 public ByteString getByteString(int index) { 73 return list.getByteString(index); 74 } 75 76 @Override add(ByteString element)77 public void add(ByteString element) { 78 throw new UnsupportedOperationException(); 79 } 80 81 @Override set(int index, ByteString element)82 public void set(int index, ByteString element) { 83 throw new UnsupportedOperationException(); 84 } 85 86 @Override addAllByteString(Collection<? extends ByteString> element)87 public boolean addAllByteString(Collection<? extends ByteString> element) { 88 throw new UnsupportedOperationException(); 89 } 90 91 @Override getByteArray(int index)92 public byte[] getByteArray(int index) { 93 return list.getByteArray(index); 94 } 95 96 @Override add(byte[] element)97 public void add(byte[] element) { 98 throw new UnsupportedOperationException(); 99 } 100 101 @Override set(int index, byte[] element)102 public void set(int index, byte[] element) { 103 throw new UnsupportedOperationException(); 104 } 105 106 @Override addAllByteArray(Collection<byte[]> element)107 public boolean addAllByteArray(Collection<byte[]> element) { 108 throw new UnsupportedOperationException(); 109 } 110 111 @Override listIterator(final int index)112 public ListIterator<String> listIterator(final int index) { 113 return new ListIterator<String>() { 114 ListIterator<String> iter = list.listIterator(index); 115 116 @Override 117 public boolean hasNext() { 118 return iter.hasNext(); 119 } 120 121 @Override 122 public String next() { 123 return iter.next(); 124 } 125 126 @Override 127 public boolean hasPrevious() { 128 return iter.hasPrevious(); 129 } 130 131 @Override 132 public String previous() { 133 return iter.previous(); 134 } 135 136 @Override 137 public int nextIndex() { 138 return iter.nextIndex(); 139 } 140 141 @Override 142 public int previousIndex() { 143 return iter.previousIndex(); 144 } 145 146 @Override 147 public void remove() { 148 throw new UnsupportedOperationException(); 149 } 150 151 @Override 152 public void set(String o) { 153 throw new UnsupportedOperationException(); 154 } 155 156 @Override 157 public void add(String o) { 158 throw new UnsupportedOperationException(); 159 } 160 }; 161 } 162 163 @Override iterator()164 public Iterator<String> iterator() { 165 return new Iterator<String>() { 166 Iterator<String> iter = list.iterator(); 167 168 @Override 169 public boolean hasNext() { 170 return iter.hasNext(); 171 } 172 173 @Override 174 public String next() { 175 return iter.next(); 176 } 177 178 @Override 179 public void remove() { 180 throw new UnsupportedOperationException(); 181 } 182 }; 183 } 184 185 @Override 186 public List<?> getUnderlyingElements() { 187 // The returned value is already unmodifiable. 188 return list.getUnderlyingElements(); 189 } 190 191 @Override 192 public void mergeFrom(LazyStringList other) { 193 throw new UnsupportedOperationException(); 194 } 195 196 @Override 197 public List<byte[]> asByteArrayList() { 198 return Collections.unmodifiableList(list.asByteArrayList()); 199 } 200 201 @Override 202 public List<ByteString> asByteStringList() { 203 return Collections.unmodifiableList(list.asByteStringList()); 204 } 205 206 @Override 207 public LazyStringList getUnmodifiableView() { 208 return this; 209 } 210 } 211