• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 package com.google.protobuf;
9 
10 import java.util.Collection;
11 import java.util.List;
12 
13 /**
14  * An interface extending {@code List<String>} that also provides access to the items of the list as
15  * UTF8-encoded ByteString or byte[] objects. This is used by the protocol buffer implementation to
16  * support lazily converting bytes parsed over the wire to String objects until needed and also
17  * increases the efficiency of serialization if the String was never requested as the ByteString or
18  * byte[] is already cached. The ByteString methods are used in immutable API only and byte[]
19  * methods used in mutable API only for they use different representations for string/bytes fields.
20  *
21  * @author jonp@google.com (Jon Perlow)
22  */
23 public interface LazyStringList extends ProtocolStringList {
24 
25   /**
26    * Returns the element at the specified position in this list as a ByteString.
27    *
28    * @param index index of the element to return
29    * @return the element at the specified position in this list
30    * @throws IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >=
31    *     size()})
32    */
getByteString(int index)33   ByteString getByteString(int index);
34 
35   /**
36    * Returns the element at the specified position in this list as an Object that will either be a
37    * String or a ByteString.
38    *
39    * @param index index of the element to return
40    * @return the element at the specified position in this list
41    * @throws IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >=
42    *     size()})
43    */
getRaw(int index)44   Object getRaw(int index);
45 
46   /**
47    * Returns the element at the specified position in this list as byte[].
48    *
49    * @param index index of the element to return
50    * @return the element at the specified position in this list
51    * @throws IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >=
52    *     size()})
53    */
getByteArray(int index)54   byte[] getByteArray(int index);
55 
56   /**
57    * Appends the specified element to the end of this list (optional operation).
58    *
59    * @param element element to be appended to this list
60    * @throws UnsupportedOperationException if the <tt>add</tt> operation is not supported by this
61    *     list
62    */
add(ByteString element)63   void add(ByteString element);
64 
65   /**
66    * Appends the specified element to the end of this list (optional operation).
67    *
68    * @param element element to be appended to this list
69    * @throws UnsupportedOperationException if the <tt>add</tt> operation is not supported by this
70    *     list
71    */
add(byte[] element)72   void add(byte[] element);
73 
74   /**
75    * Replaces the element at the specified position in this list with the specified element
76    * (optional operation).
77    *
78    * @param index index of the element to replace
79    * @param element the element to be stored at the specified position
80    * @throws UnsupportedOperationException if the <tt>set</tt> operation is not supported by this
81    *     list IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >=
82    *     size()})
83    */
set(int index, ByteString element)84   void set(int index, ByteString element);
85 
86   /**
87    * Replaces the element at the specified position in this list with the specified element
88    * (optional operation).
89    *
90    * @param index index of the element to replace
91    * @param element the element to be stored at the specified position
92    * @throws UnsupportedOperationException if the <tt>set</tt> operation is not supported by this
93    *     list IndexOutOfBoundsException if the index is out of range ({@code index < 0 || index >=
94    *     size()})
95    */
set(int index, byte[] element)96   void set(int index, byte[] element);
97 
98   /**
99    * Appends all elements in the specified ByteString collection to the end of this list.
100    *
101    * @param c collection whose elements are to be added to this list
102    * @return true if this list changed as a result of the call
103    * @throws UnsupportedOperationException if the <tt>addAllByteString</tt> operation is not
104    *     supported by this list
105    */
addAllByteString(Collection<? extends ByteString> c)106   boolean addAllByteString(Collection<? extends ByteString> c);
107 
108   /**
109    * Appends all elements in the specified byte[] collection to the end of this list.
110    *
111    * @param c collection whose elements are to be added to this list
112    * @return true if this list changed as a result of the call
113    * @throws UnsupportedOperationException if the <tt>addAllByteArray</tt> operation is not
114    *     supported by this list
115    */
addAllByteArray(Collection<byte[]> c)116   boolean addAllByteArray(Collection<byte[]> c);
117 
118   /**
119    * Returns an unmodifiable List of the underlying elements, each of which is either a {@code
120    * String} or its equivalent UTF-8 encoded {@code ByteString} or byte[]. It is an error for the
121    * caller to modify the returned List, and attempting to do so will result in an {@link
122    * UnsupportedOperationException}.
123    */
getUnderlyingElements()124   List<?> getUnderlyingElements();
125 
126   /**
127    * Merges all elements from another LazyStringList into this one. This method differs from {@link
128    * #addAll(Collection)} on that underlying byte arrays are copied instead of reference shared.
129    * Immutable API doesn't need to use this method as byte[] is not used there at all.
130    */
mergeFrom(LazyStringList other)131   void mergeFrom(LazyStringList other);
132 
133   /**
134    * Returns a mutable view of this list. Changes to the view will be made into the original list.
135    * This method is used in mutable API only.
136    */
asByteArrayList()137   List<byte[]> asByteArrayList();
138 
139   /** Returns an unmodifiable view of the list. */
getUnmodifiableView()140   LazyStringList getUnmodifiableView();
141 }
142