Skip to content

Commit e86a7e8

Browse files
mderkamziccard
authored andcommitted
Disallowed null error in BatchResult. Adjusted doc. (googleapis#921)
1 parent c95e1ec commit e86a7e8

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
package com.google.cloud;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
1920
import static com.google.common.base.Preconditions.checkState;
2021

2122
import java.util.LinkedList;
2223
import java.util.List;
2324

2425
/**
25-
* This class holds a single result of a batch call. {@code T} is the type of the result and {@code
26-
* E} is the type of the service-dependent exception thrown when a processing error occurs.
26+
* This class holds a single result of a batch call. The class is not thread-safe.
27+
*
28+
* @param <T> the type of the result
29+
* @param <E> the type of the service-dependent exception thrown when a processing error occurs
30+
*
2731
*/
2832
public abstract class BatchResult<T, E extends BaseServiceException> {
2933

@@ -44,7 +48,7 @@ public boolean completed() {
4448
* Returns the result of this call.
4549
*
4650
* @throws IllegalStateException if the batch has not been completed yet
47-
* @throws E if an error occurred when processing this request
51+
* @throws E if an error occurred when processing the batch request
4852
*/
4953
public T get() throws E {
5054
checkState(completed(), "Batch has not been completed yet");
@@ -60,18 +64,16 @@ public T get() throws E {
6064
* @throws IllegalStateException if the batch has been completed already
6165
*/
6266
public void notify(Callback<T, E> callback) {
63-
if (completed) {
64-
throw new IllegalStateException("The batch has been completed. All the calls to the notify()"
67+
checkState(!completed, "The batch has been completed. All the calls to the notify()"
6568
+ " method should be done prior to submitting the batch.");
66-
}
6769
toBeNotified.add(callback);
6870
}
6971

7072
/**
7173
* Sets an error and status as completed. Notifies all callbacks.
7274
*/
7375
protected void error(E error) {
74-
this.error = error;
76+
this.error = checkNotNull(error);
7577
this.completed = true;
7678
for (Callback<T, E> callback : toBeNotified) {
7779
callback.error(error);

gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ public void testError() {
5858
} catch (IllegalStateException ex) {
5959
// expected
6060
}
61+
try {
62+
result.error(null);
63+
fail();
64+
} catch (NullPointerException exc) {
65+
// expected
66+
}
6167
BaseServiceException ex = new BaseServiceException(0, "message", "reason", false);
6268
result.error(ex);
6369
try {

0 commit comments

Comments
 (0)