Skip to content

Commit a8ba615

Browse files
authored
Merge pull request #42 from faststats-dev/feat/array-charts
Add array charts
2 parents 2a50d14 + 6a84da3 commit a8ba615

File tree

4 files changed

+67
-34
lines changed

4 files changed

+67
-34
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dev.faststats.core.chart;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import org.jspecify.annotations.Nullable;
6+
7+
import java.util.Optional;
8+
import java.util.concurrent.Callable;
9+
10+
final class ArrayChart<T> extends SimpleChart<T[]> {
11+
public ArrayChart(@ChartId String id, Callable<T @Nullable []> callable) throws IllegalArgumentException {
12+
super(id, callable);
13+
}
14+
15+
@Override
16+
public Optional<JsonElement> getData() throws Exception {
17+
return compute().map(data -> {
18+
var elements = new JsonArray(data.length);
19+
for (var d : data) {
20+
switch (d) {
21+
case Boolean b -> elements.add(b);
22+
case Number n -> elements.add(n);
23+
default -> elements.add(d.toString());
24+
}
25+
}
26+
return elements;
27+
});
28+
}
29+
}

core/src/main/java/dev/faststats/core/chart/Chart.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.jetbrains.annotations.Contract;
55
import org.jspecify.annotations.Nullable;
66

7-
import java.util.Map;
87
import java.util.Optional;
98
import java.util.concurrent.Callable;
109

@@ -50,21 +49,51 @@ public interface Chart<T> {
5049
Optional<JsonElement> getData() throws Exception;
5150

5251
/**
53-
* Create a bar chart.
52+
* Create a string array chart.
5453
*
5554
* @param id the chart id
5655
* @param callable the chart data callable
57-
* @return the bar chart
56+
* @return the string array chart
5857
* @throws IllegalArgumentException if the chart id is invalid
5958
* @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state).
6059
* @see #compute()
61-
* @since 0.1.0
60+
* @since 0.5.0
61+
*/
62+
@Contract(value = "_, _ -> new", pure = true)
63+
static Chart<String[]> stringArray(@ChartId String id, Callable<String @Nullable []> callable) throws IllegalArgumentException {
64+
return new ArrayChart<>(id, callable);
65+
}
66+
67+
/**
68+
* Create a boolean array chart.
69+
*
70+
* @param id the chart id
71+
* @param callable the chart data callable
72+
* @return the boolean array chart
73+
* @throws IllegalArgumentException if the chart id is invalid
74+
* @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state).
75+
* @see #compute()
76+
* @since 0.5.0
77+
*/
78+
@Contract(value = "_, _ -> new", pure = true)
79+
static Chart<Boolean[]> booleanArray(@ChartId String id, Callable<Boolean @Nullable []> callable) throws IllegalArgumentException {
80+
return new ArrayChart<>(id, callable);
81+
}
82+
83+
/**
84+
* Create a number array chart.
85+
*
86+
* @param id the chart id
87+
* @param callable the chart data callable
88+
* @return the number array chart
89+
* @throws IllegalArgumentException if the chart id is invalid
90+
* @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state).
91+
* @see #compute()
92+
* @since 0.5.0
6293
*/
63-
// todo: introduce a better way to transmit multiple values
64-
@Deprecated
6594
@Contract(value = "_, _ -> new", pure = true)
66-
static Chart<Map<String, Number>> bar(@ChartId String id, Callable<@Nullable Map<String, Number>> callable) throws IllegalArgumentException {
67-
return new SimpleBarChart(id, callable);
95+
static Chart<Number[]> numberArray(@ChartId String id, Callable<Number @Nullable []> callable) throws IllegalArgumentException {
96+
return new ArrayChart<>(id, callable);
6897
}
6998

7099
/**

core/src/main/java/dev/faststats/core/chart/SimpleBarChart.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=0.4.2
1+
version=0.5.0

0 commit comments

Comments
 (0)