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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.concurrent.Callable;

final class ArrayMetric<T> extends SimpleMetric<T[]> {
public ArrayMetric(@SourceId final String id, final Callable<T @Nullable []> callable) throws IllegalArgumentException {
public ArrayMetric(@SourceId final String id, final Callable<? extends T @Nullable []> callable) throws IllegalArgumentException {
super(id, callable);
}

Expand Down
28 changes: 28 additions & 0 deletions core/src/main/java/dev/faststats/core/data/MapMetric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.faststats.core.data;

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;

final class MapMetric<T> extends SimpleMetric<Map<String, ? extends T>> {
public MapMetric(@SourceId final String id, final Callable<? extends @Nullable Map<String, ? extends T>> callable) throws IllegalArgumentException {
super(id, callable);
}

@Override
public Optional<JsonElement> getData() throws Exception {
return compute().map(data -> {
final var object = new JsonObject();
data.forEach((key, value) -> {
if (value instanceof final Boolean bool) object.addProperty(key, bool);
else if (value instanceof final Number number) object.addProperty(key, number);
else object.addProperty(key, value.toString());
});
return object;
});
}
}
51 changes: 50 additions & 1 deletion core/src/main/java/dev/faststats/core/data/Metric.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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 @@ -33,7 +34,7 @@ public interface Metric<T> {
* @since 0.16.0
*/
@Contract(pure = true)
Optional<T> compute() throws Exception;
Optional<? extends T> compute() throws Exception;

/**
* Get the metric data as a JSON element.
Expand Down Expand Up @@ -96,6 +97,54 @@ static Metric<Number[]> numberArray(@SourceId final String id, final Callable<Nu
return new ArrayMetric<>(id, callable);
}

/**
* Create a string map metric.
*
* @param id the source id
* @param callable the metric data callable
* @return the string map metric
* @throws IllegalArgumentException if the source id is invalid
* @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state).
* @see #compute()
* @since 0.23.0
*/
@Contract(value = "_, _ -> new", pure = true)
static Metric<Map<String, ? extends String>> stringMap(@SourceId final String id, final Callable<? extends @Nullable Map<String, String>> callable) throws IllegalArgumentException {
return new MapMetric<>(id, callable);
}

/**
* Create a boolean map metric.
*
* @param id the source id
* @param callable the metric data callable
* @return the boolean map metric
* @throws IllegalArgumentException if the source id is invalid
* @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state).
* @see #compute()
* @since 0.23.0
*/
@Contract(value = "_, _ -> new", pure = true)
static Metric<Map<String, ? extends Boolean>> booleanMap(@SourceId final String id, final Callable<? extends @Nullable Map<String, Boolean>> callable) throws IllegalArgumentException {
return new MapMetric<>(id, callable);
}

/**
* Create a number map metric.
*
* @param id the source id
* @param callable the metric data callable
* @return the number map metric
* @throws IllegalArgumentException if the source id is invalid
* @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state).
* @see #compute()
* @since 0.23.0
*/
@Contract(value = "_, _ -> new", pure = true)
static Metric<Map<String, ? extends Number>> numberMap(@SourceId final String id, final Callable<? extends @Nullable Map<String, ? extends Number>> callable) throws IllegalArgumentException {
return new MapMetric<>(id, callable);
}

/**
* Create a metric for a boolean value.
*
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/dev/faststats/core/data/SimpleMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

abstract class SimpleMetric<T> implements Metric<T> {
private final @SourceId String id;
private final Callable<@Nullable T> callable;
private final Callable<? extends @Nullable T> callable;

public SimpleMetric(@SourceId final String id, final Callable<@Nullable T> callable) throws IllegalArgumentException {
public SimpleMetric(@SourceId final String id, final Callable<? extends @Nullable T> callable) throws IllegalArgumentException {
if (!id.matches(SourceId.PATTERN)) {
throw new IllegalArgumentException("Invalid source id '" + id + "', must match '" + SourceId.PATTERN + "'");
}
Expand All @@ -28,7 +28,7 @@ public final Optional<T> compute() throws Exception {
}

@Override
public boolean equals(final Object o) {
public boolean equals(@Nullable final Object o) {
if (o == null || getClass() != o.getClass()) return false;
final SimpleMetric<?> that = (SimpleMetric<?>) o;
return Objects.equals(id, that.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.concurrent.Callable;

final class SingleValueMetric<T> extends SimpleMetric<T> {
public SingleValueMetric(@SourceId final String id, final Callable<@Nullable T> callable) throws IllegalArgumentException {
public SingleValueMetric(@SourceId final String id, final Callable<? extends @Nullable T> callable) throws IllegalArgumentException {
super(id, callable);
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.22.1
version=0.23.0