Skip to content

Commit

Permalink
Let ListenableFuture implement thenable via a default interface metho…
Browse files Browse the repository at this point in the history
…d in J2CL

RELNOTES=Let ListenableFuture implement thenable via a default interface method in J2CL

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=266186458
  • Loading branch information
stefanhaustein authored and kluever committed Aug 30, 2019
1 parent bbc26e1 commit e0bac74
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions guava-gwt/pom.xml
Expand Up @@ -54,6 +54,11 @@
<artifactId>guava</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.elemental2</groupId>
<artifactId>elemental2-promise</artifactId>
<version>1.0.0-RC1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
Expand Down
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2007 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.common.util.concurrent;

import elemental2.promise.IThenable;
import elemental2.promise.Promise;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import jsinterop.annotations.JsMethod;

/**
* Java super source for ListenableFuture, implementing a structural thenable via a default method.
* For restrictions, please refer to the documentation of the then() method.
*
* <p>This class is not (explicitly) implementing IThenable<V> because "then" is overloaded there
* and the single parameter version would need to be marked native, which does not seem to be
* feasible in interfaces (see "subclassing a class with overloaded methods" in jsinterop
* documentation).
*/
public interface ListenableFuture<V> extends Future<V> {
void addListener(Runnable listener, Executor executor);

/** Note that this method is not expected to be overridden. */
@JsMethod
default <R> Promise<R> then(
IThenable.ThenOnFulfilledCallbackFn<V, R> onFulfilled,
IThenable.ThenOnRejectedCallbackFn<R> onRejected) {
return new Promise<V>(
(resolve, reject) -> {
Futures.addCallback(
this,
new FutureCallback<V>() {
@Override
public void onSuccess(V value) {
resolve.onInvoke(value);
}

@Override
public void onFailure(Throwable throwable) {
reject.onInvoke(throwable.getBackingJsObject());
}
},
MoreExecutors.directExecutor());
})
.then(onFulfilled, onRejected);
}
}
Expand Up @@ -17,6 +17,8 @@
<inherits name="com.google.common.annotations.Annotations"/>
<inherits name="com.google.common.base.Base"/>
<inherits name="com.google.common.collect.Collect"/>
<inherits name="elemental2.promise.Promise"/>
<inherits name="jsinterop.base.Base"/>
<inherits name="com.google.gwt.core.Core"/>

</module>

0 comments on commit e0bac74

Please sign in to comment.