From 81fc843da38f6bfbbce61cc3c505bc5d115972d4 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 22 Apr 2024 18:58:17 +0200 Subject: [PATCH] jso: move interfaces from 'util.function' to 'function' subpackage, rename `JSFunction` to `JSMapping` to avoid name clash --- .../java/org/teavm/jso/core/JSPromise.java | 20 +++++++++---------- .../jso/{util => }/function/JSConsumer.java | 4 ++-- .../JSMapping.java} | 12 +++++------ .../jso/{util => }/function/JSSupplier.java | 4 ++-- .../teavm/samples/promise/PromiseExample.java | 18 ++++++++--------- 5 files changed, 29 insertions(+), 29 deletions(-) rename jso/apis/src/main/java/org/teavm/jso/{util => }/function/JSConsumer.java (93%) rename jso/apis/src/main/java/org/teavm/jso/{util/function/JSFunction.java => function/JSMapping.java} (76%) rename jso/apis/src/main/java/org/teavm/jso/{util => }/function/JSSupplier.java (91%) diff --git a/jso/apis/src/main/java/org/teavm/jso/core/JSPromise.java b/jso/apis/src/main/java/org/teavm/jso/core/JSPromise.java index a4f2ba00e..dbc87e642 100644 --- a/jso/apis/src/main/java/org/teavm/jso/core/JSPromise.java +++ b/jso/apis/src/main/java/org/teavm/jso/core/JSPromise.java @@ -22,9 +22,9 @@ import org.teavm.jso.JSFunctor; import org.teavm.jso.JSMethod; import org.teavm.jso.JSObject; import org.teavm.jso.JSProperty; -import org.teavm.jso.util.function.JSConsumer; -import org.teavm.jso.util.function.JSFunction; -import org.teavm.jso.util.function.JSSupplier; +import org.teavm.jso.function.JSConsumer; +import org.teavm.jso.function.JSMapping; +import org.teavm.jso.function.JSSupplier; /** * Interface for interacting with JavaScript @@ -76,29 +76,29 @@ public class JSPromise implements JSObject { public static native JSPromise reject(Object reason); /** Call {@code onFulfilled} with the success value, resolving with its return value. */ - public native JSPromise then(JSFunction onFulfilled); + public native JSPromise then(JSMapping onFulfilled); /** Call {@code onFulfilled} with the success value or {@code onRejected} with the reject reason, * resolving with its return value. */ - public native JSPromise then(JSFunction onFulfilled, JSFunction onRejected); + public native JSPromise then(JSMapping onFulfilled, JSMapping onRejected); /** Call {@code onFulfilled} with the success value, returning a new promise. */ @JSMethod("then") - public native JSPromise flatThen(JSFunction> onFulfilled); + public native JSPromise flatThen(JSMapping> onFulfilled); /** Call {@code onFulfilled} with the success value or {@code onRejected} with the reject reason, * returning a new promise. */ @JSMethod("then") - public native JSPromise flatThen(JSFunction> onFulfilled, - JSFunction> onRejected); + public native JSPromise flatThen(JSMapping> onFulfilled, + JSMapping> onRejected); /** Call {@code onRejected} with the reject reason, resolving with its return value. */ @JSMethod("catch") - public native JSPromise catchError(JSFunction onRejected); + public native JSPromise catchError(JSMapping onRejected); /** Call {@code onRejected} with the reject reason, returning a new promise. */ @JSMethod("catch") - public native JSPromise flatCatchError(JSFunction> onRejected); + public native JSPromise flatCatchError(JSMapping> onRejected); /** Call {@code onFinally} after settling, ignoring the return value. */ @JSMethod("finally") diff --git a/jso/apis/src/main/java/org/teavm/jso/util/function/JSConsumer.java b/jso/apis/src/main/java/org/teavm/jso/function/JSConsumer.java similarity index 93% rename from jso/apis/src/main/java/org/teavm/jso/util/function/JSConsumer.java rename to jso/apis/src/main/java/org/teavm/jso/function/JSConsumer.java index b4b2b1b1f..dcf62c0b6 100644 --- a/jso/apis/src/main/java/org/teavm/jso/util/function/JSConsumer.java +++ b/jso/apis/src/main/java/org/teavm/jso/function/JSConsumer.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Bernd Busse. + * Copyright 2024 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.jso.util.function; +package org.teavm.jso.function; import java.util.Objects; import org.teavm.jso.JSFunctor; diff --git a/jso/apis/src/main/java/org/teavm/jso/util/function/JSFunction.java b/jso/apis/src/main/java/org/teavm/jso/function/JSMapping.java similarity index 76% rename from jso/apis/src/main/java/org/teavm/jso/util/function/JSFunction.java rename to jso/apis/src/main/java/org/teavm/jso/function/JSMapping.java index 8e9c410ec..8667243d6 100644 --- a/jso/apis/src/main/java/org/teavm/jso/util/function/JSFunction.java +++ b/jso/apis/src/main/java/org/teavm/jso/function/JSMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Bernd Busse. + * Copyright 2024 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.jso.util.function; +package org.teavm.jso.function; import java.util.Objects; import org.teavm.jso.JSFunctor; @@ -21,10 +21,10 @@ import org.teavm.jso.JSObject; @FunctionalInterface @JSFunctor -public interface JSFunction extends JSObject { +public interface JSMapping extends JSObject { R apply(T t); - default JSFunction andThen(JSFunction after) { + default JSMapping andThen(JSMapping after) { Objects.requireNonNull(after); return (T t) -> { @@ -33,7 +33,7 @@ public interface JSFunction extends JSObject { }; } - default JSFunction compose(JSFunction before) { + default JSMapping compose(JSMapping before) { Objects.requireNonNull(before); return (V v) -> { @@ -42,7 +42,7 @@ public interface JSFunction extends JSObject { }; } - static JSFunction identity() { + static JSMapping identity() { return (T t) -> { return t; }; diff --git a/jso/apis/src/main/java/org/teavm/jso/util/function/JSSupplier.java b/jso/apis/src/main/java/org/teavm/jso/function/JSSupplier.java similarity index 91% rename from jso/apis/src/main/java/org/teavm/jso/util/function/JSSupplier.java rename to jso/apis/src/main/java/org/teavm/jso/function/JSSupplier.java index 31caef3d3..6670906f2 100644 --- a/jso/apis/src/main/java/org/teavm/jso/util/function/JSSupplier.java +++ b/jso/apis/src/main/java/org/teavm/jso/function/JSSupplier.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Bernd Busse. + * Copyright 2024 Alexey Andreev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.jso.util.function; +package org.teavm.jso.function; import org.teavm.jso.JSFunctor; import org.teavm.jso.JSObject; diff --git a/samples/promise/src/main/java/org/teavm/samples/promise/PromiseExample.java b/samples/promise/src/main/java/org/teavm/samples/promise/PromiseExample.java index cd2aa9834..812558bb6 100644 --- a/samples/promise/src/main/java/org/teavm/samples/promise/PromiseExample.java +++ b/samples/promise/src/main/java/org/teavm/samples/promise/PromiseExample.java @@ -21,9 +21,9 @@ import org.teavm.jso.browser.Window; import org.teavm.jso.core.JSArray; import org.teavm.jso.core.JSPromise; import org.teavm.jso.core.JSString; -import org.teavm.jso.util.function.JSConsumer; -import org.teavm.jso.util.function.JSFunction; -import org.teavm.jso.util.function.JSSupplier; +import org.teavm.jso.function.JSConsumer; +import org.teavm.jso.function.JSMapping; +import org.teavm.jso.function.JSSupplier; public final class PromiseExample { private static long start = System.currentTimeMillis(); @@ -67,15 +67,15 @@ public final class PromiseExample { private static void checkFunctionalInterface() { JSSupplier supplier = () -> 23; - JSFunction addTwenty = value -> value + 20; - JSFunction subTwenty = value -> value - 20; - JSFunction isPositive = value -> value >= 0; + JSMapping addTwenty = value -> value + 20; + JSMapping subTwenty = value -> value - 20; + JSMapping isPositive = value -> value >= 0; JSConsumer print = value -> report("My value: " + value.toString()); JSConsumer print2 = value -> report("My value plus 10: " + Integer.valueOf(value + 10).toString()); var value = supplier.get(); - report("Supplied value: " + value.toString()); + report("Supplied value: " + value); value = addTwenty.apply(value); report("Value plus 20: " + value.toString()); @@ -176,7 +176,7 @@ public final class PromiseExample { } private static void runLongRunningPromise(Object lock) { - var promise = new JSPromise<>((resolve, reject) -> { + new JSPromise<>((resolve, reject) -> { report("Long promise exection"); report("Wait for a while..."); Window.setTimeout(() -> { @@ -250,7 +250,7 @@ public final class PromiseExample { var item = value.get(i); var msg = "-- Promise " + i + " " + item.getStatus() + " with: "; if (item.getStatus().stringValue().equals("fulfilled")) { - msg += item.getValue().toString(); + msg += item.getValue(); } else if (item.getStatus().stringValue().equals("rejected")) { msg += item.getReason().toString(); }