1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
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
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.
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.
31
package com.google.protobuf;
33
import java.util.List;
34
import java.util.AbstractList;
35
import java.util.ArrayList;
36
import java.util.RandomAccess;
37
import java.util.Collection;
40
* An implementation of {@link LazyStringList} that wraps an ArrayList. Each
41
* element is either a ByteString or a String. It caches the last one requested
42
* which is most likely the one needed next. This minimizes memory usage while
43
* satisfying the most common use cases.
45
* <strong>Note that this implementation is not synchronized.</strong>
46
* If multiple threads access an <tt>ArrayList</tt> instance concurrently,
47
* and at least one of the threads modifies the list structurally, it
48
* <i>must</i> be synchronized externally. (A structural modification is
49
* any operation that adds or deletes one or more elements, or explicitly
50
* resizes the backing array; merely setting the value of an element is not
51
* a structural modification.) This is typically accomplished by
52
* synchronizing on some object that naturally encapsulates the list.
54
* If the implementation is accessed via concurrent reads, this is thread safe.
55
* Conversions are done in a thread safe manner. It's possible that the
56
* conversion may happen more than once if two threads attempt to access the
57
* same element and the modifications were not visible to each other, but this
58
* will not result in any corruption of the list or change in behavior other
61
* @author jonp@google.com (Jon Perlow)
63
public class LazyStringArrayList extends AbstractList<String>
64
implements LazyStringList, RandomAccess {
66
public final static LazyStringList EMPTY = new UnmodifiableLazyStringList(
67
new LazyStringArrayList());
69
private final List<Object> list;
71
public LazyStringArrayList() {
72
list = new ArrayList<Object>();
75
public LazyStringArrayList(List<String> from) {
76
list = new ArrayList<Object>(from);
80
public String get(int index) {
81
Object o = list.get(index);
82
if (o instanceof String) {
85
ByteString bs = (ByteString) o;
86
String s = bs.toStringUtf8();
87
if (Internal.isValidUtf8(bs)) {
100
public String set(int index, String s) {
101
Object o = list.set(index, s);
106
public void add(int index, String element) {
107
list.add(index, element);
112
public boolean addAll(int index, Collection<? extends String> c) {
113
boolean ret = list.addAll(index, c);
119
public String remove(int index) {
120
Object o = list.remove(index);
125
public void clear() {
131
public void add(ByteString element) {
137
public ByteString getByteString(int index) {
138
Object o = list.get(index);
139
if (o instanceof String) {
140
ByteString b = ByteString.copyFromUtf8((String) o);
144
return (ByteString) o;
148
private String asString(Object o) {
149
if (o instanceof String) {
152
return ((ByteString) o).toStringUtf8();