Skip to content

Commit 9117f75

Browse files
authored
Deprecated the Task API and introduced ApiFutures (firebase#66)
* Deprecated the Task API and introduced ApiFutures * Adding *Async() methods to DatabaseReference and OnDisconnect. Deprecated the methods that return Tasks. * Updated documentation; Fixed the isDone() method of TaskToApiFuture * Deprecated the Task API and introduced ApiFutures * Adding *Async() methods to DatabaseReference and OnDisconnect. Deprecated the methods that return Tasks. * Updated documentation; Fixed the isDone() method of TaskToApiFuture
1 parent ce4363c commit 9117f75

19 files changed

Lines changed: 739 additions & 395 deletions

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@
343343
<artifactId>guava</artifactId>
344344
<version>20.0</version>
345345
</dependency>
346+
<dependency>
347+
<groupId>com.google.api</groupId>
348+
<artifactId>api-common</artifactId>
349+
<version>1.1.0</version>
350+
</dependency>
346351
<dependency>
347352
<groupId>com.google.cloud</groupId>
348353
<artifactId>google-cloud-storage</artifactId>

src/main/java/com/google/firebase/auth/FirebaseAuth.java

Lines changed: 153 additions & 25 deletions
Large diffs are not rendered by default.

src/main/java/com/google/firebase/database/DatabaseReference.java

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.firebase.database;
1818

19+
import com.google.api.core.ApiFuture;
1920
import com.google.firebase.database.core.CompoundWrite;
2021
import com.google.firebase.database.core.DatabaseConfig;
2122
import com.google.firebase.database.core.Path;
@@ -32,6 +33,7 @@
3233
import com.google.firebase.database.utilities.Utilities;
3334
import com.google.firebase.database.utilities.Validation;
3435
import com.google.firebase.database.utilities.encoding.CustomClassMapper;
36+
import com.google.firebase.internal.TaskToApiFuture;
3537
import com.google.firebase.tasks.Task;
3638

3739
import java.io.UnsupportedEncodingException;
@@ -172,14 +174,12 @@ public DatabaseReference push() {
172174
* <code>Map&lt;String, MyPOJO&gt;</code>, as well as null values.
173175
*
174176
* @param value The value to set at this location
175-
* @return The {@link Task} for this operation.
177+
* @return The ApiFuture for this operation.
176178
*/
177-
public Task<Void> setValue(Object value) {
178-
return setValueInternal(value, PriorityUtilities.parsePriority(null), null);
179+
public ApiFuture<Void> setValueAsync(Object value) {
180+
return new TaskToApiFuture<>(setValue(value));
179181
}
180182

181-
// Set priority
182-
183183
/**
184184
* Set the data and priority to the given values. Passing null to setValue() will delete the data
185185
* at the specified location. The native types accepted by this method for the value correspond to
@@ -212,7 +212,30 @@ public Task<Void> setValue(Object value) {
212212
*
213213
* @param value The value to set at this location
214214
* @param priority The priority to set at this location
215+
* @return The ApiFuture for this operation.
216+
*/
217+
public ApiFuture<Void> setValueAsync(Object value, Object priority) {
218+
return new TaskToApiFuture<>(setValue(value, priority));
219+
}
220+
221+
/**
222+
* Similar to {@link #setValueAsync(Object)} but returns a Task.
223+
*
224+
* @param value The value to set at this location
215225
* @return The {@link Task} for this operation.
226+
* @deprecated Use {@link #setValueAsync(Object)}
227+
*/
228+
public Task<Void> setValue(Object value) {
229+
return setValueInternal(value, PriorityUtilities.parsePriority(null), null);
230+
}
231+
232+
/**
233+
* Similar to {@link #setValueAsync(Object, Object)} but returns a Task.
234+
*
235+
* @param value The value to set at this location
236+
* @param priority The priority to set at this location
237+
* @return The {@link Task} for this operation.
238+
* @deprecated Use {@link #setValueAsync(Object, Object)}
216239
*/
217240
public Task<Void> setValue(Object value, Object priority) {
218241
return setValueInternal(value, PriorityUtilities.parsePriority(priority), null);
@@ -292,8 +315,6 @@ public void setValue(Object value, Object priority, CompletionListener listener)
292315
setValueInternal(value, PriorityUtilities.parsePriority(priority), listener);
293316
}
294317

295-
// Update
296-
297318
private Task<Void> setValueInternal(Object value, Node priority, CompletionListener optListener) {
298319
Validation.validateWritablePath(getPath());
299320
ValidationPath.validateWithObject(getPath(), value);
@@ -311,6 +332,8 @@ public void run() {
311332
return wrapped.getFirst();
312333
}
313334

335+
// Set priority
336+
314337
/**
315338
* Set a priority for the data at this Database location. Priorities can be used to provide a
316339
* custom ordering for the children at a location (if no priorities are specified, the children
@@ -338,7 +361,18 @@ public void run() {
338361
* they can be parsed as a 32-bit integer.
339362
*
340363
* @param priority The priority to set at the specified location.
364+
* @return The ApiFuture for this operation.
365+
*/
366+
public ApiFuture<Void> setPriorityAsync(Object priority) {
367+
return new TaskToApiFuture<>(setPriority(priority));
368+
}
369+
370+
/**
371+
* Similar to {@link #setPriorityAsync(Object)} but returns a Task.
372+
*
373+
* @param priority The priority to set at the specified location.
341374
* @return The {@link Task} for this operation.
375+
* @deprecated Use {@link #setPriorityAsync(Object)}
342376
*/
343377
public Task<Void> setPriority(Object priority) {
344378
return setPriorityInternal(PriorityUtilities.parsePriority(priority), null);
@@ -394,12 +428,25 @@ public void run() {
394428
return wrapped.getFirst();
395429
}
396430

431+
// Update
432+
397433
/**
398434
* Update the specific child keys to the specified values. Passing null in a map to
399435
* updateChildren() will remove the value at the specified location.
400436
*
401437
* @param update The paths to update and their new values
438+
* @return The ApiFuture for this operation.
439+
*/
440+
public ApiFuture<Void> updateChildrenAsync(Map<String, Object> update) {
441+
return new TaskToApiFuture<>(updateChildren(update));
442+
}
443+
444+
/**
445+
* Similar to {@link #updateChildrenAsync(Map)} but returns a Task.
446+
*
447+
* @param update The paths to update and their new values
402448
* @return The {@link Task} for this operation.
449+
* @deprecated Use {@link #updateChildrenAsync(Map)}
403450
*/
404451
public Task<Void> updateChildren(Map<String, Object> update) {
405452
return updateChildrenInternal(update, null);
@@ -444,7 +491,17 @@ public void run() {
444491
/**
445492
* Set the value at this location to 'null'
446493
*
447-
* @return The {@link Task} for this operation.
494+
* @return The ApiFuture for this operation.
495+
*/
496+
public ApiFuture<Void> removeValueAsync() {
497+
return new TaskToApiFuture<>(removeValue());
498+
}
499+
500+
/**
501+
* Similar to {@link #removeValueAsync()} but returns a Task.
502+
*
503+
* @return The Task for this operation.
504+
* @deprecated Use {@link #removeValueAsync()}
448505
*/
449506
public Task<Void> removeValue() {
450507
return setValue(null);

src/main/java/com/google/firebase/database/OnDisconnect.java

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

1717
package com.google.firebase.database;
1818

19+
import com.google.api.core.ApiFuture;
1920
import com.google.firebase.database.DatabaseReference.CompletionListener;
2021
import com.google.firebase.database.core.Path;
2122
import com.google.firebase.database.core.Repo;
@@ -27,6 +28,7 @@
2728
import com.google.firebase.database.utilities.Utilities;
2829
import com.google.firebase.database.utilities.Validation;
2930
import com.google.firebase.database.utilities.encoding.CustomClassMapper;
31+
import com.google.firebase.internal.TaskToApiFuture;
3032
import com.google.firebase.tasks.Task;
3133

3234
import java.util.Map;
@@ -51,44 +53,35 @@ public class OnDisconnect {
5153
}
5254

5355
/**
54-
* Ensure the data at this location is set to the specified value when the client is disconnected
55-
* (due to closing the browser, navigating to a new page, or network issues). <br>
56-
* <br>
57-
* This method is especially useful for implementing "presence" systems, where a value should be
58-
* changed or cleared when a user disconnects so that they appear "offline" to other users.
56+
* Similar to {@link #setValueAsync(Object)}, but returns a Task.
5957
*
6058
* @param value The value to be set when a disconnect occurs
6159
* @return The {@link Task} for this operation.
60+
* @deprecated Use {@link #setValueAsync(Object)}
6261
*/
6362
public Task<Void> setValue(Object value) {
6463
return onDisconnectSetInternal(value, PriorityUtilities.NullPriority(), null);
6564
}
6665

6766
/**
68-
* Ensure the data at this location is set to the specified value and priority when the client is
69-
* disconnected (due to closing the browser, navigating to a new page, or network issues). <br>
70-
* <br>
71-
* This method is especially useful for implementing "presence" systems, where a value should be
72-
* changed or cleared when a user disconnects so that they appear "offline" to other users.
67+
* Similar to {@link #setValueAsync(Object, String)}, but returns a Task.
7368
*
7469
* @param value The value to be set when a disconnect occurs
7570
* @param priority The priority to be set when a disconnect occurs
7671
* @return The {@link Task} for this operation.
72+
* @deprecated Use {@link #setValueAsync(Object, String)}
7773
*/
7874
public Task<Void> setValue(Object value, String priority) {
7975
return onDisconnectSetInternal(value, PriorityUtilities.parsePriority(priority), null);
8076
}
8177

8278
/**
83-
* Ensure the data at this location is set to the specified value and priority when the client is
84-
* disconnected (due to closing the browser, navigating to a new page, or network issues). <br>
85-
* <br>
86-
* This method is especially useful for implementing "presence" systems, where a value should be
87-
* changed or cleared when a user disconnects so that they appear "offline" to other users.
79+
* Similar to {@link #setValueAsync(Object, double)}, but returns a Task.
8880
*
8981
* @param value The value to be set when a disconnect occurs
9082
* @param priority The priority to be set when a disconnect occurs
9183
* @return The {@link Task} for this operation.
84+
* @deprecated Use {@link #setValueAsync(Object, double)}
9285
*/
9386
public Task<Void> setValue(Object value, double priority) {
9487
return onDisconnectSetInternal(value, PriorityUtilities.parsePriority(priority), null);
@@ -153,6 +146,50 @@ public void setValue(Object value, Map priority, CompletionListener listener) {
153146
onDisconnectSetInternal(value, PriorityUtilities.parsePriority(priority), listener);
154147
}
155148

149+
/**
150+
* Ensure the data at this location is set to the specified value when the client is disconnected
151+
* (due to closing the browser, navigating to a new page, or network issues). <br>
152+
* <br>
153+
* This method is especially useful for implementing "presence" systems, where a value should be
154+
* changed or cleared when a user disconnects so that they appear "offline" to other users.
155+
*
156+
* @param value The value to be set when a disconnect occurs
157+
* @return The ApiFuture for this operation.
158+
*/
159+
public ApiFuture<Void> setValueAsync(Object value) {
160+
return new TaskToApiFuture<>(setValue(value));
161+
}
162+
163+
/**
164+
* Ensure the data at this location is set to the specified value and priority when the client is
165+
* disconnected (due to closing the browser, navigating to a new page, or network issues). <br>
166+
* <br>
167+
* This method is especially useful for implementing "presence" systems, where a value should be
168+
* changed or cleared when a user disconnects so that they appear "offline" to other users.
169+
*
170+
* @param value The value to be set when a disconnect occurs
171+
* @param priority The priority to be set when a disconnect occurs
172+
* @return The ApiFuture for this operation.
173+
*/
174+
public ApiFuture<Void> setValueAsync(Object value, String priority) {
175+
return new TaskToApiFuture<>(setValue(value, priority));
176+
}
177+
178+
/**
179+
* Ensure the data at this location is set to the specified value and priority when the client is
180+
* disconnected (due to closing the browser, navigating to a new page, or network issues). <br>
181+
* <br>
182+
* This method is especially useful for implementing "presence" systems, where a value should be
183+
* changed or cleared when a user disconnects so that they appear "offline" to other users.
184+
*
185+
* @param value The value to be set when a disconnect occurs
186+
* @param priority The priority to be set when a disconnect occurs
187+
* @return The ApiFuture for this operation.
188+
*/
189+
public ApiFuture<Void> setValueAsync(Object value, double priority) {
190+
return new TaskToApiFuture<>(setValue(value, priority));
191+
}
192+
156193
private Task<Void> onDisconnectSetInternal(
157194
Object value, Node priority, final CompletionListener optListener) {
158195
Validation.validateWritablePath(path);
@@ -174,10 +211,11 @@ public void run() {
174211
// Update
175212

176213
/**
177-
* Ensure the data has the specified child values updated when the client is disconnected
214+
* Similar to {@link #updateChildrenAsync(Map)}, but returns a Task.
178215
*
179216
* @param update The paths to update, along with their desired values
180217
* @return The {@link Task} for this operation.
218+
* @deprecated Use {@link #updateChildrenAsync(Map)}
181219
*/
182220
public Task<Void> updateChildren(Map<String, Object> update) {
183221
return updateChildrenInternal(update, null);
@@ -193,6 +231,16 @@ public void updateChildren(final Map<String, Object> update, final CompletionLis
193231
updateChildrenInternal(update, listener);
194232
}
195233

234+
/**
235+
* Ensure the data has the specified child values updated when the client is disconnected
236+
*
237+
* @param update The paths to update, along with their desired values
238+
* @return The ApiFuture for this operation.
239+
*/
240+
public ApiFuture<Void> updateChildrenAsync(Map<String, Object> update) {
241+
return new TaskToApiFuture<>(updateChildren(update));
242+
}
243+
196244
private Task<Void> updateChildrenInternal(
197245
final Map<String, Object> update, final CompletionListener optListener) {
198246
final Map<Path, Node> parsedUpdate = Validation.parseAndValidateUpdate(path, update);
@@ -210,9 +258,10 @@ public void run() {
210258
// Remove
211259

212260
/**
213-
* Remove the value at this location when the client disconnects
261+
* Similar to {@link #removeValueAsync()}, but returns a Task.
214262
*
215263
* @return The {@link Task} for this operation.
264+
* @deprecated Use {@link #removeValueAsync()}
216265
*/
217266
public Task<Void> removeValue() {
218267
return setValue(null);
@@ -227,12 +276,22 @@ public void removeValue(CompletionListener listener) {
227276
setValue(null, listener);
228277
}
229278

279+
/**
280+
* Remove the value at this location when the client disconnects
281+
*
282+
* @return The ApiFuture for this operation.
283+
*/
284+
public ApiFuture<Void> removeValueAsync() {
285+
return new TaskToApiFuture<>(removeValue());
286+
}
287+
230288
// Cancel the operation
231289

232290
/**
233-
* Cancel any disconnect operations that are queued up at this location
291+
* Similar to {@link #cancelAsync()} ()}, but returns a Task.
234292
*
235293
* @return The {@link Task} for this operation.
294+
* @deprecated Use {@link #cancelAsync()}.
236295
*/
237296
public Task<Void> cancel() {
238297
return cancelInternal(null);
@@ -247,6 +306,15 @@ public void cancel(final CompletionListener listener) {
247306
cancelInternal(listener);
248307
}
249308

309+
/**
310+
* Cancel any disconnect operations that are queued up at this location
311+
*
312+
* @return The ApiFuture for this operation.
313+
*/
314+
public ApiFuture<Void> cancelAsync() {
315+
return new TaskToApiFuture<>(cancel());
316+
}
317+
250318
private Task<Void> cancelInternal(final CompletionListener optListener) {
251319
final Pair<Task<Void>, CompletionListener> wrapped = Utilities.wrapOnComplete(optListener);
252320
repo.scheduleNow(

0 commit comments

Comments
 (0)