Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/src/main/java/dev/faststats/core/chart/ArrayChart.java
Original file line number Diff line number Diff line change
@@ -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<T> extends SimpleChart<T[]> {
public ArrayChart(@ChartId String id, Callable<T @Nullable []> callable) throws IllegalArgumentException {
super(id, callable);
}

@Override
public Optional<JsonElement> 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;
});
}
}
45 changes: 37 additions & 8 deletions core/src/main/java/dev/faststats/core/chart/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -50,21 +49,51 @@ public interface Chart<T> {
Optional<JsonElement> 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<String[]> stringArray(@ChartId String id, Callable<String @Nullable []> 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<Boolean[]> booleanArray(@ChartId String id, Callable<Boolean @Nullable []> 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<Map<String, Number>> bar(@ChartId String id, Callable<@Nullable Map<String, Number>> callable) throws IllegalArgumentException {
return new SimpleBarChart(id, callable);
static Chart<Number[]> numberArray(@ChartId String id, Callable<Number @Nullable []> callable) throws IllegalArgumentException {
return new ArrayChart<>(id, callable);
}

/**
Expand Down
25 changes: 0 additions & 25 deletions core/src/main/java/dev/faststats/core/chart/SimpleBarChart.java

This file was deleted.

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.4.2
version=0.5.0
Loading