Skip to content

Commit 9b0cb40

Browse files
committed
Refactor factory generics for consistency across modules
1 parent d1106ae commit 9b0cb40

File tree

14 files changed

+73
-42
lines changed

14 files changed

+73
-42
lines changed

bukkit/src/main/java/dev/faststats/bukkit/BukkitMetrics.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public sealed interface BukkitMetrics extends Metrics permits BukkitMetricsImpl
1717
* @since 0.1.0
1818
*/
1919
@Contract(pure = true)
20-
static Metrics.Factory<Plugin> factory() {
20+
static Factory factory() {
2121
return new BukkitMetricsImpl.Factory();
2222
}
23+
24+
interface Factory extends Metrics.Factory<Plugin, Factory> {
25+
}
2326
}

bukkit/src/main/java/dev/faststats/bukkit/BukkitMetricsImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class BukkitMetricsImpl extends SimpleMetrics implements BukkitMetrics {
2222

2323
@Async.Schedule
2424
@Contract(mutates = "io")
25-
private BukkitMetricsImpl(SimpleMetrics.Factory<?> factory, Plugin plugin, Path config) throws IllegalStateException {
25+
private BukkitMetricsImpl(Factory factory, Plugin plugin, Path config) throws IllegalStateException {
2626
super(factory, config);
2727

2828
this.logger = plugin.getLogger();
@@ -91,7 +91,7 @@ private <T> Optional<T> tryOrEmpty(Supplier<T> supplier) {
9191
}
9292
}
9393

94-
static final class Factory extends SimpleMetrics.Factory<Plugin> {
94+
static final class Factory extends SimpleMetrics.Factory<Plugin, BukkitMetrics.Factory> implements BukkitMetrics.Factory {
9595
@Override
9696
public Metrics create(Plugin plugin) throws IllegalStateException {
9797
var dataFolder = getPluginsFolder(plugin).resolve("faststats");

bungeecord/src/main/java/dev/faststats/bungee/BungeeMetrics.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public sealed interface BungeeMetrics extends Metrics permits BungeeMetricsImpl
1717
* @since 0.1.0
1818
*/
1919
@Contract(pure = true)
20-
static Factory<Plugin> factory() {
20+
static Factory factory() {
2121
return new BungeeMetricsImpl.Factory();
2222
}
23+
24+
interface Factory extends Metrics.Factory<Plugin, Factory> {
25+
}
2326
}

bungeecord/src/main/java/dev/faststats/bungee/BungeeMetricsImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class BungeeMetricsImpl extends SimpleMetrics implements BungeeMetrics {
2020

2121
@Async.Schedule
2222
@Contract(mutates = "io")
23-
private BungeeMetricsImpl(SimpleMetrics.Factory<?> factory, Plugin plugin, Path config) throws IllegalStateException {
23+
private BungeeMetricsImpl(Factory factory, Plugin plugin, Path config) throws IllegalStateException {
2424
super(factory, config);
2525

2626
this.logger = plugin.getLogger();
@@ -54,7 +54,7 @@ protected void printWarning(String message) {
5454
logger.warning(message);
5555
}
5656

57-
static final class Factory extends SimpleMetrics.Factory<Plugin> {
57+
static final class Factory extends SimpleMetrics.Factory<Plugin, BungeeMetrics.Factory> implements BungeeMetrics.Factory {
5858
@Override
5959
public Metrics create(Plugin plugin) throws IllegalStateException {
6060
var dataFolder = plugin.getProxy().getPluginsFolder().toPath().resolve("faststats");

core/src/main/java/dev/faststats/core/Metrics.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public interface Metrics {
5858
*
5959
* @since 0.1.0
6060
*/
61-
interface Factory<T> {
61+
interface Factory<T, F extends Factory<T, F>> {
6262
/**
6363
* Adds a chart to the metrics submission.
6464
* <p>
@@ -70,7 +70,7 @@ interface Factory<T> {
7070
* @since 0.1.0
7171
*/
7272
@Contract(mutates = "this")
73-
Factory<T> addChart(Chart<?> chart) throws IllegalArgumentException;
73+
F addChart(Chart<?> chart) throws IllegalArgumentException;
7474

7575
/**
7676
* Sets the error tracker for this metrics instance.
@@ -82,7 +82,7 @@ interface Factory<T> {
8282
* @since 0.10.0
8383
*/
8484
@Contract(mutates = "this")
85-
Factory<T> errorTracker(ErrorTracker tracker);
85+
F errorTracker(ErrorTracker tracker);
8686

8787
/**
8888
* Enables or disabled debug mode for this metrics instance.
@@ -97,7 +97,7 @@ interface Factory<T> {
9797
* @since 0.1.0
9898
*/
9999
@Contract(mutates = "this")
100-
Factory<T> debug(boolean enabled);
100+
F debug(boolean enabled);
101101

102102
/**
103103
* Sets the token used to authenticate with the metrics server and identify the project.
@@ -110,7 +110,7 @@ interface Factory<T> {
110110
* @since 0.1.0
111111
*/
112112
@Contract(mutates = "this")
113-
Factory<T> token(@Token String token) throws IllegalArgumentException;
113+
F token(@Token String token) throws IllegalArgumentException;
114114

115115
/**
116116
* Sets the metrics server URL.
@@ -122,23 +122,22 @@ interface Factory<T> {
122122
* @since 0.1.0
123123
*/
124124
@Contract(mutates = "this")
125-
Factory<T> url(URI url);
125+
F url(URI url);
126126

127127
/**
128128
* Creates a new metrics instance.
129129
* <p>
130130
* Metrics submission will start automatically.
131131
*
132-
* @param plugin the plugin instance
132+
* @param object a required object as defined by the implementation
133133
* @return the metrics instance
134134
* @throws IllegalStateException if the token is not specified
135-
* @throws IllegalArgumentException if the given object is not a valid plugin
136135
* @see #token(String)
137136
* @since 0.1.0
138137
*/
139138
@Async.Schedule
140139
@Contract(value = "_ -> new", mutates = "io")
141-
Metrics create(T plugin) throws IllegalStateException, IllegalArgumentException;
140+
Metrics create(T object) throws IllegalStateException;
142141
}
143142

144143
/**

core/src/main/java/dev/faststats/core/SimpleMetrics.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public abstract class SimpleMetrics implements Metrics {
5151

5252
@Contract(mutates = "io")
5353
@SuppressWarnings("PatternValidation")
54-
protected SimpleMetrics(Factory<?> factory, Config config) throws IllegalStateException {
54+
protected SimpleMetrics(Factory<?, ?> factory, Config config) throws IllegalStateException {
5555
if (factory.token == null) throw new IllegalStateException("Token must be specified");
5656

5757
this.config = config;
@@ -72,7 +72,7 @@ protected SimpleMetrics(Factory<?> factory, Config config) throws IllegalStateEx
7272
}
7373

7474
@Contract(mutates = "io")
75-
protected SimpleMetrics(Factory<?> factory, Path config) throws IllegalStateException {
75+
protected SimpleMetrics(Factory<?, ?> factory, Path config) throws IllegalStateException {
7676
this(factory, Config.read(config));
7777
}
7878

@@ -126,7 +126,7 @@ private void startSubmitting(long initialDelay, long period, TimeUnit unit) {
126126
var separatorLength = 0;
127127
var split = getOnboardingMessage().split("\n");
128128
for (var s : split) if (s.length() > separatorLength) separatorLength = s.length();
129-
129+
130130
printInfo("-".repeat(separatorLength));
131131
for (var s : split) printInfo(s);
132132
printInfo("-".repeat(separatorLength));
@@ -309,44 +309,49 @@ public void shutdown() {
309309
executor = null;
310310
}
311311

312-
public abstract static class Factory<T> implements Metrics.Factory<T> {
312+
public abstract static class Factory<T, F extends Metrics.Factory<T, F>> implements Metrics.Factory<T, F> {
313313
private final Set<Chart<?>> charts = new HashSet<>(0);
314314
private URI url = URI.create("https://metrics.faststats.dev/v1/collect");
315315
private @Nullable ErrorTracker tracker;
316316
private @Nullable String token;
317317
private boolean debug = false;
318318

319319
@Override
320-
public Metrics.Factory<T> addChart(Chart<?> chart) throws IllegalArgumentException {
320+
@SuppressWarnings("unchecked")
321+
public F addChart(Chart<?> chart) throws IllegalArgumentException {
321322
if (!charts.add(chart)) throw new IllegalArgumentException("Chart already added: " + chart.getId());
322-
return this;
323+
return (F) this;
323324
}
324325

325326
@Override
326-
public Metrics.Factory<T> errorTracker(ErrorTracker tracker) {
327+
@SuppressWarnings("unchecked")
328+
public F errorTracker(ErrorTracker tracker) {
327329
this.tracker = tracker;
328-
return this;
330+
return (F) this;
329331
}
330332

331333
@Override
332-
public Metrics.Factory<T> debug(boolean enabled) {
334+
@SuppressWarnings("unchecked")
335+
public F debug(boolean enabled) {
333336
this.debug = enabled;
334-
return this;
337+
return (F) this;
335338
}
336339

337340
@Override
338-
public Metrics.Factory<T> token(@Token String token) throws IllegalArgumentException {
341+
@SuppressWarnings("unchecked")
342+
public F token(@Token String token) throws IllegalArgumentException {
339343
if (!token.matches(Token.PATTERN)) {
340344
throw new IllegalArgumentException("Invalid token '" + token + "', must match '" + Token.PATTERN + "'");
341345
}
342346
this.token = token;
343-
return this;
347+
return (F) this;
344348
}
345349

346350
@Override
347-
public Metrics.Factory<T> url(URI url) {
351+
@SuppressWarnings("unchecked")
352+
public F url(URI url) {
348353
this.url = url;
349-
return this;
354+
return (F) this;
350355
}
351356
}
352357

hytale/src/main/java/dev/faststats/hytale/HytaleMetrics.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public sealed interface HytaleMetrics extends Metrics permits HytaleMetricsImpl
1717
* @since 0.9.0
1818
*/
1919
@Contract(pure = true)
20-
static Metrics.Factory<JavaPlugin> factory() {
20+
static Factory factory() {
2121
return new HytaleMetricsImpl.Factory();
2222
}
23+
24+
interface Factory extends Metrics.Factory<JavaPlugin, Factory> {
25+
}
2326
}

hytale/src/main/java/dev/faststats/hytale/HytaleMetricsImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class HytaleMetricsImpl extends SimpleMetrics implements HytaleMetrics {
1818

1919
@Async.Schedule
2020
@Contract(mutates = "io")
21-
private HytaleMetricsImpl(SimpleMetrics.Factory<?> factory, HytaleLogger logger, Path config) throws IllegalStateException {
21+
private HytaleMetricsImpl(Factory factory, HytaleLogger logger, Path config) throws IllegalStateException {
2222
super(factory, config);
2323
this.logger = logger;
2424

@@ -47,7 +47,7 @@ protected void printWarning(String message) {
4747
logger.atWarning().log(message);
4848
}
4949

50-
static final class Factory extends SimpleMetrics.Factory<JavaPlugin> {
50+
static final class Factory extends SimpleMetrics.Factory<JavaPlugin, HytaleMetrics.Factory> implements HytaleMetrics.Factory {
5151
@Override
5252
public Metrics create(JavaPlugin plugin) throws IllegalStateException {
5353
var mods = plugin.getDataDirectory().toAbsolutePath().getParent();

minestom/src/main/java/dev/faststats/minestom/MinestomMetrics.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public sealed interface MinestomMetrics extends Metrics permits MinestomMetricsI
1717
* @since 0.1.0
1818
*/
1919
@Contract(pure = true)
20-
static Metrics.Factory<MinecraftServer> factory() {
20+
static Factory factory() {
2121
return new MinestomMetricsImpl.Factory();
2222
}
23+
24+
interface Factory extends Metrics.Factory<MinecraftServer, Factory> {
25+
}
2326
}

minestom/src/main/java/dev/faststats/minestom/MinestomMetricsImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class MinestomMetricsImpl extends SimpleMetrics implements MinestomMetrics
1818

1919
@Async.Schedule
2020
@Contract(mutates = "io")
21-
private MinestomMetricsImpl(SimpleMetrics.Factory<?> factory, Path config) throws IllegalStateException {
21+
private MinestomMetricsImpl(Factory factory, Path config) throws IllegalStateException {
2222
super(factory, config);
2323

2424
startSubmitting();
@@ -47,7 +47,7 @@ protected void printWarning(String message) {
4747
logger.warn(message);
4848
}
4949

50-
static final class Factory extends SimpleMetrics.Factory<MinecraftServer> {
50+
static final class Factory extends SimpleMetrics.Factory<MinecraftServer, MinestomMetrics.Factory> implements MinestomMetrics.Factory {
5151
@Override
5252
public Metrics create(MinecraftServer server) throws IllegalStateException {
5353
var config = Path.of("faststats", "config.properties");

0 commit comments

Comments
 (0)