Use modifiable and nullable collections where possible

This commit is contained in:
filoghost 2020-06-14 12:53:25 +02:00
parent e9bbbbdf89
commit c31216f569
6 changed files with 37 additions and 32 deletions

View File

@ -69,8 +69,6 @@ public interface ConfigurableIcon extends Icon {
void removeEnchantment(Enchantment ench);
void clearEnchantments();
Color getLeatherColor();
void setLeatherColor(Color leatherColor);

View File

@ -41,6 +41,7 @@ import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.api.ClickHandler;
import me.filoghost.chestcommands.api.ClickResult;
import me.filoghost.chestcommands.api.ConfigurableIcon;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.variable.VariableManager;
public class ConfigurableIconImpl implements ConfigurableIcon {
@ -67,7 +68,6 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
private ItemStack cachedItem; // When there are no variables, we don't recreate the item
public ConfigurableIconImpl() {
enchantments = new HashMap<>();
closeOnClick = true;
amount = 1;
}
@ -142,7 +142,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override
public void setLore(List<String> lore) {
this.lore = lore;
this.lore = Utils.nullableCopy(lore);
this.loreLinesWithVariables = null;
if (lore != null) {
@ -159,7 +159,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override
public boolean hasLore() {
return lore != null && lore.size() > 0;
return !Utils.isNullOrEmpty(lore);
}
@Override
@ -169,16 +169,12 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override
public void setEnchantments(Map<Enchantment, Integer> enchantments) {
if (enchantments == null) {
this.enchantments.clear();
return;
}
this.enchantments = enchantments;
this.enchantments = Utils.nullableCopy(enchantments);
}
@Override
public Map<Enchantment, Integer> getEnchantments() {
return new HashMap<>(enchantments);
return enchantments;
}
@Override
@ -188,19 +184,20 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override
public void addEnchantment(Enchantment ench, Integer level) {
if (enchantments == null) {
enchantments = new HashMap<>();
}
enchantments.put(ench, level);
}
@Override
public void removeEnchantment(Enchantment ench) {
if (enchantments == null) {
return;
}
enchantments.remove(ench);
}
@Override
public void clearEnchantments() {
enchantments.clear();
}
@Override
public Color getLeatherColor() {
return leatherColor;
@ -239,7 +236,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override
public void setBannerPatterns(List<Pattern> bannerPatterns) {
this.bannerPatterns = bannerPatterns;
this.bannerPatterns = Utils.nullableCopy(bannerPatterns);
}
@Override
@ -367,7 +364,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
itemStack.setItemMeta(itemMeta);
if (enchantments.size() > 0) {
if (enchantments != null) {
for (Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
itemStack.addUnsafeEnchantment(entry.getKey(), entry.getValue());
}

View File

@ -26,6 +26,7 @@ import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.Permissions;
import me.filoghost.chestcommands.action.Action;
import me.filoghost.chestcommands.menu.icon.AdvancedIcon;
import me.filoghost.chestcommands.util.Utils;
public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
@ -45,12 +46,8 @@ public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
return fileName;
}
public List<Action> getOpenActions() {
return openActions;
}
public void setOpenActions(List<Action> openAction) {
this.openActions = openAction;
this.openActions = Utils.nullableCopy(openAction);
}
public String getPermission() {

View File

@ -19,8 +19,6 @@ import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import com.google.common.collect.ImmutableList;
import me.filoghost.chestcommands.action.Action;
import me.filoghost.chestcommands.action.OpenMenuAction;
import me.filoghost.chestcommands.api.impl.ConfigurableIconImpl;
@ -84,7 +82,7 @@ public class AdvancedIcon extends ConfigurableIconImpl {
}
public void setClickActions(List<Action> clickActions) {
this.clickActions = ImmutableList.copyOf(clickActions);
this.clickActions = Utils.nullableCopy(clickActions);
}
@Override

View File

@ -15,7 +15,6 @@
package me.filoghost.chestcommands.menu.settings;
import me.filoghost.chestcommands.action.Action;
import me.filoghost.chestcommands.util.Utils;
import java.util.List;
@ -45,10 +44,6 @@ public class MenuSettings {
return rows;
}
public boolean hasCommands() {
return !Utils.isNullOrEmpty(commands);
}
public void setCommands(List<String> commands) {
this.commands = commands;
}

View File

@ -14,7 +14,11 @@
*/
package me.filoghost.chestcommands.util;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class Utils {
@ -33,4 +37,20 @@ public final class Utils {
return collection == null || collection.isEmpty();
}
public static <E> List<E> nullableCopy(List<E> list) {
if (isNullOrEmpty(list)) {
return null;
} else {
return new ArrayList<>(list);
}
}
public static <K, V> Map<K, V> nullableCopy(Map<K, V> map) {
if (map == null || map.isEmpty()) {
return null;
} else {
return new HashMap<>(map);
}
}
}