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