|
4 | 4 | import org.jetbrains.annotations.Contract; |
5 | 5 | import org.jspecify.annotations.Nullable; |
6 | 6 |
|
| 7 | +import java.util.Map; |
7 | 8 | import java.util.Optional; |
8 | 9 | import java.util.concurrent.Callable; |
9 | 10 |
|
@@ -33,7 +34,7 @@ public interface Metric<T> { |
33 | 34 | * @since 0.16.0 |
34 | 35 | */ |
35 | 36 | @Contract(pure = true) |
36 | | - Optional<T> compute() throws Exception; |
| 37 | + Optional<? extends T> compute() throws Exception; |
37 | 38 |
|
38 | 39 | /** |
39 | 40 | * Get the metric data as a JSON element. |
@@ -96,6 +97,54 @@ static Metric<Number[]> numberArray(@SourceId final String id, final Callable<Nu |
96 | 97 | return new ArrayMetric<>(id, callable); |
97 | 98 | } |
98 | 99 |
|
| 100 | + /** |
| 101 | + * Create a string map metric. |
| 102 | + * |
| 103 | + * @param id the source id |
| 104 | + * @param callable the metric data callable |
| 105 | + * @return the string map metric |
| 106 | + * @throws IllegalArgumentException if the source id is invalid |
| 107 | + * @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state). |
| 108 | + * @see #compute() |
| 109 | + * @since 0.23.0 |
| 110 | + */ |
| 111 | + @Contract(value = "_, _ -> new", pure = true) |
| 112 | + static Metric<Map<String, ? extends String>> stringMap(@SourceId final String id, final Callable<? extends @Nullable Map<String, String>> callable) throws IllegalArgumentException { |
| 113 | + return new MapMetric<>(id, callable); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Create a boolean map metric. |
| 118 | + * |
| 119 | + * @param id the source id |
| 120 | + * @param callable the metric data callable |
| 121 | + * @return the boolean map metric |
| 122 | + * @throws IllegalArgumentException if the source id is invalid |
| 123 | + * @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state). |
| 124 | + * @see #compute() |
| 125 | + * @since 0.23.0 |
| 126 | + */ |
| 127 | + @Contract(value = "_, _ -> new", pure = true) |
| 128 | + static Metric<Map<String, ? extends Boolean>> booleanMap(@SourceId final String id, final Callable<? extends @Nullable Map<String, Boolean>> callable) throws IllegalArgumentException { |
| 129 | + return new MapMetric<>(id, callable); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Create a number map metric. |
| 134 | + * |
| 135 | + * @param id the source id |
| 136 | + * @param callable the metric data callable |
| 137 | + * @return the number map metric |
| 138 | + * @throws IllegalArgumentException if the source id is invalid |
| 139 | + * @apiNote The callable must be thread-safe and pure (i.e. not modify any shared state). |
| 140 | + * @see #compute() |
| 141 | + * @since 0.23.0 |
| 142 | + */ |
| 143 | + @Contract(value = "_, _ -> new", pure = true) |
| 144 | + static Metric<Map<String, ? extends Number>> numberMap(@SourceId final String id, final Callable<? extends @Nullable Map<String, ? extends Number>> callable) throws IllegalArgumentException { |
| 145 | + return new MapMetric<>(id, callable); |
| 146 | + } |
| 147 | + |
99 | 148 | /** |
100 | 149 | * Create a metric for a boolean value. |
101 | 150 | * |
|
0 commit comments