1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // http://code.google.com/p/protobuf/ 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.RandomAccess; 35 import java.util.List; 36 import java.util.ListIterator; 37 import java.util.Iterator; 38 39 /** 40 * An implementation of {@link LazyStringList} that wraps another 41 * {@link LazyStringList} such that it cannot be modified via the wrapper. 42 * 43 * @author jonp@google.com (Jon Perlow) 44 */ 45 public class UnmodifiableLazyStringList extends AbstractList<String> 46 implements LazyStringList, RandomAccess { 47 48 private final LazyStringList list; 49 UnmodifiableLazyStringList(LazyStringList list)50 public UnmodifiableLazyStringList(LazyStringList list) { 51 this.list = list; 52 } 53 54 @Override get(int index)55 public String get(int index) { 56 return list.get(index); 57 } 58 59 @Override size()60 public int size() { 61 return list.size(); 62 } 63 64 //@Override (Java 1.6 override semantics, but we must support 1.5) getByteString(int index)65 public ByteString getByteString(int index) { 66 return list.getByteString(index); 67 } 68 69 //@Override (Java 1.6 override semantics, but we must support 1.5) add(ByteString element)70 public void add(ByteString element) { 71 throw new UnsupportedOperationException(); 72 } 73 74 //@Override (Java 1.6 override semantics, but we must support 1.5) listIterator(final int index)75 public ListIterator<String> listIterator(final int index) { 76 return new ListIterator<String>() { 77 ListIterator<String> iter = list.listIterator(index); 78 79 //@Override (Java 1.6 override semantics, but we must support 1.5) 80 public boolean hasNext() { 81 return iter.hasNext(); 82 } 83 84 //@Override (Java 1.6 override semantics, but we must support 1.5) 85 public String next() { 86 return iter.next(); 87 } 88 89 //@Override (Java 1.6 override semantics, but we must support 1.5) 90 public boolean hasPrevious() { 91 return iter.hasPrevious(); 92 } 93 94 //@Override (Java 1.6 override semantics, but we must support 1.5) 95 public String previous() { 96 return iter.previous(); 97 } 98 99 //@Override (Java 1.6 override semantics, but we must support 1.5) 100 public int nextIndex() { 101 return iter.nextIndex(); 102 } 103 104 //@Override (Java 1.6 override semantics, but we must support 1.5) 105 public int previousIndex() { 106 return iter.previousIndex(); 107 } 108 109 //@Override (Java 1.6 override semantics, but we must support 1.5) 110 public void remove() { 111 throw new UnsupportedOperationException(); 112 } 113 114 //@Override (Java 1.6 override semantics, but we must support 1.5) 115 public void set(String o) { 116 throw new UnsupportedOperationException(); 117 } 118 119 //@Override (Java 1.6 override semantics, but we must support 1.5) 120 public void add(String o) { 121 throw new UnsupportedOperationException(); 122 } 123 }; 124 } 125 126 @Override iterator()127 public Iterator<String> iterator() { 128 return new Iterator<String>() { 129 Iterator<String> iter = list.iterator(); 130 131 //@Override (Java 1.6 override semantics, but we must support 1.5) 132 public boolean hasNext() { 133 return iter.hasNext(); 134 } 135 136 //@Override (Java 1.6 override semantics, but we must support 1.5) 137 public String next() { 138 return iter.next(); 139 } 140 141 //@Override (Java 1.6 override semantics, but we must support 1.5) 142 public void remove() { 143 throw new UnsupportedOperationException(); 144 } 145 }; 146 } 147 148 public List<?> getUnderlyingElements() { 149 // The returned value is already unmodifiable. 150 return list.getUnderlyingElements(); 151 } 152 } 153