Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let ListenableFuture implement thenable via a default interface metho…
…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
1 parent
bbc26e1
commit e0bac74
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ogle/common/util/concurrent/super/com/google/common/util/concurrent/ListenableFuture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters