Skip to content

Commit 68b1f37

Browse files
committed
Merge remote-tracking branch 'origin/make-lite-private' into make-lite-private
2 parents 83f5128 + 8607706 commit 68b1f37

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

protobuf-api/src/main/java/com/google/protobuf/Internal.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import java.util.AbstractList;
1515
import java.util.AbstractMap;
1616
import java.util.AbstractSet;
17+
import java.util.ArrayList;
1718
import java.util.Arrays;
19+
import java.util.Collection;
1820
import java.util.Iterator;
1921
import java.util.List;
2022
import java.util.Map;
@@ -126,6 +128,67 @@ public static ByteBuffer copyByteBuffer(ByteBuffer source) {
126128
return result;
127129
}
128130

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+
129192
/**
130193
* Helper called by generated code to determine if a byte array is a valid UTF-8 encoded string
131194
* such that the original bytes can be converted to a String object and then back to a byte array

protobuf-sdk/src/main/java/com/google/protobuf/compiler/PluginProtos.java

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)