@@ -52,20 +52,12 @@ class FirestoreImpl implements Firestore {
5252 private static final String AUTO_ID_ALPHABET =
5353 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
5454
55- /** Creates a pseudo-random 20-character ID that can be used for Firestore documents. */
56- static String autoId () {
57- StringBuilder builder = new StringBuilder ();
58- int maxRandom = AUTO_ID_ALPHABET .length ();
59- for (int i = 0 ; i < AUTO_ID_LENGTH ; i ++) {
60- builder .append (AUTO_ID_ALPHABET .charAt (RANDOM .nextInt (maxRandom )));
61- }
62- return builder .toString ();
63- }
64-
6555 private final FirestoreRpc firestoreClient ;
6656 private final FirestoreOptions firestoreOptions ;
6757 private final ResourcePath databasePath ;
6858
59+ private boolean closed ;
60+
6961 FirestoreImpl (FirestoreOptions options ) {
7062 this (options , options .getFirestoreRpc ());
7163 }
@@ -81,6 +73,16 @@ static String autoId() {
8173 ResourcePath .create (DatabaseRootName .of (options .getProjectId (), options .getDatabaseId ()));
8274 }
8375
76+ /** Creates a pseudo-random 20-character ID that can be used for Firestore documents. */
77+ static String autoId () {
78+ StringBuilder builder = new StringBuilder ();
79+ int maxRandom = AUTO_ID_ALPHABET .length ();
80+ for (int i = 0 ; i < AUTO_ID_LENGTH ; i ++) {
81+ builder .append (AUTO_ID_ALPHABET .charAt (RANDOM .nextInt (maxRandom )));
82+ }
83+ return builder .toString ();
84+ }
85+
8486 @ Nonnull
8587 @ Override
8688 public WriteBatch batch () {
@@ -324,6 +326,7 @@ FirestoreRpc getClient() {
324326 /** Request funnel for all read/write requests. */
325327 <RequestT , ResponseT > ApiFuture <ResponseT > sendRequest (
326328 RequestT requestT , UnaryCallable <RequestT , ResponseT > callable ) {
329+ Preconditions .checkState (!closed , "Firestore client has already been closed" );
327330 return callable .futureCall (requestT );
328331 }
329332
@@ -332,18 +335,26 @@ <RequestT, ResponseT> void streamRequest(
332335 RequestT requestT ,
333336 ApiStreamObserver <ResponseT > responseObserverT ,
334337 ServerStreamingCallable <RequestT , ResponseT > callable ) {
338+ Preconditions .checkState (!closed , "Firestore client has already been closed" );
335339 callable .serverStreamingCall (requestT , responseObserverT );
336340 }
337341
338342 /** Request funnel for all bidirectional streaming requests. */
339343 <RequestT , ResponseT > ApiStreamObserver <RequestT > streamRequest (
340344 ApiStreamObserver <ResponseT > responseObserverT ,
341345 BidiStreamingCallable <RequestT , ResponseT > callable ) {
346+ Preconditions .checkState (!closed , "Firestore client has already been closed" );
342347 return callable .bidiStreamingCall (responseObserverT );
343348 }
344349
345350 @ Override
346351 public FirestoreOptions getOptions () {
347352 return firestoreOptions ;
348353 }
354+
355+ @ Override
356+ public void close () throws Exception {
357+ firestoreClient .close ();
358+ closed = true ;
359+ }
349360}
0 commit comments