• 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.lang.reflect.Method;
11 import java.nio.Buffer;
12 import java.nio.ByteBuffer;
13 import java.nio.charset.Charset;
14 import java.util.AbstractList;
15 import java.util.AbstractMap;
16 import java.util.AbstractSet;
17 import java.util.Arrays;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.RandomAccess;
22 import java.util.Set;
23 
24 /**
25  * The classes contained within are used internally by the Protocol Buffer library and generated
26  * message implementations. They are public only because those generated messages do not reside in
27  * the {@code protobuf} package. Others should not use this class directly.
28  *
29  * @author kenton@google.com (Kenton Varda)
30  */
31 public final class Internal {
32 
Internal()33   private Internal() {}
34 
35   static final Charset US_ASCII = Charset.forName("US-ASCII");
36   static final Charset UTF_8 = Charset.forName("UTF-8");
37   static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
38 
39   /** Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. */
checkNotNull(T obj)40   static <T> T checkNotNull(T obj) {
41     if (obj == null) {
42       throw new NullPointerException();
43     }
44     return obj;
45   }
46 
47   /** Throws an appropriate {@link NullPointerException} if the given objects is {@code null}. */
checkNotNull(T obj, String message)48   static <T> T checkNotNull(T obj, String message) {
49     if (obj == null) {
50       throw new NullPointerException(message);
51     }
52     return obj;
53   }
54 
55   /**
56    * Helper called by generated code to construct default values for string fields.
57    *
58    * <p>The protocol compiler does not actually contain a UTF-8 decoder -- it just pushes
59    * UTF-8-encoded text around without touching it. The one place where this presents a problem is
60    * when generating Java string literals. Unicode characters in the string literal would normally
61    * need to be encoded using a Unicode escape sequence, which would require decoding them. To get
62    * around this, protoc instead embeds the UTF-8 bytes into the generated code and leaves it to the
63    * runtime library to decode them.
64    *
65    * <p>It gets worse, though. If protoc just generated a byte array, like: new byte[] {0x12, 0x34,
66    * 0x56, 0x78} Java actually generates *code* which allocates an array and then fills in each
67    * value. This is much less efficient than just embedding the bytes directly into the bytecode. To
68    * get around this, we need another work-around. String literals are embedded directly, so protoc
69    * actually generates a string literal corresponding to the bytes. The easiest way to do this is
70    * to use the ISO-8859-1 character set, which corresponds to the first 256 characters of the
71    * Unicode range. Protoc can then use good old CEscape to generate the string.
72    *
73    * <p>So we have a string literal which represents a set of bytes which represents another string.
74    * This function -- stringDefaultValue -- converts from the generated string to the string we
75    * actually want. The generated code calls this automatically.
76    */
stringDefaultValue(String bytes)77   public static String stringDefaultValue(String bytes) {
78     return new String(bytes.getBytes(ISO_8859_1), UTF_8);
79   }
80 
81   /**
82    * Helper called by generated code to construct default values for bytes fields.
83    *
84    * <p>This is a lot like {@link #stringDefaultValue}, but for bytes fields. In this case we only
85    * need the second of the two hacks -- allowing us to embed raw bytes as a string literal with
86    * ISO-8859-1 encoding.
87    */
bytesDefaultValue(String bytes)88   public static ByteString bytesDefaultValue(String bytes) {
89     return ByteString.copyFrom(bytes.getBytes(ISO_8859_1));
90   }
91   /**
92    * Helper called by generated code to construct default values for bytes fields.
93    *
94    * <p>This is like {@link #bytesDefaultValue}, but returns a byte array.
95    */
byteArrayDefaultValue(String bytes)96   public static byte[] byteArrayDefaultValue(String bytes) {
97     return bytes.getBytes(ISO_8859_1);
98   }
99 
100   /**
101    * Helper called by generated code to construct default values for bytes fields.
102    *
103    * <p>This is like {@link #bytesDefaultValue}, but returns a ByteBuffer.
104    */
byteBufferDefaultValue(String bytes)105   public static ByteBuffer byteBufferDefaultValue(String bytes) {
106     return ByteBuffer.wrap(byteArrayDefaultValue(bytes));
107   }
108 
109   /**
110    * Create a new ByteBuffer and copy all the content of {@code source} ByteBuffer to the new
111    * ByteBuffer. The new ByteBuffer's limit and capacity will be source.capacity(), and its position
112    * will be 0. Note that the state of {@code source} ByteBuffer won't be changed.
113    */
copyByteBuffer(ByteBuffer source)114   public static ByteBuffer copyByteBuffer(ByteBuffer source) {
115     // Make a duplicate of the source ByteBuffer and read data from the
116     // duplicate. This is to avoid affecting the source ByteBuffer's state.
117     ByteBuffer temp = source.duplicate();
118     // We want to copy all the data in the source ByteBuffer, not just the
119     // remaining bytes.
120     // View ByteBuffer as Buffer to avoid issue with covariant return types
121     // See https://issues.apache.org/jira/browse/MRESOLVER-85
122     ((Buffer) temp).clear();
123     ByteBuffer result = ByteBuffer.allocate(temp.capacity());
124     result.put(temp);
125     ((Buffer) result).clear();
126     return result;
127   }
128 
129   /**
130    * Helper called by generated code to determine if a byte array is a valid UTF-8 encoded string
131    * such that the original bytes can be converted to a String object and then back to a byte array
132    * round tripping the bytes without loss. More precisely, returns {@code true} whenever:
133    *
134    * <pre>{@code
135    * Arrays.equals(byteString.toByteArray(),
136    *     new String(byteString.toByteArray(), "UTF-8").getBytes("UTF-8"))
137    * }</pre>
138    *
139    * <p>This method rejects "overlong" byte sequences, as well as 3-byte sequences that would map to
140    * a surrogate character, in accordance with the restricted definition of UTF-8 introduced in
141    * Unicode 3.1. Note that the UTF-8 decoder included in Oracle's JDK has been modified to also
142    * reject "overlong" byte sequences, but currently (2011) still accepts 3-byte surrogate character
143    * byte sequences.
144    *
145    * <p>See the Unicode Standard,<br>
146    * Table 3-6. <em>UTF-8 Bit Distribution</em>,<br>
147    * Table 3-7. <em>Well Formed UTF-8 Byte Sequences</em>.
148    *
149    * <p>As of 2011-02, this method simply returns the result of {@link ByteString#isValidUtf8()}.
150    * Calling that method directly is preferred.
151    *
152    * @param byteString the string to check
153    * @return whether the byte array is round trippable
154    */
isValidUtf8(ByteString byteString)155   public static boolean isValidUtf8(ByteString byteString) {
156     return byteString.isValidUtf8();
157   }
158 
159   /** Like {@link #isValidUtf8(ByteString)} but for byte arrays. */
isValidUtf8(byte[] byteArray)160   public static boolean isValidUtf8(byte[] byteArray) {
161     return Utf8.isValidUtf8(byteArray);
162   }
163 
164   /** Helper method to get the UTF-8 bytes of a string. */
toByteArray(String value)165   public static byte[] toByteArray(String value) {
166     return value.getBytes(UTF_8);
167   }
168 
169   /** Helper method to convert a byte array to a string using UTF-8 encoding. */
toStringUtf8(byte[] bytes)170   public static String toStringUtf8(byte[] bytes) {
171     return new String(bytes, UTF_8);
172   }
173 
174   /**
175    * Interface for an enum value or value descriptor, to be used in FieldSet. The lite library
176    * stores enum values directly in FieldSets but the full library stores EnumValueDescriptors in
177    * order to better support reflection.
178    */
179   public interface EnumLite {
getNumber()180     int getNumber();
181   }
182 
183   /**
184    * Interface for an object which maps integers to {@link EnumLite}s. {@link
185    * Descriptors.EnumDescriptor} implements this interface by mapping numbers to {@link
186    * Descriptors.EnumValueDescriptor}s. Additionally, every generated enum type has a static method
187    * internalGetValueMap() which returns an implementation of this type that maps numbers to enum
188    * values.
189    */
190   public interface EnumLiteMap<T extends EnumLite> {
findValueByNumber(int number)191     T findValueByNumber(int number);
192   }
193 
194   /** Interface for an object which verifies integers are in range. */
195   public interface EnumVerifier {
isInRange(int number)196     boolean isInRange(int number);
197   }
198 
199   /**
200    * Helper method for implementing {@link Message#hashCode()} for longs.
201    *
202    * @see Long#hashCode()
203    */
hashLong(long n)204   public static int hashLong(long n) {
205     return (int) (n ^ (n >>> 32));
206   }
207 
208   /**
209    * Helper method for implementing {@link Message#hashCode()} for booleans.
210    *
211    * @see Boolean#hashCode()
212    */
hashBoolean(boolean b)213   public static int hashBoolean(boolean b) {
214     return b ? 1231 : 1237;
215   }
216 
217   /**
218    * Helper method for implementing {@link Message#hashCode()} for enums.
219    *
220    * <p>This is needed because {@link java.lang.Enum#hashCode()} is final, but we need to use the
221    * field number as the hash code to ensure compatibility between statically and dynamically
222    * generated enum objects.
223    */
hashEnum(EnumLite e)224   public static int hashEnum(EnumLite e) {
225     return e.getNumber();
226   }
227 
228   /** Helper method for implementing {@link Message#hashCode()} for enum lists. */
hashEnumList(List<? extends EnumLite> list)229   public static int hashEnumList(List<? extends EnumLite> list) {
230     int hash = 1;
231     for (EnumLite e : list) {
232       hash = 31 * hash + hashEnum(e);
233     }
234     return hash;
235   }
236 
237   /** Helper method for implementing {@link Message#equals(Object)} for bytes field. */
equals(List<byte[]> a, List<byte[]> b)238   public static boolean equals(List<byte[]> a, List<byte[]> b) {
239     if (a.size() != b.size()) {
240       return false;
241     }
242     for (int i = 0; i < a.size(); ++i) {
243       if (!Arrays.equals(a.get(i), b.get(i))) {
244         return false;
245       }
246     }
247     return true;
248   }
249 
250   /** Helper method for implementing {@link Message#hashCode()} for bytes field. */
hashCode(List<byte[]> list)251   public static int hashCode(List<byte[]> list) {
252     int hash = 1;
253     for (byte[] bytes : list) {
254       hash = 31 * hash + hashCode(bytes);
255     }
256     return hash;
257   }
258 
259   /** Helper method for implementing {@link Message#hashCode()} for bytes field. */
hashCode(byte[] bytes)260   public static int hashCode(byte[] bytes) {
261     // The hash code for a byte array should be the same as the hash code for a
262     // ByteString with the same content. This is to ensure that the generated
263     // hashCode() method will return the same value as the pure reflection
264     // based hashCode() method.
265     return Internal.hashCode(bytes, 0, bytes.length);
266   }
267 
268   /** Helper method for implementing {@link LiteralByteString#hashCode()}. */
hashCode(byte[] bytes, int offset, int length)269   static int hashCode(byte[] bytes, int offset, int length) {
270     // The hash code for a byte array should be the same as the hash code for a
271     // ByteString with the same content. This is to ensure that the generated
272     // hashCode() method will return the same value as the pure reflection
273     // based hashCode() method.
274     int h = Internal.partialHash(length, bytes, offset, length);
275     return h == 0 ? 1 : h;
276   }
277 
278   /** Helper method for continuously hashing bytes. */
partialHash(int h, byte[] bytes, int offset, int length)279   static int partialHash(int h, byte[] bytes, int offset, int length) {
280     for (int i = offset; i < offset + length; i++) {
281       h = h * 31 + bytes[i];
282     }
283     return h;
284   }
285 
286   /** Helper method for implementing {@link Message#equals(Object)} for bytes field. */
equalsByteBuffer(ByteBuffer a, ByteBuffer b)287   public static boolean equalsByteBuffer(ByteBuffer a, ByteBuffer b) {
288     if (a.capacity() != b.capacity()) {
289       return false;
290     }
291     // ByteBuffer.equals() will only compare the remaining bytes, but we want to
292     // compare all the content.
293     ByteBuffer aDuplicate = a.duplicate();
294     Java8Compatibility.clear(aDuplicate);
295     ByteBuffer bDuplicate = b.duplicate();
296     Java8Compatibility.clear(bDuplicate);
297     return aDuplicate.equals(bDuplicate);
298   }
299 
300   /** Helper method for implementing {@link Message#equals(Object)} for bytes field. */
equalsByteBuffer(List<ByteBuffer> a, List<ByteBuffer> b)301   public static boolean equalsByteBuffer(List<ByteBuffer> a, List<ByteBuffer> b) {
302     if (a.size() != b.size()) {
303       return false;
304     }
305     for (int i = 0; i < a.size(); ++i) {
306       if (!equalsByteBuffer(a.get(i), b.get(i))) {
307         return false;
308       }
309     }
310     return true;
311   }
312 
313   /** Helper method for implementing {@link Message#hashCode()} for bytes field. */
hashCodeByteBuffer(List<ByteBuffer> list)314   public static int hashCodeByteBuffer(List<ByteBuffer> list) {
315     int hash = 1;
316     for (ByteBuffer bytes : list) {
317       hash = 31 * hash + hashCodeByteBuffer(bytes);
318     }
319     return hash;
320   }
321 
322   private static final int DEFAULT_BUFFER_SIZE = 4096;
323 
324   /** Helper method for implementing {@link Message#hashCode()} for bytes field. */
hashCodeByteBuffer(ByteBuffer bytes)325   public static int hashCodeByteBuffer(ByteBuffer bytes) {
326     if (bytes.hasArray()) {
327       // Fast path.
328       int h = partialHash(bytes.capacity(), bytes.array(), bytes.arrayOffset(), bytes.capacity());
329       return h == 0 ? 1 : h;
330     } else {
331       // Read the data into a temporary byte array before calculating the
332       // hash value.
333       final int bufferSize =
334           bytes.capacity() > DEFAULT_BUFFER_SIZE ? DEFAULT_BUFFER_SIZE : bytes.capacity();
335       final byte[] buffer = new byte[bufferSize];
336       final ByteBuffer duplicated = bytes.duplicate();
337       Java8Compatibility.clear(duplicated);
338       int h = bytes.capacity();
339       while (duplicated.remaining() > 0) {
340         final int length =
341             duplicated.remaining() <= bufferSize ? duplicated.remaining() : bufferSize;
342         duplicated.get(buffer, 0, length);
343         h = partialHash(h, buffer, 0, length);
344       }
345       return h == 0 ? 1 : h;
346     }
347   }
348 
349   @SuppressWarnings("unchecked")
getDefaultInstance(Class<T> clazz)350   public static <T extends MessageLite> T getDefaultInstance(Class<T> clazz) {
351     try {
352       Method method = clazz.getMethod("getDefaultInstance");
353       return (T) method.invoke(method);
354     } catch (Exception e) {
355       throw new RuntimeException("Failed to get default instance for " + clazz, e);
356     }
357   }
358 
359   /** An empty byte array constant used in generated code. */
360   public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
361 
362   /** An empty byte array constant used in generated code. */
363   public static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(EMPTY_BYTE_ARRAY);
364 
365   /** An empty coded input stream constant used in generated code. */
366   public static final CodedInputStream EMPTY_CODED_INPUT_STREAM =
367       CodedInputStream.newInstance(EMPTY_BYTE_ARRAY);
368 
369   /** Helper method to merge two MessageLite instances. */
mergeMessage(Object destination, Object source)370   static Object mergeMessage(Object destination, Object source) {
371     return ((MessageLite) destination).toBuilder().mergeFrom((MessageLite) source).buildPartial();
372   }
373 
374   /**
375    * Provides an immutable view of {@code List<T>} around an {@code IntList}.
376    *
377    * <p>Protobuf internal. Used in protobuf generated code only.
378    */
379   public static class IntListAdapter<T> extends AbstractList<T> {
380     /** Convert individual elements of the List from int to T. */
381     public interface IntConverter<T> {
convert(int from)382       T convert(int from);
383     }
384 
385     private final IntList fromList;
386     private final IntConverter<T> converter;
387 
IntListAdapter(IntList fromList, IntConverter<T> converter)388     public IntListAdapter(IntList fromList, IntConverter<T> converter) {
389       this.fromList = fromList;
390       this.converter = converter;
391     }
392 
393     @Override
get(int index)394     public T get(int index) {
395       return converter.convert(fromList.getInt(index));
396     }
397 
398     @Override
size()399     public int size() {
400       return fromList.size();
401     }
402   }
403 
404   /**
405    * Provides an immutable view of {@code List<T>} around a {@code List<F>}.
406    *
407    * <p>Protobuf internal. Used in protobuf generated code only.
408    */
409   public static class ListAdapter<F, T> extends AbstractList<T> {
410     /** Convert individual elements of the List from F to T. */
411     public interface Converter<F, T> {
convert(F from)412       T convert(F from);
413     }
414 
415     private final List<F> fromList;
416     private final Converter<F, T> converter;
417 
ListAdapter(List<F> fromList, Converter<F, T> converter)418     public ListAdapter(List<F> fromList, Converter<F, T> converter) {
419       this.fromList = fromList;
420       this.converter = converter;
421     }
422 
423     @Override
get(int index)424     public T get(int index) {
425       return converter.convert(fromList.get(index));
426     }
427 
428     @Override
size()429     public int size() {
430       return fromList.size();
431     }
432   }
433 
434   /** Wrap around a {@code Map<K, RealValue>} and provide a {@code Map<K, V>} interface. */
435   public static class MapAdapter<K, V, RealValue> extends AbstractMap<K, V> {
436     /** An interface used to convert between two types. */
437     public interface Converter<A, B> {
doForward(A object)438       B doForward(A object);
439 
doBackward(B object)440       A doBackward(B object);
441     }
442 
newEnumConverter( final EnumLiteMap<T> enumMap, final T unrecognizedValue)443     public static <T extends EnumLite> Converter<Integer, T> newEnumConverter(
444         final EnumLiteMap<T> enumMap, final T unrecognizedValue) {
445       return new Converter<Integer, T>() {
446         @Override
447         public T doForward(Integer value) {
448           T result = enumMap.findValueByNumber(value);
449           return result == null ? unrecognizedValue : result;
450         }
451 
452         @Override
453         public Integer doBackward(T value) {
454           return value.getNumber();
455         }
456       };
457     }
458 
459     private final Map<K, RealValue> realMap;
460     private final Converter<RealValue, V> valueConverter;
461 
MapAdapter(Map<K, RealValue> realMap, Converter<RealValue, V> valueConverter)462     public MapAdapter(Map<K, RealValue> realMap, Converter<RealValue, V> valueConverter) {
463       this.realMap = realMap;
464       this.valueConverter = valueConverter;
465     }
466 
467     @Override
get(Object key)468     public V get(Object key) {
469       RealValue result = realMap.get(key);
470       if (result == null) {
471         return null;
472       }
473       return valueConverter.doForward(result);
474     }
475 
476     @Override
put(K key, V value)477     public V put(K key, V value) {
478       RealValue oldValue = realMap.put(key, valueConverter.doBackward(value));
479       if (oldValue == null) {
480         return null;
481       }
482       return valueConverter.doForward(oldValue);
483     }
484 
485     @Override
entrySet()486     public Set<java.util.Map.Entry<K, V>> entrySet() {
487       return new SetAdapter(realMap.entrySet());
488     }
489 
490     private class SetAdapter extends AbstractSet<Map.Entry<K, V>> {
491       private final Set<Map.Entry<K, RealValue>> realSet;
492 
SetAdapter(Set<Map.Entry<K, RealValue>> realSet)493       public SetAdapter(Set<Map.Entry<K, RealValue>> realSet) {
494         this.realSet = realSet;
495       }
496 
497       @Override
iterator()498       public Iterator<java.util.Map.Entry<K, V>> iterator() {
499         return new IteratorAdapter(realSet.iterator());
500       }
501 
502       @Override
size()503       public int size() {
504         return realSet.size();
505       }
506     }
507 
508     private class IteratorAdapter implements Iterator<Map.Entry<K, V>> {
509       private final Iterator<Map.Entry<K, RealValue>> realIterator;
510 
IteratorAdapter(Iterator<Map.Entry<K, RealValue>> realIterator)511       public IteratorAdapter(Iterator<Map.Entry<K, RealValue>> realIterator) {
512         this.realIterator = realIterator;
513       }
514 
515       @Override
hasNext()516       public boolean hasNext() {
517         return realIterator.hasNext();
518       }
519 
520       @Override
next()521       public java.util.Map.Entry<K, V> next() {
522         return new EntryAdapter(realIterator.next());
523       }
524 
525       @Override
remove()526       public void remove() {
527         realIterator.remove();
528       }
529     }
530 
531     private class EntryAdapter implements Map.Entry<K, V> {
532       private final Map.Entry<K, RealValue> realEntry;
533 
EntryAdapter(Map.Entry<K, RealValue> realEntry)534       public EntryAdapter(Map.Entry<K, RealValue> realEntry) {
535         this.realEntry = realEntry;
536       }
537 
538       @Override
getKey()539       public K getKey() {
540         return realEntry.getKey();
541       }
542 
543       @Override
getValue()544       public V getValue() {
545         return valueConverter.doForward(realEntry.getValue());
546       }
547 
548       @Override
setValue(V value)549       public V setValue(V value) {
550         RealValue oldValue = realEntry.setValue(valueConverter.doBackward(value));
551         if (oldValue == null) {
552           return null;
553         }
554         return valueConverter.doForward(oldValue);
555       }
556 
557       @Override
equals(Object o)558       public boolean equals(Object o) {
559         if (o == this) {
560           return true;
561         }
562         if (!(o instanceof Map.Entry)) {
563           return false;
564         }
565         Map.Entry<?, ?> other = (Map.Entry<?, ?>) o;
566         return getKey().equals(other.getKey()) && getValue().equals(getValue());
567       }
568 
569       @Override
hashCode()570       public int hashCode() {
571         return realEntry.hashCode();
572       }
573     }
574   }
575 
576   /**
577    * Extends {@link List} to add the capability to make the list immutable and inspect if it is
578    * modifiable.
579    *
580    * <p>All implementations must support efficient random access.
581    */
582   public static interface ProtobufList<E> extends List<E>, RandomAccess {
583 
584     /**
585      * Makes this list immutable. All subsequent modifications will throw an {@link
586      * UnsupportedOperationException}.
587      */
588     void makeImmutable();
589 
590     /**
591      * Returns whether this list can be modified via the publicly accessible {@link List} methods.
592      */
593     boolean isModifiable();
594 
595     /** Returns a mutable clone of this list with the specified capacity. */
596     ProtobufList<E> mutableCopyWithCapacity(int capacity);
597   }
598 
599   /**
600    * A {@link java.util.List} implementation that avoids boxing the elements into Integers if
601    * possible. Does not support null elements.
602    */
603   public static interface IntList extends ProtobufList<Integer> {
604 
605     /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */
606     int getInt(int index);
607 
608     /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */
609     void addInt(int element);
610 
611     /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */
612     @CanIgnoreReturnValue
613     int setInt(int index, int element);
614 
615     /** Returns a mutable clone of this list with the specified capacity. */
616     @Override
617     IntList mutableCopyWithCapacity(int capacity);
618   }
619 
620   /**
621    * A {@link java.util.List} implementation that avoids boxing the elements into Booleans if
622    * possible. Does not support null elements.
623    */
624   public static interface BooleanList extends ProtobufList<Boolean> {
625 
626     /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */
627     boolean getBoolean(int index);
628 
629     /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */
630     void addBoolean(boolean element);
631 
632     /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */
633     @CanIgnoreReturnValue
634     boolean setBoolean(int index, boolean element);
635 
636     /** Returns a mutable clone of this list with the specified capacity. */
637     @Override
638     BooleanList mutableCopyWithCapacity(int capacity);
639   }
640 
641   /**
642    * A {@link java.util.List} implementation that avoids boxing the elements into Longs if possible.
643    * Does not support null elements.
644    */
645   public static interface LongList extends ProtobufList<Long> {
646 
647     /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */
648     long getLong(int index);
649 
650     /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */
651     void addLong(long element);
652 
653     /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */
654     @CanIgnoreReturnValue
655     long setLong(int index, long element);
656 
657     /** Returns a mutable clone of this list with the specified capacity. */
658     @Override
659     LongList mutableCopyWithCapacity(int capacity);
660   }
661 
662   /**
663    * A {@link java.util.List} implementation that avoids boxing the elements into Doubles if
664    * possible. Does not support null elements.
665    */
666   public static interface DoubleList extends ProtobufList<Double> {
667 
668     /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */
669     double getDouble(int index);
670 
671     /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */
672     void addDouble(double element);
673 
674     /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */
675     @CanIgnoreReturnValue
676     double setDouble(int index, double element);
677 
678     /** Returns a mutable clone of this list with the specified capacity. */
679     @Override
680     DoubleList mutableCopyWithCapacity(int capacity);
681   }
682 
683   /**
684    * A {@link java.util.List} implementation that avoids boxing the elements into Floats if
685    * possible. Does not support null elements.
686    */
687   public static interface FloatList extends ProtobufList<Float> {
688 
689     /** Like {@link #get(int)} but more efficient in that it doesn't box the returned value. */
690     float getFloat(int index);
691 
692     /** Like {@link #add(Object)} but more efficient in that it doesn't box the element. */
693     void addFloat(float element);
694 
695     /** Like {@link #set(int, Object)} but more efficient in that it doesn't box the element. */
696     @CanIgnoreReturnValue
697     float setFloat(int index, float element);
698 
699     /** Returns a mutable clone of this list with the specified capacity. */
700     @Override
701     FloatList mutableCopyWithCapacity(int capacity);
702   }
703 }
704