2424import static com .google .cloud .logging .Logging .WriteOption .OptionType .LABELS ;
2525import static com .google .cloud .logging .Logging .WriteOption .OptionType .LOG_NAME ;
2626import static com .google .cloud .logging .Logging .WriteOption .OptionType .RESOURCE ;
27- import static com .google .common .util .concurrent .Futures .lazyTransform ;
2827
2928import com .google .cloud .AsyncPage ;
3029import com .google .cloud .AsyncPageImpl ;
4342import com .google .common .collect .Iterables ;
4443import com .google .common .collect .Lists ;
4544import com .google .common .collect .Maps ;
45+ import com .google .common .util .concurrent .Futures ;
46+ import com .google .common .util .concurrent .ListenableFuture ;
4647import com .google .common .util .concurrent .Uninterruptibles ;
4748import com .google .logging .v2 .CreateLogMetricRequest ;
4849import com .google .logging .v2 .CreateSinkRequest ;
@@ -103,6 +104,14 @@ private static <V> V get(Future<V> future) {
103104 }
104105 }
105106
107+ private static <I , O > Future <O > transform (Future <I > future ,
108+ Function <? super I , ? extends O > function ) {
109+ if (future instanceof ListenableFuture ) {
110+ return Futures .transform ((ListenableFuture <I >) future , function );
111+ }
112+ return Futures .lazyTransform (future , function );
113+ }
114+
106115 private abstract static class BasePageFetcher <T > implements AsyncPageImpl .NextPageFetcher <T > {
107116
108117 private static final long serialVersionUID = 5095123855547444030L ;
@@ -198,7 +207,7 @@ public Future<Sink> createAsync(SinkInfo sink) {
198207 .setParent (ConfigServiceV2Api .formatParentName (options ().projectId ()))
199208 .setSink (sink .toPb (options ().projectId ()))
200209 .build ();
201- return lazyTransform (rpc .create (request ), Sink .fromPbFunction (this ));
210+ return transform (rpc .create (request ), Sink .fromPbFunction (this ));
202211 }
203212
204213 @ Override
@@ -212,7 +221,7 @@ public Future<Sink> updateAsync(SinkInfo sink) {
212221 .setSinkName (ConfigServiceV2Api .formatSinkName (options ().projectId (), sink .name ()))
213222 .setSink (sink .toPb (options ().projectId ()))
214223 .build ();
215- return lazyTransform (rpc .update (request ), Sink .fromPbFunction (this ));
224+ return transform (rpc .update (request ), Sink .fromPbFunction (this ));
216225 }
217226
218227 @ Override
@@ -225,7 +234,7 @@ public Future<Sink> getSinkAsync(String sink) {
225234 GetSinkRequest request = GetSinkRequest .newBuilder ()
226235 .setSinkName (ConfigServiceV2Api .formatSinkName (options ().projectId (), sink ))
227236 .build ();
228- return lazyTransform (rpc .get (request ), Sink .fromPbFunction (this ));
237+ return transform (rpc .get (request ), Sink .fromPbFunction (this ));
229238 }
230239
231240 private static ListSinksRequest listSinksRequest (LoggingOptions serviceOptions ,
@@ -247,7 +256,7 @@ private static Future<AsyncPage<Sink>> listSinksAsync(final LoggingOptions servi
247256 final Map <Option .OptionType , ?> options ) {
248257 final ListSinksRequest request = listSinksRequest (serviceOptions , options );
249258 Future <ListSinksResponse > list = serviceOptions .rpc ().list (request );
250- return lazyTransform (list , new Function <ListSinksResponse , AsyncPage <Sink >>() {
259+ return transform (list , new Function <ListSinksResponse , AsyncPage <Sink >>() {
251260 @ Override
252261 public AsyncPage <Sink > apply (ListSinksResponse listSinksResponse ) {
253262 List <Sink > sinks = listSinksResponse .getSinksList () == null ? ImmutableList .<Sink >of ()
@@ -281,7 +290,7 @@ public Future<Boolean> deleteSinkAsync(String sink) {
281290 DeleteSinkRequest request = DeleteSinkRequest .newBuilder ()
282291 .setSinkName (ConfigServiceV2Api .formatSinkName (options ().projectId (), sink ))
283292 .build ();
284- return lazyTransform (rpc .delete (request ), EMPTY_TO_BOOLEAN_FUNCTION );
293+ return transform (rpc .delete (request ), EMPTY_TO_BOOLEAN_FUNCTION );
285294 }
286295
287296 public boolean deleteLog (String log ) {
@@ -292,7 +301,7 @@ public Future<Boolean> deleteLogAsync(String log) {
292301 DeleteLogRequest request = DeleteLogRequest .newBuilder ()
293302 .setLogName (LoggingServiceV2Api .formatLogName (options ().projectId (), log ))
294303 .build ();
295- return lazyTransform (rpc .delete (request ), EMPTY_TO_BOOLEAN_FUNCTION );
304+ return transform (rpc .delete (request ), EMPTY_TO_BOOLEAN_FUNCTION );
296305 }
297306
298307 private static ListMonitoredResourceDescriptorsRequest listMonitoredResourceDescriptorsRequest (
@@ -316,7 +325,7 @@ private static ListMonitoredResourceDescriptorsRequest listMonitoredResourceDesc
316325 final ListMonitoredResourceDescriptorsRequest request =
317326 listMonitoredResourceDescriptorsRequest (options );
318327 Future <ListMonitoredResourceDescriptorsResponse > list = serviceOptions .rpc ().list (request );
319- return lazyTransform (list , new Function <ListMonitoredResourceDescriptorsResponse ,
328+ return transform (list , new Function <ListMonitoredResourceDescriptorsResponse ,
320329 AsyncPage <MonitoredResourceDescriptor >>() {
321330 @ Override
322331 public AsyncPage <MonitoredResourceDescriptor > apply (
@@ -355,7 +364,7 @@ public Future<Metric> createAsync(MetricInfo metric) {
355364 .setParent (MetricsServiceV2Api .formatParentName (options ().projectId ()))
356365 .setMetric (metric .toPb ())
357366 .build ();
358- return lazyTransform (rpc .create (request ), Metric .fromPbFunction (this ));
367+ return transform (rpc .create (request ), Metric .fromPbFunction (this ));
359368 }
360369
361370 @ Override
@@ -369,7 +378,7 @@ public Future<Metric> updateAsync(MetricInfo metric) {
369378 .setMetricName (MetricsServiceV2Api .formatMetricName (options ().projectId (), metric .name ()))
370379 .setMetric (metric .toPb ())
371380 .build ();
372- return lazyTransform (rpc .update (request ), Metric .fromPbFunction (this ));
381+ return transform (rpc .update (request ), Metric .fromPbFunction (this ));
373382 }
374383
375384 @ Override
@@ -382,7 +391,7 @@ public Future<Metric> getMetricAsync(String metric) {
382391 GetLogMetricRequest request = GetLogMetricRequest .newBuilder ()
383392 .setMetricName (MetricsServiceV2Api .formatMetricName (options ().projectId (), metric ))
384393 .build ();
385- return lazyTransform (rpc .get (request ), Metric .fromPbFunction (this ));
394+ return transform (rpc .get (request ), Metric .fromPbFunction (this ));
386395 }
387396
388397 private static ListLogMetricsRequest listMetricsRequest (LoggingOptions serviceOptions ,
@@ -404,7 +413,7 @@ private static Future<AsyncPage<Metric>> listMetricsAsync(final LoggingOptions s
404413 final Map <Option .OptionType , ?> options ) {
405414 final ListLogMetricsRequest request = listMetricsRequest (serviceOptions , options );
406415 Future <ListLogMetricsResponse > list = serviceOptions .rpc ().list (request );
407- return lazyTransform (list , new Function <ListLogMetricsResponse , AsyncPage <Metric >>() {
416+ return transform (list , new Function <ListLogMetricsResponse , AsyncPage <Metric >>() {
408417 @ Override
409418 public AsyncPage <Metric > apply (ListLogMetricsResponse listMetricsResponse ) {
410419 List <Metric > metrics = listMetricsResponse .getMetricsList () == null
@@ -438,7 +447,7 @@ public Future<Boolean> deleteMetricAsync(String metric) {
438447 DeleteLogMetricRequest request = DeleteLogMetricRequest .newBuilder ()
439448 .setMetricName (MetricsServiceV2Api .formatMetricName (options ().projectId (), metric ))
440449 .build ();
441- return lazyTransform (rpc .delete (request ), EMPTY_TO_BOOLEAN_FUNCTION );
450+ return transform (rpc .delete (request ), EMPTY_TO_BOOLEAN_FUNCTION );
442451 }
443452
444453 private static WriteLogEntriesRequest writeLogEntriesRequest (LoggingOptions serviceOptions ,
@@ -466,9 +475,8 @@ public void write(Iterable<LogEntry> logEntries, WriteOption... options) {
466475 }
467476
468477 public Future <Void > writeAsync (Iterable <LogEntry > logEntries , WriteOption ... options ) {
469- return lazyTransform (
470- rpc .write (writeLogEntriesRequest (options (), logEntries , optionMap (options ))),
471- WRITE_RESPONSE_TO_VOID_FUNCTION );
478+ return transform (rpc .write (writeLogEntriesRequest (options (), logEntries , optionMap (options ))),
479+ WRITE_RESPONSE_TO_VOID_FUNCTION );
472480 }
473481
474482 private static ListLogEntriesRequest listLogEntriesRequest (LoggingOptions serviceOptions ,
@@ -498,7 +506,7 @@ private static Future<AsyncPage<LogEntry>> listLogEntriesAsync(
498506 final LoggingOptions serviceOptions , final Map <Option .OptionType , ?> options ) {
499507 final ListLogEntriesRequest request = listLogEntriesRequest (serviceOptions , options );
500508 Future <ListLogEntriesResponse > list = serviceOptions .rpc ().list (request );
501- return lazyTransform (list , new Function <ListLogEntriesResponse , AsyncPage <LogEntry >>() {
509+ return transform (list , new Function <ListLogEntriesResponse , AsyncPage <LogEntry >>() {
502510 @ Override
503511 public AsyncPage <LogEntry > apply (ListLogEntriesResponse listLogEntrysResponse ) {
504512 List <LogEntry > entries = listLogEntrysResponse .getEntriesList () == null
0 commit comments