16
16
17
17
package com .google .cloud ;
18
18
19
+ import static com .google .common .base .Preconditions .checkNotNull ;
19
20
import static com .google .common .base .Preconditions .checkState ;
20
21
21
22
import java .util .LinkedList ;
22
23
import java .util .List ;
23
24
24
25
/**
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
+ *
27
31
*/
28
32
public abstract class BatchResult <T , E extends BaseServiceException > {
29
33
@@ -44,7 +48,7 @@ public boolean completed() {
44
48
* Returns the result of this call.
45
49
*
46
50
* @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
48
52
*/
49
53
public T get () throws E {
50
54
checkState (completed (), "Batch has not been completed yet" );
@@ -60,18 +64,16 @@ public T get() throws E {
60
64
* @throws IllegalStateException if the batch has been completed already
61
65
*/
62
66
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()"
65
68
+ " method should be done prior to submitting the batch." );
66
- }
67
69
toBeNotified .add (callback );
68
70
}
69
71
70
72
/**
71
73
* Sets an error and status as completed. Notifies all callbacks.
72
74
*/
73
75
protected void error (E error ) {
74
- this .error = error ;
76
+ this .error = checkNotNull ( error ) ;
75
77
this .completed = true ;
76
78
for (Callback <T , E > callback : toBeNotified ) {
77
79
callback .error (error );
0 commit comments