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 byte[]. 62 * 63 * @param index index of the element to return 64 * @return the element at the specified position in this list 65 * @throws IndexOutOfBoundsException if the index is out of range 66 * ({@code index < 0 || index >= size()}) 67 */ getByteArray(int index)68 byte[] getByteArray(int index); 69 70 /** 71 * Appends the specified element to the end of this list (optional 72 * operation). 73 * 74 * @param element element to be appended to this list 75 * @throws UnsupportedOperationException if the <tt>add</tt> operation 76 * is not supported by this list 77 */ add(ByteString element)78 void add(ByteString element); 79 80 /** 81 * Appends the specified element to the end of this list (optional 82 * operation). 83 * 84 * @param element element to be appended to this list 85 * @throws UnsupportedOperationException if the <tt>add</tt> operation 86 * is not supported by this list 87 */ add(byte[] element)88 void add(byte[] element); 89 90 /** 91 * Replaces the element at the specified position in this list with the 92 * specified element (optional operation). 93 * 94 * @param index index of the element to replace 95 * @param element the element to be stored at the specified position 96 * @throws UnsupportedOperationException if the <tt>set</tt> operation 97 * is not supported by this list 98 * IndexOutOfBoundsException if the index is out of range 99 * ({@code index < 0 || index >= size()}) 100 */ set(int index, ByteString element)101 void set(int index, ByteString element); 102 103 /** 104 * Replaces the element at the specified position in this list with the 105 * specified element (optional operation). 106 * 107 * @param index index of the element to replace 108 * @param element the element to be stored at the specified position 109 * @throws UnsupportedOperationException if the <tt>set</tt> operation 110 * is not supported by this list 111 * IndexOutOfBoundsException if the index is out of range 112 * ({@code index < 0 || index >= size()}) 113 */ set(int index, byte[] element)114 void set(int index, byte[] element); 115 116 /** 117 * Appends all elements in the specified ByteString collection to the end of 118 * this list. 119 * 120 * @param c collection whose elements are to be added to this list 121 * @return true if this list changed as a result of the call 122 * @throws UnsupportedOperationException if the <tt>addAllByteString</tt> 123 * operation is not supported by this list 124 */ addAllByteString(Collection<? extends ByteString> c)125 boolean addAllByteString(Collection<? extends ByteString> c); 126 127 /** 128 * Appends all elements in the specified byte[] 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>addAllByteArray</tt> 134 * operation is not supported by this list 135 */ addAllByteArray(Collection<byte[]> c)136 boolean addAllByteArray(Collection<byte[]> c); 137 138 /** 139 * Returns an unmodifiable List of the underlying elements, each of which is 140 * either a {@code String} or its equivalent UTF-8 encoded {@code ByteString} 141 * or byte[]. It is an error for the caller to modify the returned 142 * List, and attempting to do so will result in an 143 * {@link UnsupportedOperationException}. 144 */ getUnderlyingElements()145 List<?> getUnderlyingElements(); 146 147 /** 148 * Merges all elements from another LazyStringList into this one. This method 149 * differs from {@link #addAll(Collection)} on that underlying byte arrays are 150 * copied instead of reference shared. Immutable API doesn't need to use this 151 * method as byte[] is not used there at all. 152 */ mergeFrom(LazyStringList other)153 void mergeFrom(LazyStringList other); 154 155 /** 156 * Returns a mutable view of this list. Changes to the view will be made into 157 * the original list. This method is used in mutable API only. 158 */ asByteArrayList()159 List<byte[]> asByteArrayList(); 160 161 /** Returns an unmodifiable view of the list. */ getUnmodifiableView()162 LazyStringList getUnmodifiableView(); 163 } 164