FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add transformAsync and bump version to 1.1.0 (#28) · googleapis/api-common-java@c3c17c5 · GitHub

This repository was archived by the owner on Sep 27, 2023. It is now read-only.
/ api-common-java Public archive

Commit c3c17c5

Browse files
authored
add transformAsync and bump version to 1.1.0 (#28)
1 parent 6275504 commit c3c17c5

5 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎build.gradle‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')
235235
}
236236
}
237237

238+
gradle.projectsEvaluated {
239+
tasks.withType(JavaCompile) {
240+
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
241+
}
242+
}
243+
238244
// Formatting tasks
239245
// ================
240246

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2017, Google Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following disclaimer
13+
* in the documentation and/or other materials provided with the
14+
* distribution.
15+
* * Neither the name of Google Inc. nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.google.api.core;
32+
33+
/**
34+
* Transforms a value, possibly asynchronously.
35+
*
36+
* <p>It is similar to Guava's {@code AsyncFunction}, redeclared so that Guava can be shaded.
37+
*/
38+
public interface ApiAsyncFunction<I, O> {
39+
/**
40+
* Returns an output Future to use in place of the given input. The output Future need not be
41+
* done, making AsyncFunction suitable for asynchronous derivations.
42+
*
43+
* <p>Throwing an exception from this method is equivalent to returning a failing Future.
44+
*/
45+
ApiFuture<O> apply(I input) throws Exception;
46+
}

‎src/main/java/com/google/api/core/ApiFutures.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import com.google.common.base.Function;
3434
import com.google.common.collect.Iterables;
35+
import com.google.common.util.concurrent.AsyncFunction;
3536
import com.google.common.util.concurrent.FutureCallback;
3637
import com.google.common.util.concurrent.Futures;
3738
import com.google.common.util.concurrent.ListenableFuture;
@@ -99,6 +100,21 @@ public ListenableFuture<V> apply(ApiFuture<V> apiFuture) {
99100
})));
100101
}
101102

103+
public static <I, O> ApiFuture<O> transformAsync(
104+
ApiFuture<I> input, final ApiAsyncFunction<I, O> function) {
105+
ListenableFuture<I> listenableInput = listenableFutureForApiFuture(input);
106+
ListenableFuture<O> listenableOutput =
107+
Futures.transformAsync(
108+
listenableInput,
109+
new AsyncFunction<I, O>() {
110+
@Override
111+
public ListenableFuture<O> apply(I input) throws Exception {
112+
return listenableFutureForApiFuture(function.apply(input));
113+
}
114+
});
115+
return new ListenableFutureToApiFuture<>(listenableOutput);
116+
}
117+
102118
private static <V> ListenableFuture<V> listenableFutureForApiFuture(ApiFuture<V> apiFuture) {
103119
ListenableFuture<V> listenableFuture;
104120
if (apiFuture instanceof AbstractApiFuture) {

‎src/test/java/com/google/api/core/ApiFuturesTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,19 @@ public void testAllAsList() throws Exception {
102102
inputFuture2.set(2);
103103
Truth.assertThat(listFuture.get()).containsExactly(1, 2).inOrder();
104104
}
105+
106+
@Test
107+
public void testTransformAsync() throws Exception {
108+
ApiFuture<Integer> inputFuture = ApiFutures.immediateFuture(0);
109+
ApiFuture<Integer> outputFuture =
110+
ApiFutures.transformAsync(
111+
inputFuture,
112+
new ApiAsyncFunction<Integer, Integer>() {
113+
@Override
114+
public ApiFuture<Integer> apply(Integer input) {
115+
return ApiFutures.immediateFuture(input + 1);
116+
}
117+
});
118+
Truth.assertThat(outputFuture.get()).isEqualTo(1);
119+
}
105120
}

‎version.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.1.0

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL