From c631dc51c69a0489dc4fc4d5dbaa75c1fd67d0c8 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 7 Dec 2025 09:13:41 +0100 Subject: [PATCH 1/2] Update version to 0.5.0 in gradle.properties --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 43ae76cb..59a78457 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=0.4.2 +version=0.5.0 From 6a84da3b9139b5a606325225ede4f37410fed848 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 7 Dec 2025 09:13:48 +0100 Subject: [PATCH 2/2] Refactor chart creation to support array types --- .../dev/faststats/core/chart/ArrayChart.java | 29 ++++++++++++ .../java/dev/faststats/core/chart/Chart.java | 45 +++++++++++++++---- .../faststats/core/chart/SimpleBarChart.java | 25 ----------- 3 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 core/src/main/java/dev/faststats/core/chart/ArrayChart.java delete mode 100644 core/src/main/java/dev/faststats/core/chart/SimpleBarChart.java diff --git a/core/src/main/java/dev/faststats/core/chart/ArrayChart.java b/core/src/main/java/dev/faststats/core/chart/ArrayChart.java new file mode 100644 index 00000000..81849428 --- /dev/null +++ b/core/src/main/java/dev/faststats/core/chart/ArrayChart.java @@ -0,0 +1,29 @@ +package dev.faststats.core.chart; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import org.jspecify.annotations.Nullable; + +import java.util.Optional; +import java.util.concurrent.Callable; + +final class ArrayChart extends SimpleChart { + public ArrayChart(@ChartId String id, Callable callable) throws IllegalArgumentException { + super(id, callable); + } + + @Override + public Optional getData() throws Exception { + return compute().map(data -> { + var elements = new JsonArray(data.length); + for (var d : data) { + switch (d) { + case Boolean b -> elements.add(b); + case Number n -> elements.add(n); + default -> elements.add(d.toString()); + } + } + return elements; + }); + } +} diff --git a/core/src/main/java/dev/faststats/core/chart/Chart.java b/core/src/main/java/dev/faststats/core/chart/Chart.java index b97d0248..84a5af42 100644 --- a/core/src/main/java/dev/faststats/core/chart/Chart.java +++ b/core/src/main/java/dev/faststats/core/chart/Chart.java @@ -4,7 +4,6 @@ import org.jetbrains.annotations.Contract; import org.jspecify.annotations.Nullable; -import java.util.Map; import java.util.Optional; import java.util.concurrent.Callable; @@ -50,21 +49,51 @@ public interface Chart { Optional getData() throws Exception; /** - * Create a bar chart. + * Create a string array chart. * * @param id the chart id * @param callable the chart data callable - * @return the bar chart + * @return the string array chart * @throws IllegalArgumentException if the chart id is invalid * @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state). * @see #compute() - * @since 0.1.0 + * @since 0.5.0 + */ + @Contract(value = "_, _ -> new", pure = true) + static Chart stringArray(@ChartId String id, Callable callable) throws IllegalArgumentException { + return new ArrayChart<>(id, callable); + } + + /** + * Create a boolean array chart. + * + * @param id the chart id + * @param callable the chart data callable + * @return the boolean array chart + * @throws IllegalArgumentException if the chart id is invalid + * @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state). + * @see #compute() + * @since 0.5.0 + */ + @Contract(value = "_, _ -> new", pure = true) + static Chart booleanArray(@ChartId String id, Callable callable) throws IllegalArgumentException { + return new ArrayChart<>(id, callable); + } + + /** + * Create a number array chart. + * + * @param id the chart id + * @param callable the chart data callable + * @return the number array chart + * @throws IllegalArgumentException if the chart id is invalid + * @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state). + * @see #compute() + * @since 0.5.0 */ - // todo: introduce a better way to transmit multiple values - @Deprecated @Contract(value = "_, _ -> new", pure = true) - static Chart> bar(@ChartId String id, Callable<@Nullable Map> callable) throws IllegalArgumentException { - return new SimpleBarChart(id, callable); + static Chart numberArray(@ChartId String id, Callable callable) throws IllegalArgumentException { + return new ArrayChart<>(id, callable); } /** diff --git a/core/src/main/java/dev/faststats/core/chart/SimpleBarChart.java b/core/src/main/java/dev/faststats/core/chart/SimpleBarChart.java deleted file mode 100644 index ee16d839..00000000 --- a/core/src/main/java/dev/faststats/core/chart/SimpleBarChart.java +++ /dev/null @@ -1,25 +0,0 @@ -package dev.faststats.core.chart; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import org.jspecify.annotations.Nullable; - -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; - -@Deprecated -final class SimpleBarChart extends SimpleChart> { - public SimpleBarChart(@ChartId String id, Callable<@Nullable Map> callable) throws IllegalArgumentException { - super(id, callable); - } - - @Override - public Optional getData() throws Exception { - return compute().filter(bar -> !bar.isEmpty()).map(bar -> { - var values = new JsonObject(); - bar.forEach(values::addProperty); - return values; - }); - } -}