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 9909cc1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/vinrobot/mcemote/config/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.vinrobot.mcemote.config;

import net.vinrobot.mcemote.config.options.Option;

public interface Configuration {
Option<String> twitchId();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.vinrobot.mcemote.config.impl;

import net.vinrobot.mcemote.config.Configuration;
import net.vinrobot.mcemote.config.Option;
import net.vinrobot.mcemote.config.options.Option;
import net.vinrobot.mcemote.config.options.TwitchIdOption;

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

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

@Override
public Option<String> twitchId() {
Expand Down
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 9909cc1

Please sign in to comment.