|
14 | 14 | import java.util.AbstractList;
|
15 | 15 | import java.util.AbstractMap;
|
16 | 16 | import java.util.AbstractSet;
|
| 17 | +import java.util.ArrayList; |
17 | 18 | import java.util.Arrays;
|
| 19 | +import java.util.Collection; |
18 | 20 | import java.util.Iterator;
|
19 | 21 | import java.util.List;
|
20 | 22 | import java.util.Map;
|
@@ -126,6 +128,67 @@ public static ByteBuffer copyByteBuffer(ByteBuffer source) {
|
126 | 128 | return result;
|
127 | 129 | }
|
128 | 130 |
|
| 131 | + /** |
| 132 | + * Adds the {@code values} to the {@code list}. This is a helper method used by generated code. |
| 133 | + * Users should ignore it. |
| 134 | + * |
| 135 | + * @throws NullPointerException if {@code values} or any of the elements of {@code values} is |
| 136 | + * null. |
| 137 | + */ |
| 138 | + public static <T> void addAll(final Iterable<T> values, final List<? super T> list) { |
| 139 | + checkNotNull(values); |
| 140 | + if (values instanceof LazyStringList) { |
| 141 | + // For StringOrByteStringLists, check the underlying elements to avoid |
| 142 | + // forcing conversions of ByteStrings to Strings. |
| 143 | + // TODO: Could we just prohibit nulls in all protobuf lists and get rid of this? Is |
| 144 | + // if even possible to hit this condition as all protobuf methods check for null first, |
| 145 | + // right? |
| 146 | + List<?> lazyValues = ((LazyStringList) values).getUnderlyingElements(); |
| 147 | + LazyStringList lazyList = (LazyStringList) list; |
| 148 | + int begin = list.size(); |
| 149 | + for (Object value : lazyValues) { |
| 150 | + if (value == null) { |
| 151 | + // encountered a null value so we must undo our modifications prior to throwing |
| 152 | + String message = "Element at index " + (lazyList.size() - begin) + " is null."; |
| 153 | + for (int i = lazyList.size() - 1; i >= begin; i--) { |
| 154 | + lazyList.remove(i); |
| 155 | + } |
| 156 | + throw new NullPointerException(message); |
| 157 | + } |
| 158 | + if (value instanceof ByteString) { |
| 159 | + lazyList.add((ByteString) value); |
| 160 | + } else { |
| 161 | + lazyList.add((String) value); |
| 162 | + } |
| 163 | + } |
| 164 | + } else { |
| 165 | + if (values instanceof PrimitiveNonBoxingCollection) { |
| 166 | + list.addAll((Collection<T>) values); |
| 167 | + } else { |
| 168 | + addAllCheckingNulls(values, list); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // We check nulls as we iterate to avoid iterating over values twice. |
| 174 | + private static <T> void addAllCheckingNulls(Iterable<T> values, List<? super T> list) { |
| 175 | + if (list instanceof ArrayList && values instanceof Collection) { |
| 176 | + ((ArrayList<T>) list).ensureCapacity(list.size() + ((Collection<T>) values).size()); |
| 177 | + } |
| 178 | + int begin = list.size(); |
| 179 | + for (T value : values) { |
| 180 | + if (value == null) { |
| 181 | + // encountered a null value so we must undo our modifications prior to throwing |
| 182 | + String message = "Element at index " + (list.size() - begin) + " is null."; |
| 183 | + for (int i = list.size() - 1; i >= begin; i--) { |
| 184 | + list.remove(i); |
| 185 | + } |
| 186 | + throw new NullPointerException(message); |
| 187 | + } |
| 188 | + list.add(value); |
| 189 | + } |
| 190 | + } |
| 191 | + |
129 | 192 | /**
|
130 | 193 | * Helper called by generated code to determine if a byte array is a valid UTF-8 encoded string
|
131 | 194 | * such that the original bytes can be converted to a String object and then back to a byte array
|
|
0 commit comments