Skip to content

Commit

Permalink
Simplify config Option
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinrobot committed Nov 19, 2023
1 parent e2e04b3 commit 5bed29b
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 113 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/vinrobot/mcemote/MinecraftEmote.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.fabricmc.loader.api.FabricLoader;
import net.vinrobot.mcemote.config.ConfigurationManager;
import net.vinrobot.mcemote.config.ConfigurationService;
import net.vinrobot.mcemote.config.impl.file.FileConfigurationService;
import net.vinrobot.mcemote.config.file.FileConfigurationService;

import java.nio.file.Path;

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/vinrobot/mcemote/config/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package net.vinrobot.mcemote.config;

public interface Configuration {
Option<String> twitchId();
import net.vinrobot.mcemote.config.options.Option;
import net.vinrobot.mcemote.config.options.TwitchIdOption;

public class Configuration {
private static final String SCRAPIE_TWITCH_ID = "40646018";

private final Option<String> twitchId = new TwitchIdOption(SCRAPIE_TWITCH_ID);

public Option<String> twitchId() {
return this.twitchId;
}
}
29 changes: 15 additions & 14 deletions src/main/java/net/vinrobot/mcemote/config/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import static net.vinrobot.mcemote.MinecraftEmoteMod.LOGGER;

public class ConfigurationManager {
private final Set<ChangeCallback> changeCallbacks = new LinkedHashSet<>();
private final Queue<ChangeCallback> changeCallbacks = new ConcurrentLinkedQueue<>();
private final ConfigurationService configService;
private Configuration configuration;

public ConfigurationManager(final ConfigurationService configService) {
this.configService = configService;
this.configService = Objects.requireNonNull(configService);
}

public Configuration getConfig() {
Expand All @@ -23,28 +24,28 @@ public Configuration getConfig() {
}

public Configuration load() {
final ConfigurationService service = this.configService;
final Configuration config = new Configuration();

try {
LOGGER.info("Loading config");
return this.configuration = service.load();
this.configService.load(config);
} catch (final NoSuchFileException e) {
LOGGER.warn("Config file not found: {}", e.getFile());
return this.configuration = service.create();
} catch (final FileNotFoundException e) {
LOGGER.warn("Config file not found");
return this.configuration = service.create();
} catch (final IOException e) {
LOGGER.error("Failed to load config", e);
return this.configuration = service.create();
} finally {
this.triggerOnChange();
this.triggerOnChange(config);
}

return this.configuration = config;
}

public void save() {
final Configuration config = this.configuration;
if (config != null) {
this.triggerOnChange();
this.triggerOnChange(config);

try {
this.configService.save(config);
Expand All @@ -55,16 +56,16 @@ public void save() {
}

public void reset() {
this.configuration = this.configService.create();
this.configuration = null;
}

public void onChange(final ChangeCallback callback) {
this.changeCallbacks.add(callback);
}

public void triggerOnChange() {
private void triggerOnChange(final Configuration config) {
for (final ChangeCallback callback : this.changeCallbacks) {
callback.onChange(this.configuration);
callback.onChange(config);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import java.io.IOException;

public interface ConfigurationService {
Configuration create();

Configuration load() throws IOException;
void load(Configuration configuration) throws IOException;

void save(Configuration config) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.vinrobot.mcemote.config.impl.file;
package net.vinrobot.mcemote.config.file;

import net.vinrobot.mcemote.config.Configuration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package net.vinrobot.mcemote.config.impl.file;
package net.vinrobot.mcemote.config.file;

import net.vinrobot.mcemote.config.Configuration;
import net.vinrobot.mcemote.config.ConfigurationService;
import net.vinrobot.mcemote.config.TypedGson;
import net.vinrobot.mcemote.config.impl.ConfigurationImpl;

import java.io.IOException;
import java.io.Reader;
Expand All @@ -20,22 +18,15 @@ public FileConfigurationService(final Path configFile) {
}

@Override
public Configuration create() {
return new ConfigurationImpl();
}

@Override
public Configuration load() throws IOException {
FileConfiguration fileConfiguration;
public void load(final Configuration configuration) throws IOException {
final FileConfiguration fileConfiguration;
try (final Reader reader = Files.newBufferedReader(this.configFile)) {
fileConfiguration = this.gson.fromJson(reader, FileConfiguration.class);
}

final Configuration config = this.create();
if (fileConfiguration != null) {
fileConfiguration.copyTo(config);
fileConfiguration.copyTo(configuration);
}
return config;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.vinrobot.mcemote.config;
package net.vinrobot.mcemote.config.file;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down

This file was deleted.

32 changes: 0 additions & 32 deletions src/main/java/net/vinrobot/mcemote/config/impl/OptionImpl.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package net.vinrobot.mcemote.config;

import net.vinrobot.mcemote.config.impl.OptionImpl;
package net.vinrobot.mcemote.config.options;

import java.util.Objects;
import java.util.Optional;

public interface Option<T> {
public class Option<T> {
static <T> Option<T> of(final T defaultValue) {
return new OptionImpl<>(defaultValue);
return new Option<>(defaultValue);
}

static <T> Option<T> of(final T defaultValue, Optional<T> value) {
final Option<T> option = of(defaultValue);
option.set(value);
return option;
}
private final T defaultValue;
private Optional<T> value = Optional.empty();

static <T> Option<T> of(final T defaultValue, T value) {
final Option<T> option = of(defaultValue);
option.set(value);
return option;
protected Option(final T defaultValue) {
this.defaultValue = defaultValue;
}

/**
Expand All @@ -28,15 +21,19 @@ static <T> Option<T> of(final T defaultValue, T value) {
* @param value The optional value to set.
* @return This option.
*/
Option<T> set(Optional<T> value);
public Option<T> set(final Optional<T> value) {
value.ifPresent(this::validate);
this.value = Objects.requireNonNull(value);
return this;
}

/**
* Set the value of this option.
*
* @param value The value to set. Must not be null.
* @return This option.
*/
default Option<T> set(T value) {
public Option<T> set(final T value) {
return this.set(Optional.of(value));
}

Expand All @@ -45,17 +42,16 @@ default Option<T> set(T value) {
*
* @return This option.
*/
default Option<T> reset() {
public Option<T> reset() {
return this.set(Optional.empty());
}

/**
* Get the value of this option.
* Returns the default value if the value is not set.
* Get the value of this option. Returns the default value if the value is not set.
*
* @return The value of this option.
*/
default T get() {
public T get() {
return this.getRaw().orElseGet(this::getDefault);
}

Expand All @@ -64,33 +60,37 @@ default T get() {
*
* @return The raw value of this option.
*/
Optional<T> getRaw();
public Optional<T> getRaw() {
return this.value;
}

/**
* Get the default value of this option.
*
* @return The default value of this option.
*/
T getDefault();
public T getDefault() {
return this.defaultValue;
}

/**
* Validate the value of this option.
*
* @param value The value to validate.
* @throws ValidationFailedException If the validation fails.
*/
default void validate(final T value) throws ValidationFailedException {
public void validate(final T value) throws ValidationFailedException {
Objects.requireNonNull(value);
}

/**
* Check if the value of this option is valid.
*
* @see #validate(T)
* @param value The value to check.
* @return True if the value is valid, false otherwise.
* @see #validate(T)
*/
default boolean isValid(final T value) {
public boolean isValid(final T value) {
try {
this.validate(value);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package net.vinrobot.mcemote.config.impl;
package net.vinrobot.mcemote.config.options;

import net.vinrobot.mcemote.config.ValidationFailedException;

public class TwitchIdOptionImpl extends OptionImpl<String> {
public TwitchIdOptionImpl(final String defaultValue) {
public class TwitchIdOption extends Option<String> {
public TwitchIdOption(final String defaultValue) {
super(defaultValue);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.vinrobot.mcemote.config;
package net.vinrobot.mcemote.config.options;

public class ValidationFailedException extends RuntimeException {
private static final long serialVersionUID = 1L;
Expand Down

0 comments on commit 5bed29b

Please sign in to comment.