Skip to content

Commit 319b783

Browse files
Logging: Changing Future -> ApiFuture
1 parent 5cbbbae commit 319b783

File tree

11 files changed

+292
-296
lines changed

11 files changed

+292
-296
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/Logging.java

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

google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import static com.google.cloud.logging.Logging.WriteOption.OptionType.LOG_NAME;
2626
import static com.google.cloud.logging.Logging.WriteOption.OptionType.RESOURCE;
2727

28+
import com.google.api.gax.core.ApiFuture;
29+
import com.google.api.gax.core.ApiFutures;
2830
import com.google.cloud.AsyncPage;
2931
import com.google.cloud.AsyncPageImpl;
3032
import com.google.cloud.BaseService;
@@ -39,7 +41,6 @@
3941
import com.google.common.collect.Iterables;
4042
import com.google.common.collect.Lists;
4143
import com.google.common.collect.Maps;
42-
import com.google.common.util.concurrent.Futures;
4344
import com.google.common.util.concurrent.Uninterruptibles;
4445
import com.google.logging.v2.CreateLogMetricRequest;
4546
import com.google.logging.v2.CreateSinkRequest;
@@ -65,11 +66,9 @@
6566
import com.google.logging.v2.WriteLogEntriesRequest;
6667
import com.google.logging.v2.WriteLogEntriesResponse;
6768
import com.google.protobuf.Empty;
68-
6969
import java.util.List;
7070
import java.util.Map;
7171
import java.util.concurrent.ExecutionException;
72-
import java.util.concurrent.Future;
7372

