• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 package com.google.protobuf;
32 
33 import java.util.Collection;
34 import java.util.List;
35 
36 /**
37  * An interface extending {@code List<String>} that also provides access to the
38  * items of the list as UTF8-encoded ByteString or byte[] objects. This is
39  * used by the protocol buffer implementation to support lazily converting bytes
40  * parsed over the wire to String objects until needed and also increases the
41  * efficiency of serialization if the String was never requested as the
42  * ByteString or byte[] is already cached. The ByteString methods are used in
43  * immutable API only and byte[] methods used in mutable API only for they use
44  * different representations for string/bytes fields.
45  *
46  * @author jonp@google.com (Jon Perlow)
47  */
48 public interface LazyStringList extends ProtocolStringList {
49 
50   /**
51    * Returns the element at the specified position in this list as a ByteString.
52    *
53    * @param index index of the element to return
54    * @return the element at the specified position in this list
55    * @throws IndexOutOfBoundsException if the index is out of range
56    *         ({@code index < 0 || index >= size()})
57    */
getByteString(int index)58   ByteString getByteString(int index);
59 
60   /**
61    * Returns the element at the specified position in this list as an Object
62    * that will either be a String or a ByteString.
63    *
64    * @param index index of the element to return
65    * @return the element at the specified position in this list
66    * @throws IndexOutOfBoundsException if the index is out of range
67    *         ({@code index < 0 || index >= size()})
68    */
getRaw(int index)69   Object getRaw(int index);
70 
71   /**
72    * Returns the element at the specified position in this list as byte[].
73    *
74    * @param index index of the element to return
75    * @return the element at the specified position in this list
76    * @throws IndexOutOfBoundsException if the index is out of range
77    *         ({@code index < 0 || index >= size()})
78    */
getByteArray(int index)79   byte[] getByteArray(int index);
80 
81   /**
82    * Appends the specified element to the end of this list (optional
83    * operation).
84    *
85    * @param element element to be appended to this list
86    * @throws UnsupportedOperationException if the <tt>add</tt> operation
87    *         is not supported by this list
88    */
add(ByteString element)89   void add(ByteString element);
90 
91   /**
92    * Appends the specified element to the end of this list (optional
93    * operation).
94    *
95    * @param element element to be appended to this list
96    * @throws UnsupportedOperationException if the <tt>add</tt> operation
97    *         is not supported by this list
98    */
add(byte[] element)99   void add(byte[] element);
100 
101   /**
102    * Replaces the element at the specified position in this list with the
103    * specified element (optional operation).
104    *
105    * @param index index of the element to replace
106    * @param element the element to be stored at the specified position
107    * @throws UnsupportedOperationException if the <tt>set</tt> operation
108    *         is not supported by this list
109    *         IndexOutOfBoundsException if the index is out of range
110    *         ({@code index < 0 || index >= size()})
111    */
set(int index, ByteString element)112   void set(int index, ByteString element);
113 
114   /**
115    * Replaces the element at the specified position in this list with the
116    * specified element (optional operation).
117    *
118    * @param index index of the element to replace
119    * @param element the element to be stored at the specified position
120    * @throws UnsupportedOperationException if the <tt>set</tt> operation
121    *         is not supported by this list
122    *         IndexOutOfBoundsException if the index is out of range
123    *         ({@code index < 0 || index >= size()})
124    */
set(int index, byte[] element)125   void set(int index, byte[] element);
126 
127   /**
128    * Appends all elements in the specified ByteString collection to the end of
129    * this list.
130    *
131    * @param c collection whose elements are to be added to this list
132    * @return true if this list changed as a result of the call
133    * @throws UnsupportedOperationException if the <tt>addAllByteString</tt>
134    *         operation is not supported by this list
135    */
addAllByteString(Collection<? extends ByteString> c)136   boolean addAllByteString(Collection<? extends ByteString> c);
137 
138   /**
139    * Appends all elements in the specified byte[] collection to the end of
140    * this list.
141    *
142    * @param c collection whose elements are to be added to this list
143    * @return true if this list changed as a result of the call
144    * @throws UnsupportedOperationException if the <tt>addAllByteArray</tt>
145    *         operation is not supported by this list
146    */
addAllByteArray(Collection<byte[]> c)147   boolean addAllByteArray(Collection<byte[]> c);
148 
149   /**
150    * Returns an unmodifiable List of the underlying elements, each of which is
151    * either a {@code String} or its equivalent UTF-8 encoded {@code ByteString}
152    * or byte[]. It is an error for the caller to modify the returned
153    * List, and attempting to do so will result in an
154    * {@link UnsupportedOperationException}.
155    */
getUnderlyingElements()156   List<?> getUnderlyingElements();
157 
158   /**
159    * Merges all elements from another LazyStringList into this one. This method
160    * differs from {@link #addAll(Collection)} on that underlying byte arrays are
161    * copied instead of reference shared. Immutable API doesn't need to use this
162    * method as byte[] is not used there at all.
163    */
mergeFrom(LazyStringList other)164   void mergeFrom(LazyStringList other);
165 
166   /**
167    * Returns a mutable view of this list. Changes to the view will be made into
168    * the original list. This method is used in mutable API only.
169    */
asByteArrayList()170   List<byte[]> asByteArrayList();
171 
172   /** Returns an unmodifiable view of the list. */
getUnmodifiableView()173   LazyStringList getUnmodifiableView();
174 }
175