From e86a7e8804c6994b5777f44893eb03c3f32d1f6f Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 14 Apr 2016 10:11:38 -0400 Subject: [PATCH] Disallowed null error in BatchResult. Adjusted doc. (#921) --- .../main/java/com/google/cloud/BatchResult.java | 16 +++++++++------- .../java/com/google/cloud/BatchResultTest.java | 6 ++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java index 388551d5aaea..3d5b06fd1db6 100644 --- a/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java +++ b/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java @@ -16,14 +16,18 @@ package com.google.cloud; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import java.util.LinkedList; import java.util.List; /** - * This class holds a single result of a batch call. {@code T} is the type of the result and {@code - * E} is the type of the service-dependent exception thrown when a processing error occurs. + * This class holds a single result of a batch call. The class is not thread-safe. + * + * @param the type of the result + * @param the type of the service-dependent exception thrown when a processing error occurs + * */ public abstract class BatchResult { @@ -44,7 +48,7 @@ public boolean completed() { * Returns the result of this call. * * @throws IllegalStateException if the batch has not been completed yet - * @throws E if an error occurred when processing this request + * @throws E if an error occurred when processing the batch request */ public T get() throws E { checkState(completed(), "Batch has not been completed yet"); @@ -60,10 +64,8 @@ public T get() throws E { * @throws IllegalStateException if the batch has been completed already */ public void notify(Callback callback) { - if (completed) { - throw new IllegalStateException("The batch has been completed. All the calls to the notify()" + checkState(!completed, "The batch has been completed. All the calls to the notify()" + " method should be done prior to submitting the batch."); - } toBeNotified.add(callback); } @@ -71,7 +73,7 @@ public void notify(Callback callback) { * Sets an error and status as completed. Notifies all callbacks. */ protected void error(E error) { - this.error = error; + this.error = checkNotNull(error); this.completed = true; for (Callback callback : toBeNotified) { callback.error(error); diff --git a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java index 85172170144d..8fb26bc8ce36 100644 --- a/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java @@ -58,6 +58,12 @@ public void testError() { } catch (IllegalStateException ex) { // expected } + try { + result.error(null); + fail(); + } catch (NullPointerException exc) { + // expected + } BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); result.error(ex); try {