aboutsummaryrefslogtreecommitdiff
path: root/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java')
-rw-r--r--java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java80
1 files changed, 37 insertions, 43 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java b/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java
index d474c51e..a2de6f9c 100644
--- a/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java
+++ b/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java
@@ -39,26 +39,22 @@ import java.util.List;
import java.util.RandomAccess;
/**
- * An implementation of {@link LazyStringList} that wraps an ArrayList. Each
- * element is one of String, ByteString, or byte[]. It caches the last one
- * requested which is most likely the one needed next. This minimizes memory
- * usage while satisfying the most common use cases.
- * <p>
- * <strong>Note that this implementation is not synchronized.</strong>
- * If multiple threads access an <tt>ArrayList</tt> instance concurrently,
- * and at least one of the threads modifies the list structurally, it
- * <i>must</i> be synchronized externally. (A structural modification is
- * any operation that adds or deletes one or more elements, or explicitly
- * resizes the backing array; merely setting the value of an element is not
- * a structural modification.) This is typically accomplished by
- * synchronizing on some object that naturally encapsulates the list.
- * <p>
- * If the implementation is accessed via concurrent reads, this is thread safe.
- * Conversions are done in a thread safe manner. It's possible that the
- * conversion may happen more than once if two threads attempt to access the
- * same element and the modifications were not visible to each other, but this
- * will not result in any corruption of the list or change in behavior other
- * than performance.
+ * An implementation of {@link LazyStringList} that wraps an ArrayList. Each element is one of
+ * String, ByteString, or byte[]. It caches the last one requested which is most likely the one
+ * needed next. This minimizes memory usage while satisfying the most common use cases.
+ *
+ * <p><strong>Note that this implementation is not synchronized.</strong> If multiple threads access
+ * an <tt>ArrayList</tt> instance concurrently, and at least one of the threads modifies the list
+ * structurally, it <i>must</i> be synchronized externally. (A structural modification is any
+ * operation that adds or deletes one or more elements, or explicitly resizes the backing array;
+ * merely setting the value of an element is not a structural modification.) This is typically
+ * accomplished by synchronizing on some object that naturally encapsulates the list.
+ *
+ * <p>If the implementation is accessed via concurrent reads, this is thread safe. Conversions are
+ * done in a thread safe manner. It's possible that the conversion may happen more than once if two
+ * threads attempt to access the same element and the modifications were not visible to each other,
+ * but this will not result in any corruption of the list or change in behavior other than
+ * performance.
*
* @author jonp@google.com (Jon Perlow)
*/
@@ -66,10 +62,11 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
implements LazyStringList, RandomAccess {
private static final LazyStringArrayList EMPTY_LIST = new LazyStringArrayList();
+
static {
EMPTY_LIST.makeImmutable();
}
-
+
static LazyStringArrayList emptyList() {
return EMPTY_LIST;
}
@@ -83,8 +80,8 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
this(DEFAULT_CAPACITY);
}
- public LazyStringArrayList(int intialCapacity) {
- this(new ArrayList<Object>(intialCapacity));
+ public LazyStringArrayList(int initialCapacity) {
+ this(new ArrayList<Object>(initialCapacity));
}
public LazyStringArrayList(LazyStringList from) {
@@ -95,7 +92,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
public LazyStringArrayList(List<String> from) {
this(new ArrayList<Object>(from));
}
-
+
private LazyStringArrayList(ArrayList<Object> list) {
this.list = list;
}
@@ -150,13 +147,13 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
list.add(index, element);
modCount++;
}
-
+
private void add(int index, ByteString element) {
ensureIsMutable();
list.add(index, element);
modCount++;
}
-
+
private void add(int index, byte[] element) {
ensureIsMutable();
list.add(index, element);
@@ -177,8 +174,8 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
ensureIsMutable();
// When copying from another LazyStringList, directly copy the underlying
// elements rather than forcing each element to be decoded to a String.
- Collection<?> collection = c instanceof LazyStringList
- ? ((LazyStringList) c).getUnderlyingElements() : c;
+ Collection<?> collection =
+ c instanceof LazyStringList ? ((LazyStringList) c).getUnderlyingElements() : c;
boolean ret = list.addAll(index, collection);
modCount++;
return ret;
@@ -221,7 +218,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
list.add(element);
modCount++;
}
-
+
@Override
public void add(byte[] element) {
ensureIsMutable();
@@ -233,7 +230,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
public Object getRaw(int index) {
return list.get(index);
}
-
+
@Override
public ByteString getByteString(int index) {
Object o = list.get(index);
@@ -243,7 +240,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
}
return b;
}
-
+
@Override
public byte[] getByteArray(int index) {
Object o = list.get(index);
@@ -258,7 +255,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
public void set(int index, ByteString s) {
setAndReturn(index, s);
}
-
+
private Object setAndReturn(int index, ByteString s) {
ensureIsMutable();
return list.set(index, s);
@@ -268,7 +265,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
public void set(int index, byte[] s) {
setAndReturn(index, s);
}
-
+
private Object setAndReturn(int index, byte[] s) {
ensureIsMutable();
return list.set(index, s);
@@ -283,7 +280,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return Internal.toStringUtf8((byte[]) o);
}
}
-
+
private static ByteString asByteString(Object o) {
if (o instanceof ByteString) {
return (ByteString) o;
@@ -293,7 +290,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return ByteString.copyFrom((byte[]) o);
}
}
-
+
private static byte[] asByteArray(Object o) {
if (o instanceof byte[]) {
return (byte[]) o;
@@ -324,14 +321,13 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
}
}
- private static class ByteArrayListView extends AbstractList<byte[]>
- implements RandomAccess {
+ private static class ByteArrayListView extends AbstractList<byte[]> implements RandomAccess {
private final LazyStringArrayList list;
-
+
ByteArrayListView(LazyStringArrayList list) {
this.list = list;
}
-
+
@Override
public byte[] get(int index) {
return list.getByteArray(index);
@@ -362,14 +358,13 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return asByteArray(o);
}
}
-
+
@Override
public List<byte[]> asByteArrayList() {
return new ByteArrayListView(this);
}
- private static class ByteStringListView extends AbstractList<ByteString>
- implements RandomAccess {
+ private static class ByteStringListView extends AbstractList<ByteString> implements RandomAccess {
private final LazyStringArrayList list;
ByteStringListView(LazyStringArrayList list) {
@@ -419,5 +414,4 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
}
return this;
}
-
}