mirror of
https://github.com/SimplexDevelopment/SimplexCore.git
synced 2024-12-22 08:37:37 +00:00
Cleanup
- Removed Wrapper.java (unnecessary wrapper class which did unnecessary things)
- Removed Validate.java (BooleanSupplier)
- Changed how CommandLoader.Registry works, now uses ReflectionTools instead.
- Renamed Action to ClickAction to be more specific with what it does
- Added JavaDocs to ClickAction and IGUI, more to follow soon
💯
This commit is contained in:
parent
5490213609
commit
628b87bfc1
@ -1,6 +1,6 @@
|
||||
package io.github.simplexdev.api;
|
||||
|
||||
import io.github.simplexdev.api.func.Action;
|
||||
import io.github.simplexdev.api.func.ClickAction;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
@ -11,24 +11,79 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* An interface which supplies customizable inventory GUI instances.
|
||||
*/
|
||||
public interface IGUI {
|
||||
/**
|
||||
* @return UUID of the inventory provided by the interface instance.
|
||||
*/
|
||||
UUID getInvUUId();
|
||||
|
||||
/**
|
||||
* The actual inventory provided by the interface instance.
|
||||
* @return the inventory which is provided by the interface instance.
|
||||
*/
|
||||
Inventory getInventory();
|
||||
|
||||
void setItem(int slot, @NotNull ItemStack stack, @Nullable Action action);
|
||||
/**
|
||||
* Sets the item for the defined slot to the specified item stack.
|
||||
* You may define an action to run when the player clicks on the inventory slot,
|
||||
* however it is not required; you may also pass null to the clickAction parameter.
|
||||
* @param slot The inventory slot to set; remember that arrays start at 0, so
|
||||
* an inventory size of 9 will range from 0 to 8.
|
||||
* @param stack The item to display in the slot. This can be Material.AIR.
|
||||
* @param clickAction The action to perform when a player clicks the inventory slot.
|
||||
* This may be null.
|
||||
*/
|
||||
void setItem(int slot, @NotNull ItemStack stack, @Nullable ClickAction clickAction);
|
||||
|
||||
/**
|
||||
* Sets the item for the defined slot to the specified item stack.
|
||||
* This method should automatically define the slot action as null.
|
||||
* @param slot The inventory slot to set; remember that arrays start at 0, so
|
||||
* an inventory size of 9 will range from 0 to 8.
|
||||
* @param stack The item to display in the slot. This can be Material.AIR.
|
||||
*/
|
||||
void setItem(int slot, @NotNull ItemStack stack);
|
||||
|
||||
/**
|
||||
* Force a player to open the inventory provided by the interface instance.
|
||||
* @param player The player to open the inventory on.
|
||||
*/
|
||||
void open(@NotNull Player player);
|
||||
|
||||
/**
|
||||
* Force a player to close the inventory provided by the interface instance.
|
||||
* @param player The player to close the inventory on.
|
||||
*/
|
||||
void close(@NotNull Player player);
|
||||
|
||||
/**
|
||||
* Delete the inventory provided by the interface instance.
|
||||
*/
|
||||
void delete();
|
||||
|
||||
Map<Integer, Action> getActions();
|
||||
/**
|
||||
* @return A map containing each inventory slot and their respective actions,
|
||||
* but only if the slot actually has an action.
|
||||
*/
|
||||
Map<Integer, ClickAction> getActions();
|
||||
|
||||
/**
|
||||
* Creates a new ItemStack instance to place in the inventory provided by the interface instance.
|
||||
* @param material The item material
|
||||
* @param name The name of the item
|
||||
* @param lore Optional item descriptions
|
||||
* @return The newly created item
|
||||
*/
|
||||
ItemStack newItem(@NotNull Material material, @NotNull String name, String... lore);
|
||||
|
||||
/**
|
||||
* Creates a new ItemStack instance to place in the inventory provided by the interface instance.
|
||||
* @param material The item material
|
||||
* @param name The name of the item
|
||||
* @return The newly created item with no lore.
|
||||
*/
|
||||
ItemStack newItem(@NotNull Material material, @NotNull String name);
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
package io.github.simplexdev.api.func;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Action {
|
||||
void onClick(Player player);
|
||||
}
|
17
src/main/java/io/github/simplexdev/api/func/ClickAction.java
Normal file
17
src/main/java/io/github/simplexdev/api/func/ClickAction.java
Normal file
@ -0,0 +1,17 @@
|
||||
package io.github.simplexdev.api.func;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* Functional interface which provides an action to execute
|
||||
* when a player clicks on an {@link io.github.simplexdev.api.IGUI} provided inventory slot.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ClickAction {
|
||||
/**
|
||||
* Returns a void when a player clicks on an
|
||||
* inventory slot provided by the GUI class for the GUIListener.
|
||||
* @param player The player who interacted with the inventory slot.
|
||||
*/
|
||||
void onClick(Player player);
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package io.github.simplexdev.api.func;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Validate {
|
||||
boolean isValid();
|
||||
}
|
@ -1,38 +1,37 @@
|
||||
package io.github.simplexdev.simplexcore.chat;
|
||||
|
||||
import io.github.simplexdev.simplexcore.utils.Wrapper;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public final class ChatUtils {
|
||||
protected final Wrapper<? extends CommandSender> target;
|
||||
protected final CommandSender target;
|
||||
protected final TextComponentFactory factory = new TextComponentFactory();
|
||||
|
||||
private ChatUtils(Wrapper<? extends CommandSender> target) {
|
||||
private <T extends CommandSender> ChatUtils(T target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public static <T extends CommandSender> ChatUtils target(T target) {
|
||||
return new ChatUtils(Wrapper.of(target));
|
||||
return new ChatUtils(target);
|
||||
}
|
||||
|
||||
public void msg(String message) {
|
||||
target.get().sendMessage(message);
|
||||
target.sendMessage(message);
|
||||
}
|
||||
|
||||
public void msg(TextComponent component) {
|
||||
target.get().sendMessage(component);
|
||||
target.sendMessage(component);
|
||||
}
|
||||
|
||||
public void err(Messages message) {
|
||||
target.get().sendMessage(message.getMessage());
|
||||
target.sendMessage(message.getMessage());
|
||||
}
|
||||
|
||||
public void color(String message) {
|
||||
target.get().sendMessage(factory.colorize(message));
|
||||
target.sendMessage(factory.colorize(message));
|
||||
}
|
||||
|
||||
public void color(TextComponent component) {
|
||||
target.get().sendMessage(factory.colorize(component.getText()));
|
||||
target.sendMessage(factory.colorize(component.getText()));
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package io.github.simplexdev.simplexcore.command;
|
||||
import io.github.simplexdev.api.annotations.CommandInfo;
|
||||
import io.github.simplexdev.simplexcore.command.defaults.DefaultCommand;
|
||||
import io.github.simplexdev.simplexcore.utils.Constants;
|
||||
import io.github.simplexdev.simplexcore.utils.ReflectionTools;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.*;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@ -13,7 +14,6 @@ import org.reflections.Reflections;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.MissingResourceException;
|
||||
@ -52,7 +52,7 @@ public final class CommandLoader {
|
||||
PluginCommand objectToRegister = Registry.create(Constants.getPlugin(), info.name().toLowerCase());
|
||||
objectToRegister.setAliases(Arrays.asList(info.aliases().split(",")));
|
||||
objectToRegister.setDescription(info.description());
|
||||
objectToRegister.setExecutor(getFromSetName(info.name()));
|
||||
objectToRegister.setExecutor(getExecutorFromName(info.name()));
|
||||
objectToRegister.setLabel(info.name().toLowerCase());
|
||||
objectToRegister.setPermission(info.permission());
|
||||
objectToRegister.setPermissionMessage(info.permissionMessage());
|
||||
@ -62,7 +62,7 @@ public final class CommandLoader {
|
||||
});
|
||||
}
|
||||
|
||||
public synchronized CommandExecutor getFromSetName(String name) {
|
||||
public synchronized CommandExecutor getExecutorFromName(String name) {
|
||||
for (Class<? extends CommandExecutor> obj : reflections.getSubTypesOf(CommandExecutor.class)) {
|
||||
if (!obj.isAnnotationPresent(CommandInfo.class)) {
|
||||
throw new RuntimeException("Missing annotation CommandInfo!");
|
||||
@ -73,8 +73,9 @@ public final class CommandLoader {
|
||||
if (name.equalsIgnoreCase(info.name())) {
|
||||
try {
|
||||
Constructor<? extends CommandExecutor> constr = obj.getDeclaredConstructor();
|
||||
constr.setAccessible(true);
|
||||
return constr.newInstance();
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
|
||||
} catch (ReflectiveOperationException ignored) {
|
||||
return new DefaultCommand();
|
||||
}
|
||||
}
|
||||
@ -93,8 +94,9 @@ public final class CommandLoader {
|
||||
if (name.equalsIgnoreCase(info.name())) {
|
||||
try {
|
||||
Constructor<? extends TabCompleter> constr = obj.getDeclaredConstructor();
|
||||
constr.setAccessible(true);
|
||||
return constr.newInstance();
|
||||
} catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
|
||||
} catch (ReflectiveOperationException ignored) {
|
||||
return new DefaultCommand();
|
||||
}
|
||||
}
|
||||
@ -108,40 +110,13 @@ public final class CommandLoader {
|
||||
private static final Field knownCmdsField;
|
||||
|
||||
static {
|
||||
Constructor<PluginCommand> temp;
|
||||
try {
|
||||
temp = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
|
||||
temp.setAccessible(true);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
constructor = temp;
|
||||
|
||||
Field cmf;
|
||||
try {
|
||||
cmf = SimplePluginManager.class.getDeclaredField("commandMap");
|
||||
cmf.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
cmdMapField = cmf;
|
||||
|
||||
Field kcf;
|
||||
try {
|
||||
kcf = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||
kcf.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
knownCmdsField = kcf;
|
||||
constructor = ReflectionTools.getDeclaredConstructor(PluginCommand.class, String.class, Plugin.class);
|
||||
cmdMapField = ReflectionTools.getDeclaredField(SimplePluginManager.class, "commandMap");
|
||||
knownCmdsField = ReflectionTools.getDeclaredField(SimpleCommandMap.class, "knownCommands");
|
||||
}
|
||||
|
||||
public static PluginCommand create(@NotNull Plugin plugin, @NotNull String name) {
|
||||
try {
|
||||
return constructor.newInstance(name, plugin);
|
||||
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return ReflectionTools.initConstructor(constructor, name, plugin);
|
||||
}
|
||||
|
||||
public static void registerCommand(PluginCommand command) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package io.github.simplexdev.simplexcore.gui;
|
||||
|
||||
import io.github.simplexdev.api.func.Action;
|
||||
import io.github.simplexdev.api.func.ClickAction;
|
||||
import io.github.simplexdev.api.IGUI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
@ -16,7 +16,7 @@ import java.util.*;
|
||||
|
||||
public abstract class AbstractGUI implements InventoryHolder, IGUI {
|
||||
private final Inventory INV;
|
||||
private final Map<Integer, Action> actions;
|
||||
private final Map<Integer, ClickAction> actions;
|
||||
private final UUID uuid;
|
||||
|
||||
private final List<Integer> validSize = new ArrayList<>(){{
|
||||
@ -53,10 +53,10 @@ public abstract class AbstractGUI implements InventoryHolder, IGUI {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setItem(int slot, @NotNull ItemStack stack, @Nullable Action action) {
|
||||
public void setItem(int slot, @NotNull ItemStack stack, @Nullable ClickAction clickAction) {
|
||||
INV.setItem(slot, stack);
|
||||
if (action != null) {
|
||||
actions.put(slot, action);
|
||||
if (clickAction != null) {
|
||||
actions.put(slot, clickAction);
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,7 +97,7 @@ public abstract class AbstractGUI implements InventoryHolder, IGUI {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Integer, Action> getActions() {
|
||||
public Map<Integer, ClickAction> getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
package io.github.simplexdev.simplexcore.gui;
|
||||
|
||||
import io.github.simplexdev.api.func.Action;
|
||||
import io.github.simplexdev.api.func.ClickAction;
|
||||
import io.github.simplexdev.api.IGUI;
|
||||
import io.github.simplexdev.simplexcore.listener.SimplexListener;
|
||||
import io.github.simplexdev.simplexcore.plugin.SimplexAddon;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
@ -11,7 +13,11 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public final class GUIHandler implements Listener {
|
||||
public final class GUIHandler extends SimplexListener {
|
||||
public GUIHandler(SimplexAddon<?> plugin) {
|
||||
register(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void invClick(InventoryClickEvent event) {
|
||||
if (!(event.getWhoClicked() instanceof Player)) {
|
||||
@ -25,9 +31,9 @@ public final class GUIHandler implements Listener {
|
||||
if (invUUID != null) {
|
||||
event.setCancelled(true);
|
||||
IGUI gui = AbstractGUI.getInvByUUId().get(invUUID);
|
||||
Action action = gui.getActions().get(event.getSlot());
|
||||
if (action != null) {
|
||||
action.onClick(player);
|
||||
ClickAction clickAction = gui.getActions().get(event.getSlot());
|
||||
if (clickAction != null) {
|
||||
clickAction.onClick(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package io.github.simplexdev.simplexcore.listener;
|
||||
|
||||
import io.github.simplexdev.api.func.Validate;
|
||||
import io.github.simplexdev.simplexcore.utils.Constants;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
@ -8,6 +7,7 @@ import org.bukkit.event.server.PluginEnableEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
public final class DependencyListener extends SimplexListener {
|
||||
public List<String> PAPI_NAMES = new ArrayList<>() {{
|
||||
@ -30,14 +30,14 @@ public final class DependencyListener extends SimplexListener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void pluginRegister(PluginEnableEvent event) {
|
||||
Validate temp = () -> PLIB_NAMES.contains(event.getPlugin().getName());
|
||||
Validate temp2 = () -> PAPI_NAMES.contains(event.getPlugin().getName());
|
||||
BooleanSupplier temp = () -> PLIB_NAMES.contains(event.getPlugin().getName());
|
||||
BooleanSupplier temp2 = () -> PAPI_NAMES.contains(event.getPlugin().getName());
|
||||
|
||||
if (temp.isValid()) {
|
||||
if (temp.getAsBoolean()) {
|
||||
Constants.getPlugin().getInstances().getDependencyManager().registerProtocolLib();
|
||||
}
|
||||
|
||||
if (temp2.isValid()) {
|
||||
if (temp2.getAsBoolean()) {
|
||||
Constants.getPlugin().getInstances().getDependencyManager().registerPAPI();
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +70,10 @@ public class SignFactory {
|
||||
@Nullable
|
||||
public IUsableSign addTagsToSign(Sign sign, Object... tags) {
|
||||
if (getSignData() != null) {
|
||||
if (!SignData.getUnlabeledSigns().containsKey(sign)) {
|
||||
createNoTag(sign);
|
||||
}
|
||||
|
||||
AbstractSign abs = SignData.getUnlabeledSigns().get(sign);
|
||||
for (Object tag : tags) {
|
||||
if (tag instanceof VoidSupplier) {
|
||||
@ -126,10 +130,10 @@ public class SignFactory {
|
||||
&& SIGN_TYPES.contains(event.getClickedBlock().getType())) {
|
||||
Sign sign = (Sign) event.getClickedBlock();
|
||||
if (signMap.containsKey(sign)) {
|
||||
IUsableSign isign = signMap.get(sign);
|
||||
String tag = isign.signTag();
|
||||
if (isign.getLines().get(0).equals(tag)) {
|
||||
isign.execute();
|
||||
IUsableSign iSign = signMap.get(sign);
|
||||
String tag = iSign.signTag();
|
||||
if (iSign.getLines().get(0).equals(tag)) {
|
||||
iSign.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,68 +1,95 @@
|
||||
package io.github.simplexdev.simplexcore.utils;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AccessibleObject;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public final class ReflectionTools {
|
||||
public ReflectionTools() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Reflections reflect(Class<?> loadFrom) {
|
||||
public static Reflections reflect(Class<?> loadFrom) {
|
||||
return new Reflections(loadFrom.getName());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Set<Class<?>> getAnnotatedClasses(Class<?> loadFrom, Class<? extends Annotation> annotation) {
|
||||
public static Set<Class<?>> getAnnotatedClasses(Class<?> loadFrom, Class<? extends Annotation> annotation) {
|
||||
return new Reflections(loadFrom.getName()).getTypesAnnotatedWith(annotation);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Field getField(Class<?> cls, String name) {
|
||||
public static <T> Field getField(Class<T> cls, String name) {
|
||||
try {
|
||||
Field field = cls.getField(name);
|
||||
field.setAccessible(true);
|
||||
return field;
|
||||
return asAccessible(cls.getField(name));
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Constructor<?> getConstructor(Class<?> cls, String name, Class<?>... initializers) {
|
||||
public static <T> Field getDeclaredField(Class<T> cls, String name) {
|
||||
try {
|
||||
return cls.getDeclaredConstructor(initializers);
|
||||
return asAccessible(cls.getDeclaredField(name));
|
||||
} catch (ReflectiveOperationException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T> Constructor<T> getConstructor(Class<T> cls, Class<?>... initializers) {
|
||||
try {
|
||||
return asAccessible(cls.getConstructor(initializers));
|
||||
} catch (NoSuchMethodException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Wrapper<?> initConstructor(Constructor<?> constructor, Object... initializers) {
|
||||
public static <T> Constructor<T> getDeclaredConstructor(Class<T> cls, Class<?>... initializers) {
|
||||
try {
|
||||
return Wrapper.of(constructor.newInstance(initializers));
|
||||
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
|
||||
return asAccessible(cls.getDeclaredConstructor(initializers));
|
||||
} catch (NoSuchMethodException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Method getMethod(Class<?> clazz, String name, Class<?>... params) {
|
||||
public static <T> T initConstructor(Constructor<? extends T> constructor, Object... initializers) {
|
||||
try {
|
||||
return Wrapper.of(clazz.getMethod(name, params))
|
||||
.perform(obj -> obj.setAccessible(true))
|
||||
.get();
|
||||
return constructor.newInstance(initializers);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T> Method getMethod(Class<T> clazz, String name, Class<?>... params) {
|
||||
try {
|
||||
return asAccessible(clazz.getMethod(name, params));
|
||||
} catch (NoSuchMethodException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T> Method getDeclaredMethod(Class<T> clazz, String name, Class<?>... params) {
|
||||
try {
|
||||
return asAccessible(clazz.getDeclaredMethod(name, params));
|
||||
} catch (ReflectiveOperationException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
public static <T extends AccessibleObject> T asAccessible(T object) {
|
||||
object.setAccessible(true);
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
@ -1,78 +0,0 @@
|
||||
package io.github.simplexdev.simplexcore.utils;
|
||||
|
||||
import io.github.simplexdev.api.func.VoidSupplier;
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public final class Wrapper<T> {
|
||||
protected T bean;
|
||||
|
||||
private Wrapper(T bean) {
|
||||
this.bean = bean;
|
||||
}
|
||||
|
||||
public void set(T bean) {
|
||||
this.bean = bean;
|
||||
}
|
||||
|
||||
public T get() {
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return new HashCodeBuilder().append(get()).toHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof Wrapper<?>)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return new EqualsBuilder().append(((Wrapper<?>) obj).get(), get()).isEquals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return get().toString();
|
||||
}
|
||||
|
||||
public static <T> Wrapper<T> of(T object) {
|
||||
return new Wrapper<>(object);
|
||||
}
|
||||
|
||||
public final <P> P as(Function<? super Wrapper<T>, P> function) {
|
||||
return function.apply(this);
|
||||
}
|
||||
|
||||
public final <R> Wrapper<R> flatMap(Function<? super T, ? extends Wrapper<? extends R>> function) {
|
||||
return new Wrapper<>(function.apply(get()).get());
|
||||
}
|
||||
|
||||
public final <R> Wrapper<R> map(Function<? super T, ? extends R> function) {
|
||||
return new Wrapper<>(function.apply(get()));
|
||||
}
|
||||
|
||||
public final Wrapper<T> perform(Consumer<? super T> consumer) {
|
||||
consumer.accept(get());
|
||||
return this;
|
||||
}
|
||||
|
||||
public final Wrapper<T> then(VoidSupplier supplier) {
|
||||
supplier.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
public final Wrapper<T> filter(Predicate<? super T> predicate) {
|
||||
return predicate.test(get()) ? this : null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user