From 261c5d83e62556cde6a5bacb7e4e7c1164d17aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Fri, 22 Nov 2024 17:46:30 +0000 Subject: [PATCH] simplify. (#20) * override. * simplify. * format --- .../frame/element/ElementItemBuilder.java | 2 + .../frame/element/ElementBuilderImpl.java | 57 +++++---- .../frame/element/ElementItemBuilderImpl.java | 121 +----------------- .../ElementPaginationBuilderImpl.java | 107 +--------------- 4 files changed, 36 insertions(+), 251 deletions(-) diff --git a/common/src/main/java/net/infumia/frame/element/ElementItemBuilder.java b/common/src/main/java/net/infumia/frame/element/ElementItemBuilder.java index 1001840..bdaacfd 100644 --- a/common/src/main/java/net/infumia/frame/element/ElementItemBuilder.java +++ b/common/src/main/java/net/infumia/frame/element/ElementItemBuilder.java @@ -52,9 +52,11 @@ ElementItemBuilder renderWith( ElementItemBuilder updateOnClick(); @NotNull + @Override ElementItemBuilder interactionDelay(@Nullable Duration interactionDelay); @NotNull + @Override ElementItemBuilder onInteractionDelay( @NotNull Consumer onInteractionDelay ); diff --git a/core/src/main/java/net/infumia/frame/element/ElementBuilderImpl.java b/core/src/main/java/net/infumia/frame/element/ElementBuilderImpl.java index 35a6da3..a25606a 100644 --- a/core/src/main/java/net/infumia/frame/element/ElementBuilderImpl.java +++ b/core/src/main/java/net/infumia/frame/element/ElementBuilderImpl.java @@ -14,7 +14,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class ElementBuilderImpl implements ElementBuilderRich { +public class ElementBuilderImpl> + implements ElementBuilderRich { Element root; boolean cancelOnClick; @@ -42,9 +43,9 @@ public ElementBuilderImpl() {} @NotNull @Override - public ElementBuilder root(@NotNull final Element root) { + public Self root(@NotNull final Element root) { this.root = root; - return this; + return this.self(); } @NotNull @@ -55,62 +56,62 @@ public Element build(@NotNull final ContextBase context) { @NotNull @Override - public ElementBuilder cancelOnClick() { + public Self cancelOnClick() { return this.cancelOnClick(true); } @NotNull @Override - public ElementBuilder closeOnClick() { + public Self closeOnClick() { return this.closeOnClick(true); } @NotNull @Override - public ElementBuilder updateOnClick() { + public Self updateOnClick() { return this.updateOnClick(true); } @NotNull @Override - public ElementBuilder cancelOnClick(final boolean cancelOnClick) { + public Self cancelOnClick(final boolean cancelOnClick) { this.cancelOnClick = cancelOnClick; - return this; + return this.self(); } @NotNull @Override - public ElementBuilder closeOnClick(final boolean cancelOnClick) { + public Self closeOnClick(final boolean cancelOnClick) { this.closeOnClick = cancelOnClick; - return this; + return this.self(); } @NotNull @Override - public ElementBuilder updateOnClick(final boolean updateOnClick) { + public Self updateOnClick(final boolean updateOnClick) { this.updateOnClick = updateOnClick; - return this; + return this.self(); } @NotNull @Override - public ElementBuilder interactionDelay(@Nullable final Duration interactionDelay) { + public Self interactionDelay(@Nullable final Duration interactionDelay) { this.interactionDelay = interactionDelay; - return this; + return this.self(); } @NotNull @Override - public ElementBuilder onInteractionDelay( + public Self onInteractionDelay( @NotNull final Consumer onInteractionDelay ) { this.onInteractionDelay = onInteractionDelay; - return this; + return this.self(); } @NotNull @Override - public ElementBuilder updateOnStateChange( + public Self updateOnStateChange( @NotNull final State state, @NotNull final State @NotNull... otherStates ) { @@ -119,12 +120,12 @@ public ElementBuilder updateOnStateChange( } this.updateOnStateChange.add(state); Collections.addAll(this.updateOnStateChange, otherStates); - return this; + return this.self(); } @NotNull @Override - public ElementBuilder updateOnStateAccess( + public Self updateOnStateAccess( @NotNull final State state, @NotNull final State @NotNull... otherStates ) { @@ -133,31 +134,37 @@ public ElementBuilder updateOnStateAccess( } this.updateOnStateAccess.add(state); Collections.addAll(this.updateOnStateAccess, otherStates); - return this; + return this.self(); } @NotNull @Override - public ElementBuilder displayIf(@NotNull final Predicate condition) { + public Self displayIf(@NotNull final Predicate condition) { this.displayIf = condition; - return this; + return this.self(); } @NotNull @Override - public ElementBuilder displayIf(@NotNull final BooleanSupplier condition) { + public Self displayIf(@NotNull final BooleanSupplier condition) { return this.displayIf(__ -> condition.getAsBoolean()); } @NotNull @Override - public ElementBuilder hideIf(@NotNull final Predicate condition) { + public Self hideIf(@NotNull final Predicate condition) { return this.displayIf(ctx -> condition.negate().test(ctx)); } @NotNull @Override - public ElementBuilder hideIf(@NotNull final BooleanSupplier condition) { + public Self hideIf(@NotNull final BooleanSupplier condition) { return this.hideIf(__ -> condition.getAsBoolean()); } + + @NotNull + @SuppressWarnings("unchecked") + private Self self() { + return (Self) this; + } } diff --git a/core/src/main/java/net/infumia/frame/element/ElementItemBuilderImpl.java b/core/src/main/java/net/infumia/frame/element/ElementItemBuilderImpl.java index 1a0eb3e..49900a9 100644 --- a/core/src/main/java/net/infumia/frame/element/ElementItemBuilderImpl.java +++ b/core/src/main/java/net/infumia/frame/element/ElementItemBuilderImpl.java @@ -1,23 +1,17 @@ package net.infumia.frame.element; -import java.time.Duration; -import java.util.function.BooleanSupplier; import java.util.function.Consumer; import java.util.function.Function; -import java.util.function.Predicate; import net.infumia.frame.context.ContextBase; -import net.infumia.frame.context.element.ContextElementClick; import net.infumia.frame.context.element.ContextElementItemClick; import net.infumia.frame.context.element.ContextElementItemRender; import net.infumia.frame.context.element.ContextElementItemUpdate; -import net.infumia.frame.context.element.ContextElementRender; -import net.infumia.frame.state.State; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public final class ElementItemBuilderImpl - extends ElementBuilderImpl + extends ElementBuilderImpl implements ElementItemBuilderRich { ItemStack item; @@ -95,122 +89,9 @@ public ElementItemBuilder onUpdate( return this; } - @NotNull - @Override - public ElementItemBuilder root(@NotNull final Element root) { - super.root(root); - return this; - } - @NotNull @Override public ElementItem build(@NotNull final ContextBase parent) { return new ElementItemImpl(this, parent); } - - @NotNull - @Override - public ElementItemBuilder cancelOnClick() { - super.cancelOnClick(); - return this; - } - - @NotNull - @Override - public ElementItemBuilder closeOnClick() { - super.closeOnClick(); - return this; - } - - @NotNull - @Override - public ElementItemBuilder updateOnClick() { - super.updateOnClick(); - return this; - } - - @NotNull - @Override - public ElementItemBuilder onInteractionDelay( - final @NotNull Consumer onInteractionDelay - ) { - super.onInteractionDelay(onInteractionDelay); - return this; - } - - @NotNull - @Override - public ElementItemBuilder interactionDelay(final @Nullable Duration interactionDelay) { - super.interactionDelay(interactionDelay); - return this; - } - - @NotNull - @Override - public ElementItemBuilder cancelOnClick(final boolean cancelOnClick) { - super.cancelOnClick(cancelOnClick); - return this; - } - - @NotNull - @Override - public ElementItemBuilder closeOnClick(final boolean cancelOnClick) { - super.closeOnClick(cancelOnClick); - return this; - } - - @NotNull - @Override - public ElementItemBuilder updateOnClick(final boolean updateOnClick) { - super.updateOnClick(updateOnClick); - return this; - } - - @NotNull - @Override - public ElementItemBuilder updateOnStateChange( - @NotNull final State state, - @NotNull final State @NotNull... otherStates - ) { - super.updateOnStateChange(state, otherStates); - return this; - } - - @NotNull - @Override - public ElementItemBuilder updateOnStateAccess( - @NotNull final State state, - @NotNull final State @NotNull... otherStates - ) { - super.updateOnStateAccess(state, otherStates); - return this; - } - - @NotNull - @Override - public ElementItemBuilder displayIf(@NotNull final Predicate condition) { - super.displayIf(condition); - return this; - } - - @NotNull - @Override - public ElementItemBuilder displayIf(@NotNull final BooleanSupplier condition) { - super.displayIf(condition); - return this; - } - - @NotNull - @Override - public ElementItemBuilder hideIf(@NotNull final Predicate condition) { - super.hideIf(condition); - return this; - } - - @NotNull - @Override - public ElementItemBuilder hideIf(@NotNull final BooleanSupplier condition) { - super.hideIf(condition); - return this; - } } diff --git a/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationBuilderImpl.java b/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationBuilderImpl.java index 8ac5c30..3d01266 100644 --- a/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationBuilderImpl.java +++ b/core/src/main/java/net/infumia/frame/element/pagination/ElementPaginationBuilderImpl.java @@ -1,12 +1,8 @@ package net.infumia.frame.element.pagination; import java.util.function.BiConsumer; -import java.util.function.BooleanSupplier; import java.util.function.Function; -import java.util.function.Predicate; import net.infumia.frame.context.ContextBase; -import net.infumia.frame.context.element.ContextElementRender; -import net.infumia.frame.element.Element; import net.infumia.frame.element.ElementBuilderImpl; import net.infumia.frame.element.ElementItemBuilder; import net.infumia.frame.state.State; @@ -15,7 +11,7 @@ import org.jetbrains.annotations.NotNull; public final class ElementPaginationBuilderImpl - extends ElementBuilderImpl + extends ElementBuilderImpl> implements ElementPaginationBuilderRich { final SourceProvider sourceProvider; @@ -90,110 +86,9 @@ public StatePagination buildPagination() { return this.stateFactory.apply(this); } - @NotNull - @Override - public ElementPaginationBuilder root(@NotNull final Element root) { - super.root(root); - return this; - } - @NotNull @Override public ElementPagination build(@NotNull final ContextBase parent) { return new ElementPaginationImpl<>(this, parent); } - - @NotNull - @Override - public ElementPaginationBuilder cancelOnClick() { - super.cancelOnClick(); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder closeOnClick() { - super.closeOnClick(); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder updateOnClick() { - super.updateOnClick(); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder cancelOnClick(final boolean cancelOnClick) { - super.cancelOnClick(cancelOnClick); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder closeOnClick(final boolean cancelOnClick) { - super.closeOnClick(cancelOnClick); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder updateOnClick(final boolean updateOnClick) { - super.updateOnClick(updateOnClick); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder updateOnStateChange( - @NotNull final State state, - @NotNull final State @NotNull... otherStates - ) { - super.updateOnStateChange(state, otherStates); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder updateOnStateAccess( - @NotNull final State state, - @NotNull final State @NotNull... otherStates - ) { - super.updateOnStateAccess(state, otherStates); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder displayIf( - @NotNull final Predicate condition - ) { - super.displayIf(condition); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder displayIf(@NotNull final BooleanSupplier condition) { - super.displayIf(condition); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder hideIf( - @NotNull final Predicate condition - ) { - super.hideIf(condition); - return this; - } - - @NotNull - @Override - public ElementPaginationBuilder hideIf(@NotNull final BooleanSupplier condition) { - super.hideIf(condition); - return this; - } }