From 0ef53049e5b1e947ea0b448e60f155eb86a1271b Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Fri, 15 Nov 2024 15:39:59 +0100 Subject: [PATCH] Use `FunctionalInterface` for Python function. --- .../src/main/java/com/example/PyGalServiceMixed.java | 11 ++++++++--- .../java/com/example/demo/PyGalServiceMixed.java | 12 +++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/graalpy/graalpy-micronaut-pygal-charts/src/main/java/com/example/PyGalServiceMixed.java b/graalpy/graalpy-micronaut-pygal-charts/src/main/java/com/example/PyGalServiceMixed.java index f93fc7e..d8e9e4e 100644 --- a/graalpy/graalpy-micronaut-pygal-charts/src/main/java/com/example/PyGalServiceMixed.java +++ b/graalpy/graalpy-micronaut-pygal-charts/src/main/java/com/example/PyGalServiceMixed.java @@ -14,7 +14,7 @@ @Singleton public class PyGalServiceMixed implements PyGalService { - private final Value pythonFunctionXY; + private final RenderXYFunction pythonFunctionXY; PyGalServiceMixed(GraalPyContext graalPyContext) { pythonFunctionXY = graalPyContext.eval( @@ -29,12 +29,17 @@ def render_xy(title, label_datapoint_entries): xy_chart.add(entry.label(), entry.dataPoints()) return xy_chart.render().decode() - render_xy"""); + render_xy""").as(RenderXYFunction.class); } public record Entry(String label, double[][] dataPoints) { } + @FunctionalInterface + public interface RenderXYFunction { + String apply(String title, List labelDatapointEntries); + } + @Override public String renderXYChart() { String title = "XY Cosinus"; @@ -46,6 +51,6 @@ public String renderXYChart() { new Entry("y = 1", new double[][]{{-5, 1}, {5, 1}}), new Entry("y = -1", new double[][]{{-5, -1}, {5, -1}}) ); - return pythonFunctionXY.execute(title, labelDatapointEntries).asString(); + return pythonFunctionXY.apply(title, labelDatapointEntries); } } diff --git a/graalpy/graalpy-spring-boot-pygal-charts/src/main/java/com/example/demo/PyGalServiceMixed.java b/graalpy/graalpy-spring-boot-pygal-charts/src/main/java/com/example/demo/PyGalServiceMixed.java index f0fc98b..1a05af8 100644 --- a/graalpy/graalpy-spring-boot-pygal-charts/src/main/java/com/example/demo/PyGalServiceMixed.java +++ b/graalpy/graalpy-spring-boot-pygal-charts/src/main/java/com/example/demo/PyGalServiceMixed.java @@ -15,7 +15,7 @@ @Service public class PyGalServiceMixed implements PyGalService { - private final Value pythonFunctionXY; + private final RenderXYFunction pythonFunctionXY; PyGalServiceMixed(GraalPyContext graalPyContext) { pythonFunctionXY = graalPyContext.eval( @@ -30,12 +30,18 @@ def render_xy(title, label_datapoint_entries): xy_chart.add(entry.label(), entry.dataPoints()) return xy_chart.render().decode() - render_xy"""); + render_xy""").as(RenderXYFunction.class); } public record Entry(String label, double[][] dataPoints) { } + + @FunctionalInterface + public interface RenderXYFunction { + String apply(String title, List labelDatapointEntries); + } + @Override public String renderXYChart() { String title = "XY Cosinus"; @@ -47,6 +53,6 @@ public String renderXYChart() { new Entry("y = 1", new double[][]{{-5, 1}, {5, 1}}), new Entry("y = -1", new double[][]{{-5, -1}, {5, -1}}) ); - return pythonFunctionXY.execute(title, labelDatapointEntries).asString(); + return pythonFunctionXY.apply(title, labelDatapointEntries); } }