7473
class LoggingImpl extends BaseService<LoggingOptions> implements Logging {
7574

@@ -96,17 +95,22 @@ public Void apply(WriteLogEntriesResponse input) {
9695
rpc = options.getRpc();
9796
}
9897

99-
private static <V> V get(Future<V> future) {
98+
private static <V> V get(ApiFuture<V> future) {
10099
try {
101100
return Uninterruptibles.getUninterruptibly(future);
102101
} catch (ExecutionException ex) {
103102
throw Throwables.propagate(ex.getCause());
104103
}
105104
}
106105

107-
private static <I, O> Future<O> transform(Future<I> future,
108-
Function<? super I, ? extends O> function) {
109-
return Futures.lazyTransform(future, function);
106+
private static <I, O> ApiFuture<O> transform(ApiFuture<I> future,
107+
final Function<? super I, ? extends O> function) {
108+
return ApiFutures.transform(future, new com.google.api.gax.core.Function<I, O>() {
109+
@Override
110+
public O apply(I i) {
111+
return function.apply(i);
112+
}
113+
});
110114
}
111115

112116
private abstract static class BasePageFetcher<T> implements AsyncPageImpl.NextPageFetcher<T> {
@@ -143,7 +147,7 @@ private static class SinkPageFetcher extends BasePageFetcher<Sink> {
143147

144148

145149
@Override
146-
public Future<AsyncPage<Sink>> getNextPage() {
150+
public ApiFuture<AsyncPage<Sink>> getNextPage() {
147151
return listSinksAsync(serviceOptions(), requestOptions());
148152
}
149153
}
@@ -160,7 +164,7 @@ private static class MonitoredResourceDescriptorPageFetcher
160164

161165

162166
@Override
163-
public Future<AsyncPage<MonitoredResourceDescriptor>> getNextPage() {
167+
public ApiFuture<AsyncPage<MonitoredResourceDescriptor>> getNextPage() {
164168
return listMonitoredResourceDescriptorsAsync(serviceOptions(), requestOptions());
165169
}
166170
}
@@ -176,7 +180,7 @@ private static class MetricPageFetcher extends BasePageFetcher<Metric> {
176180

177181

178182
@Override
179-
public Future<AsyncPage<Metric>> getNextPage() {
183+
public ApiFuture<AsyncPage<Metric>> getNextPage() {
180184
return listMetricsAsync(serviceOptions(), requestOptions());
181185
}
182186
}
@@ -192,7 +196,7 @@ private static class LogEntryPageFetcher extends BasePageFetcher<LogEntry> {
192196

193197

194198
@Override
195-
public Future<AsyncPage<LogEntry>> getNextPage() {
199+
public ApiFuture<AsyncPage<LogEntry>> getNextPage() {
196200
return listLogEntriesAsync(serviceOptions(), requestOptions());
197201
}
198202
}
@@ -203,7 +207,7 @@ public Sink create(SinkInfo sink) {
203207
}
204208

205209
@Override
206-
public Future<Sink> createAsync(SinkInfo sink) {
210+
public ApiFuture<Sink> createAsync(SinkInfo sink) {
207211
CreateSinkRequest request = CreateSinkRequest.newBuilder()
208212
.setParent(ProjectName.create(getOptions().getProjectId()).toString())
209213
.setSink(sink.toPb(getOptions().getProjectId()))
@@ -217,7 +221,7 @@ public Sink update(SinkInfo sink) {
217221
}
218222

219223
@Override
220-
public Future<Sink> updateAsync(SinkInfo sink) {
224+
public ApiFuture<Sink> updateAsync(SinkInfo sink) {
221225
UpdateSinkRequest request = UpdateSinkRequest.newBuilder()
222226
.setSinkName(SinkName.create(getOptions().getProjectId(), sink.getName()).toString())
223227
.setSink(sink.toPb(getOptions().getProjectId()))
@@ -231,7 +235,7 @@ public Sink getSink(String sink) {
231235
}
232236

233237
@Override
234-
public Future<Sink> getSinkAsync(String sink) {
238+
public ApiFuture<Sink> getSinkAsync(String sink) {
235239
GetSinkRequest request = GetSinkRequest.newBuilder()
236240
.setSinkName(SinkName.create(getOptions().getProjectId(), sink).toString())
237241
.build();
@@ -253,10 +257,10 @@ private static ListSinksRequest listSinksRequest(LoggingOptions serviceOptions,
253257
return builder.build();
254258
}
255259

256-
private static Future<AsyncPage<Sink>> listSinksAsync(final LoggingOptions serviceOptions,
260+
private static ApiFuture<AsyncPage<Sink>> listSinksAsync(final LoggingOptions serviceOptions,
257261
final Map<Option.OptionType, ?> options) {
258262
final ListSinksRequest request = listSinksRequest(serviceOptions, options);
259-
Future<ListSinksResponse> list = serviceOptions.getRpc().list(request);
263+
ApiFuture<ListSinksResponse> list = serviceOptions.getRpc().list(request);
260264
return transform(list, new Function<ListSinksResponse, AsyncPage<Sink>>() {
261265
@Override
262266
public AsyncPage<Sink> apply(ListSinksResponse listSinksResponse) {
@@ -277,7 +281,7 @@ public Page<Sink> listSinks(ListOption... options) {
277281
}
278282

279283
@Override
280-
public Future<AsyncPage<Sink>> listSinksAsync(ListOption... options) {
284+
public ApiFuture<AsyncPage<Sink>> listSinksAsync(ListOption... options) {
281285
return listSinksAsync(getOptions(), optionMap(options));
282286
}
283287

@@ -287,7 +291,7 @@ public boolean deleteSink(String sink) {
287291
}
288292

289293
@Override
290-
public Future<Boolean> deleteSinkAsync(String sink) {
294+
public ApiFuture<Boolean> deleteSinkAsync(String sink) {
291295
DeleteSinkRequest request = DeleteSinkRequest.newBuilder()
292296
.setSinkName(SinkName.create(getOptions().getProjectId(), sink).toString())
293297
.build();
@@ -298,7 +302,7 @@ public boolean deleteLog(String log) {
298302
return get(deleteLogAsync(log));
299303
}
300304

301-
public Future<Boolean> deleteLogAsync(String log) {
305+
public ApiFuture<Boolean> deleteLogAsync(String log) {
302306
DeleteLogRequest request = DeleteLogRequest.newBuilder()
303307
.setLogName(LogName.create(getOptions().getProjectId(), log).toString())
304308
.build();
@@ -320,12 +324,12 @@ private static ListMonitoredResourceDescriptorsRequest listMonitoredResourceDesc
320324
return builder.build();
321325
}
322326

323-
private static Future<AsyncPage<MonitoredResourceDescriptor>>
327+
private static ApiFuture<AsyncPage<MonitoredResourceDescriptor>>
324328
listMonitoredResourceDescriptorsAsync(final LoggingOptions serviceOptions,
325329
final Map<Option.OptionType, ?> options) {
326330
final ListMonitoredResourceDescriptorsRequest request =
327331
listMonitoredResourceDescriptorsRequest(options);
328-
Future<ListMonitoredResourceDescriptorsResponse> list = serviceOptions.getRpc().list(request);
332+
ApiFuture<ListMonitoredResourceDescriptorsResponse> list = serviceOptions.getRpc().list(request);
329333
return transform(list, new Function<ListMonitoredResourceDescriptorsResponse,
330334
AsyncPage<MonitoredResourceDescriptor>>() {
331335
@Override
@@ -349,7 +353,7 @@ public Page<MonitoredResourceDescriptor> listMonitoredResourceDescriptors(ListOp
349353
return get(listMonitoredResourceDescriptorsAsync(options));
350354
}
351355

352-
public Future<AsyncPage<MonitoredResourceDescriptor>> listMonitoredResourceDescriptorsAsync(
356+
public ApiFuture<AsyncPage<MonitoredResourceDescriptor>> listMonitoredResourceDescriptorsAsync(
353357
ListOption... options) {
354358
return listMonitoredResourceDescriptorsAsync(getOptions(), optionMap(options));
355359
}
@@ -360,7 +364,7 @@ public Metric create(MetricInfo metric) {
360364
}
361365

362366
@Override
363-
public Future<Metric> createAsync(MetricInfo metric) {
367+
public ApiFuture<Metric> createAsync(MetricInfo metric) {
364368
CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder()
365369
.setParent(ProjectName.create(getOptions().getProjectId()).toString())
366370
.setMetric(metric.toPb())
@@ -374,7 +378,7 @@ public Metric update(MetricInfo metric) {
374378
}
375379

376380
@Override
377-
public Future<Metric> updateAsync(MetricInfo metric) {
381+
public ApiFuture<Metric> updateAsync(MetricInfo metric) {
378382
UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder()
379383
.setMetricName(MetricName.create(getOptions().getProjectId(), metric.getName()).toString())
380384
.setMetric(metric.toPb())
@@ -388,7 +392,7 @@ public Metric getMetric(String metric) {
388392
}
389393

390394
@Override
391-
public Future<Metric> getMetricAsync(String metric) {
395+
public ApiFuture<Metric> getMetricAsync(String metric) {
392396
GetLogMetricRequest request = GetLogMetricRequest.newBuilder()
393397
.setMetricName(MetricName.create(getOptions().getProjectId(), metric).toString())
394398
.build();
@@ -410,10 +414,10 @@ private static ListLogMetricsRequest listMetricsRequest(LoggingOptions serviceOp
410414
return builder.build();
411415
}
412416

413-
private static Future<AsyncPage<Metric>> listMetricsAsync(final LoggingOptions serviceOptions,
417+
private static ApiFuture<AsyncPage<Metric>> listMetricsAsync(final LoggingOptions serviceOptions,
414418
final Map<Option.OptionType, ?> options) {
415419
final ListLogMetricsRequest request = listMetricsRequest(serviceOptions, options);
416-
Future<ListLogMetricsResponse> list = serviceOptions.getRpc().list(request);
420+
ApiFuture<ListLogMetricsResponse> list = serviceOptions.getRpc().list(request);
417421
return transform(list, new Function<ListLogMetricsResponse, AsyncPage<Metric>>() {
418422
@Override
419423
public AsyncPage<Metric> apply(ListLogMetricsResponse listMetricsResponse) {
@@ -434,7 +438,7 @@ public Page<Metric> listMetrics(ListOption... options) {
434438
}
435439

436440
@Override
437-
public Future<AsyncPage<Metric>> listMetricsAsync(ListOption... options) {
441+
public ApiFuture<AsyncPage<Metric>> listMetricsAsync(ListOption... options) {
438442
return listMetricsAsync(getOptions(), optionMap(options));
439443
}
440444

@@ -444,7 +448,7 @@ public boolean deleteMetric(String metric) {
444448
}
445449

446450
@Override
447-
public Future<Boolean> deleteMetricAsync(String metric) {
451+
public ApiFuture<Boolean> deleteMetricAsync(String metric) {
448452
DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder()
449453
.setMetricName(MetricName.create(getOptions().getProjectId(), metric).toString())
450454
.build();
@@ -475,7 +479,7 @@ public void write(Iterable<LogEntry> logEntries, WriteOption... options) {
475479
get(writeAsync(logEntries, options));
476480
}
477481

478-
public Future<Void> writeAsync(Iterable<LogEntry> logEntries, WriteOption... options) {
482+
public ApiFuture<Void> writeAsync(Iterable<LogEntry> logEntries, WriteOption... options) {
479483
return transform(
480484
rpc.write(writeLogEntriesRequest(getOptions(), logEntries, optionMap(options))),
481485
WRITE_RESPONSE_TO_VOID_FUNCTION);
@@ -504,10 +508,10 @@ private static ListLogEntriesRequest listLogEntriesRequest(LoggingOptions servic
504508
return builder.build();
505509
}
506510

507-
private static Future<AsyncPage<LogEntry>> listLogEntriesAsync(
511+
private static ApiFuture<AsyncPage<LogEntry>> listLogEntriesAsync(
508512
final LoggingOptions serviceOptions, final Map<Option.OptionType, ?> options) {
509513
final ListLogEntriesRequest request = listLogEntriesRequest(serviceOptions, options);
510-
Future<ListLogEntriesResponse> list = serviceOptions.getRpc().list(request);
514+
ApiFuture<ListLogEntriesResponse> list = serviceOptions.getRpc().list(request);
511515
return transform(list, new Function<ListLogEntriesResponse, AsyncPage<LogEntry>>() {
512516
@Override
513517
public AsyncPage<LogEntry> apply(ListLogEntriesResponse listLogEntrysResponse) {
@@ -528,7 +532,7 @@ public Page<LogEntry> listLogEntries(EntryListOption... options) {
528532
}
529533

530534
@Override
531-
public Future<AsyncPage<LogEntry>> listLogEntriesAsync(EntryListOption... options) {
535+
public ApiFuture<AsyncPage<LogEntry>> listLogEntriesAsync(EntryListOption... options) {
532536
return listLogEntriesAsync(getOptions(), optionMap(options));
533537
}
534538

google-cloud-logging/src/main/java/com/google/cloud/logging/Metric.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21+
import com.google.api.gax.core.ApiFuture;
2122
import com.google.common.base.Function;
2223
import com.google.logging.v2.LogMetric;
2324

2425
import java.io.IOException;
2526
import java.io.ObjectInputStream;
2627
import java.util.Objects;
27-
import java.util.concurrent.Future;
2828

2929
/**
3030
* Stackdriver Logging metrics describe logs-based metric. The value of the metric is the number of
@@ -141,13 +141,13 @@ public boolean delete() {
141141
}
142142

143143
/**
144-
* Sends a request for deleting this metric. This method returns a {@code Future} object to
145-
* consume the result. {@link Future#get()} returns {@code true} if the metric was deleted,
144+
* Sends a request for deleting this metric. This method returns a {@code ApiFuture} object to
145+
* consume the result. {@link ApiFuture#get()} returns {@code true} if the metric was deleted,
146146
* {@code false} if it was not found.
147147
*
148148
* <p>Example of asynchronously deleting the metric.
149149
* <pre> {@code
150-
* Future<Boolean> future = metric.deleteAsync();
150+
* ApiFuture<Boolean> future = metric.deleteAsync();
151151
* // ...
152152
* boolean deleted = future.get();
153153
* if (deleted) {
@@ -159,7 +159,7 @@ public boolean delete() {
159159
*
160160
* @throws LoggingException upon failure
161161
*/
162-
public Future<Boolean> deleteAsync() {
162+
public ApiFuture<Boolean> deleteAsync() {
163163
return logging.deleteMetricAsync(getName());
164164
}
165165

@@ -183,12 +183,12 @@ public Metric reload() {
183183

184184
/**
185185
* Sends a request to fetch current metric's latest information. This method returns a
186-
* {@code Future} object to consume the result. {@link Future#get()} returns a {@code Metric}
186+
* {@code ApiFuture} object to consume the result. {@link ApiFuture#get()} returns a {@code Metric}
187187
* object with latest information or {@code null} if not found.
188188
*
189189
* <p>Example of asynchronously getting the metric's latest information.
190190
* <pre> {@code
191-
* Future<Metric> future = metric.reloadAsync();
191+
* ApiFuture<Metric> future = metric.reloadAsync();
192192
* // ...
193193
* Metric latestMetric = future.get();
194194
* if (latestMetric == null) {
@@ -198,7 +198,7 @@ public Metric reload() {
198198
*
199199
* @throws LoggingException upon failure
200200
*/
201-
public Future<Metric> reloadAsync() {
201+
public ApiFuture<Metric> reloadAsync() {
202202
return logging.getMetricAsync(getName());
203203
}
204204

@@ -222,12 +222,12 @@ public Metric update() {
222222

223223
/**
224224
* Sends a request to update current metric. If the metric does not exist, it is created. This
225-
* method returns a {@code Future} object to consume the result. {@link Future#get()} returns a
225+
* method returns a {@code ApiFuture} object to consume the result. {@link ApiFuture#get()} returns a
226226
* {@code Metric} object with updated information.
227227
*
228228
* <p>Example of asynchronously updating the metric's information.
229229
* <pre> {@code
230-
* Future<Metric> future = metric.toBuilder()
230+
* ApiFuture<Metric> future = metric.toBuilder()
231231
* .setDescription("A more detailed description")
232232
* .build()
233233
* .updateAsync();
@@ -237,7 +237,7 @@ public Metric update() {
237237
*
238238
* @throws LoggingException upon failure
239239
*/
240-
public Future<Metric> updateAsync() {
240+
public ApiFuture<Metric> updateAsync() {
241241
return logging.updateAsync(this);
242242
}
243243

0 commit comments

Comments
 (0)