Skip to content

Commit

Permalink
More option add methods so there is less reason to break the constant…
Browse files Browse the repository at this point in the history
… builder chain
  • Loading branch information
isXander committed Oct 22, 2023
1 parent 0e09749 commit ec17d3c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 5 deletions.
48 changes: 46 additions & 2 deletions common/src/main/java/dev/isxander/yacl3/api/ConfigCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.function.Supplier;

/**
* Separates {@link Option}s or {@link OptionGroup}s into multiple distinct sections.
Expand Down Expand Up @@ -47,18 +48,61 @@ interface Builder extends OptionAddable {
/**
* Adds an option to the root group of the category.
* To add to another group, use {@link Builder#group(OptionGroup)}.
* To construct an option, use {@link Option#createBuilder(Class)}
* To construct an option, use {@link Option#createBuilder()}
*
* @see ConfigCategory#groups()
* @see OptionGroup#isRoot()
*/
@Override
Builder option(@NotNull Option<?> option);

/**
* Adds an option to the root group of the category.
* To add to another group, use {@link Builder#group(OptionGroup)}.
* To construct an option, use {@link Option#createBuilder()}
*
* @param optionSupplier to be called to initialise the option. called immediately
* @return this
*/
@Override
default Builder option(@NotNull Supplier<@NotNull Option<?>> optionSupplier) {
OptionAddable.super.option(optionSupplier);
return this;
}

/**
* Adds an option to the root group of the category if a condition is met.
* To add to another group, use {@link Builder#group(OptionGroup)}.
* To construct an option, use {@link Option#createBuilder()}
*
* @param condition only if true is the option added
* @return this
*/
@Override
default Builder optionIf(boolean condition, @NotNull Option<?> option) {
OptionAddable.super.optionIf(condition, option);
return this;
}

/**
* Adds an option to the root group of the category if a condition is met.
* To add to another group, use {@link Builder#group(OptionGroup)}.
* To construct an option, use {@link Option#createBuilder()}
*
* @param condition only if true is the option added
* @param optionSupplier to be called to initialise the option. called immediately only if condition is true
* @return this
*/
@Override
default Builder optionIf(boolean condition, @NotNull Supplier<@NotNull Option<?>> optionSupplier) {
OptionAddable.super.optionIf(condition, optionSupplier);
return this;
}

/**
* Adds multiple options to the root group of the category.
* To add to another group, use {@link Builder#groups(Collection)}.
* To construct an option, use {@link Option#createBuilder(Class)}
* To construct an option, use {@link Option#createBuilder()}
*
* @see ConfigCategory#groups()
* @see OptionGroup#isRoot()
Expand Down
36 changes: 34 additions & 2 deletions common/src/main/java/dev/isxander/yacl3/api/OptionAddable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,49 @@
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.function.Supplier;

public interface OptionAddable {
/**
* Adds an option to an abstract builder.
* To construct an option, use {@link Option#createBuilder(Class)}
* To construct an option, use {@link Option#createBuilder()}
*/
OptionAddable option(@NotNull Option<?> option);

/**
* Adds an option to an abstract builder.
* To construct an option, use {@link Option#createBuilder()}
* @param optionSupplier to be called to initialise the option. called immediately
*/
default OptionAddable option(@NotNull Supplier<@NotNull Option<?>> optionSupplier) {
return option(optionSupplier.get());
}

/**
* Adds an option to an abstract builder if a condition is met.
* To construct an option, use {@link Option#createBuilder()}
* @param condition only if true is the option added
* @param option the option to add
* @return this
*/
default OptionAddable optionIf(boolean condition, @NotNull Option<?> option) {
return condition ? option(option) : this;
}

/**
* Adds an option to an abstract builder if a condition is met.
* To construct an option, use {@link Option#createBuilder()}
* @param condition only if true is the option added
* @param optionSupplier to be called to initialise the option. called immediately only if condition is true
* @return this
*/
default OptionAddable optionIf(boolean condition, @NotNull Supplier<@NotNull Option<?>> optionSupplier) {
return condition ? option(optionSupplier) : this;
}

/**
* Adds multiple options to an abstract builder.
* To construct an option, use {@link Option#createBuilder(Class)}
* To construct an option, use {@link Option#createBuilder()}
*/
OptionAddable options(@NotNull Collection<? extends Option<?>> options);
}
43 changes: 42 additions & 1 deletion common/src/main/java/dev/isxander/yacl3/api/OptionGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.function.Supplier;

/**
* Serves as a separator between multiple chunks of options
Expand Down Expand Up @@ -69,9 +70,49 @@ interface Builder extends OptionAddable {
@Override
Builder option(@NotNull Option<?> option);

/**
* Adds an option to this group.
* To construct an option, use {@link Option#createBuilder()}
*
* @param optionSupplier to be called to initialise the option. called immediately
* @return this
*/
@Override
default Builder option(@NotNull Supplier<@NotNull Option<?>> optionSupplier) {
OptionAddable.super.option(optionSupplier);
return this;
}

/**
* Adds an option to this group if a condition is met.
* To construct an option, use {@link Option#createBuilder()}
*
* @param condition only if true is the option added
* @return this
*/
@Override
default Builder optionIf(boolean condition, @NotNull Option<?> option) {
OptionAddable.super.optionIf(condition, option);
return this;
}

/**
* Adds an option to this group if a condition is met.
* To construct an option, use {@link Option#createBuilder()}
*
* @param condition only if true is the option added
* @param optionSupplier to be called to initialise the option. called immediately only if condition is true
* @return this
*/
@Override
default Builder optionIf(boolean condition, @NotNull Supplier<@NotNull Option<?>> optionSupplier) {
OptionAddable.super.optionIf(condition, optionSupplier);
return this;
}

/**
* Adds multiple options to group.
* To construct an option, use {@link Option#createBuilder(Class)}
* To construct an option, use {@link Option#createBuilder()}
*
* @see OptionGroup#options()
*/
Expand Down

0 comments on commit ec17d3c

Please sign in to comment.