Skip to content

Commit

Permalink
api: a few minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Nov 23, 2020
1 parent 3c24c3b commit cdd2fe6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.Consumer;
import java.util.stream.Stream;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.util.Buildable;
import net.kyori.examination.ExaminableProperty;
import net.kyori.examination.string.StringExaminer;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down Expand Up @@ -95,9 +96,7 @@ protected AbstractComponent(final @NonNull List<? extends ComponentLike> childre
@Override
public @NonNull Component replaceText(final @NonNull Consumer<TextReplacementConfig.Builder> configurer) {
requireNonNull(configurer, "configurer");
final TextReplacementConfigImpl.Builder builder = new TextReplacementConfigImpl.Builder();
configurer.accept(builder);
return TextReplacementRenderer.INSTANCE.render(this, builder.toState());
return this.replaceText(Buildable.configureAndBuild(TextReplacementConfig.builder(), configurer));
}

@Override
Expand All @@ -106,7 +105,7 @@ protected AbstractComponent(final @NonNull List<? extends ComponentLike> childre
if(!(config instanceof TextReplacementConfigImpl)) {
throw new IllegalArgumentException("Provided replacement was a custom TextReplacementConfig implementation, which is not supported.");
}
return TextReplacementRenderer.INSTANCE.render(this, ((TextReplacementConfigImpl) config).toState());
return TextReplacementRenderer.INSTANCE.render(this, ((TextReplacementConfigImpl) config).createState());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ default Builder matchLiteral(final String literal) {
* @return this builder
* @since 4.2.0
*/
default @NonNull Builder times(int times) {
default @NonNull Builder times(final int times) {
return this.condition((index, replaced) -> replaced < times ? PatternReplacementResult.REPLACE : PatternReplacementResult.STOP);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class TextReplacementConfigImpl implements TextReplacementConfig {
return this.matchPattern;
}

TextReplacementRenderer.State toState() {
TextReplacementRenderer.State createState() {
return new TextReplacementRenderer.State(this.matchPattern, this.replacement, this.continuer);
}

Expand All @@ -77,7 +77,7 @@ public String toString() {

static final class Builder implements TextReplacementConfig.Builder {
@MonotonicNonNull Pattern matchPattern;
BiFunction<MatchResult, TextComponent.Builder, @Nullable ComponentLike> replacement;
@MonotonicNonNull BiFunction<MatchResult, TextComponent.Builder, @Nullable ComponentLike> replacement;
IntFunction2<PatternReplacementResult> continuer = (index, replacement) -> PatternReplacementResult.REPLACE;

Builder() {
Expand Down Expand Up @@ -107,24 +107,10 @@ static final class Builder implements TextReplacementConfig.Builder {
return this;
}

private void validate() {
if(this.matchPattern == null) {
throw new IllegalStateException("A pattern must be provided to match against");
}
if(this.replacement == null) {
throw new IllegalStateException("A replacement action must be provided");
}
}

TextReplacementRenderer.State toState() {
this.validate();
return new TextReplacementRenderer.State(this.matchPattern, this.replacement, this.continuer);
}

@Override
public TextReplacementConfig build() {
this.validate();

if(this.matchPattern == null) throw new IllegalStateException("A pattern must be provided to match against");
if(this.replacement == null) throw new IllegalStateException("A replacement action must be provided");
return new TextReplacementConfigImpl(this);
}
}
Expand Down

0 comments on commit cdd2fe6

Please sign in to comment.