From 92ec2c91f37047f47766f6697e744e2dbd563422 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 8 Nov 2015 19:07:40 +0100 Subject: [PATCH] Performance Fixes to TypeAbstractSelect --- .../massivecore/adapter/InventoryAdapter.java | 2 +- .../command/type/TypeAbstractChoice.java | 115 ++++++++++++------ .../command/type/TypePermission.java | 6 + .../command/type/TypePotionEffectType.java | 12 +- .../command/type/enumeration/TypeEnum.java | 13 +- .../type/enumeration/TypeEnvironment.java | 4 +- .../command/type/primitive/TypeBoolean.java | 14 +-- .../command/type/store/TypeColl.java | 6 + 8 files changed, 108 insertions(+), 64 deletions(-) diff --git a/src/com/massivecraft/massivecore/adapter/InventoryAdapter.java b/src/com/massivecraft/massivecore/adapter/InventoryAdapter.java index 8d2649c6..88bc0f18 100644 --- a/src/com/massivecraft/massivecore/adapter/InventoryAdapter.java +++ b/src/com/massivecraft/massivecore/adapter/InventoryAdapter.java @@ -201,7 +201,7 @@ public class InventoryAdapter implements JsonDeserializer, JsonSerial size = jsonSize.getAsInt(); // This is a "Custom" Inventory (content only). - ret = Mixin.createInventory(null, size, null); + ret = Mixin.createInventory(null, size, ""); } // Now process content diff --git a/src/com/massivecraft/massivecore/command/type/TypeAbstractChoice.java b/src/com/massivecraft/massivecore/command/type/TypeAbstractChoice.java index 3a555c59..e061ead4 100644 --- a/src/com/massivecraft/massivecore/command/type/TypeAbstractChoice.java +++ b/src/com/massivecraft/massivecore/command/type/TypeAbstractChoice.java @@ -1,6 +1,8 @@ package com.massivecraft.massivecore.command.type; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -51,10 +53,46 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A return ! TypeAbstractChoice.class.equals(ReflectionUtil.getSuperclassDeclaringMethod(this.getClass(), true, "canSee")); } - // TODO: cache stuff... the options? - protected boolean cachable = false; - public boolean isCachable() { return this.cachable; } - public void setCachable(boolean cachable) { this.cachable = cachable; } + // -------------------------------------------- // + // FIELDS: CACHE + // -------------------------------------------- // + + // All: You should either setAll or override getAll. + protected Collection all = null; + public Collection getAll() { return all; } + public void setAll(Collection all) + { + if (all != null) all = Collections.unmodifiableCollection(new MassiveList(all)); + this.all = all; + + if (all == null) + { + this.options = null; + this.tabs = null; + } + else if ( ! this.isCanSeeOverridden()) + { + // The "all" cache is set and canSee is not overriden. + // This means we can cache options and tabs. + this.options = this.createOptions(all); + this.tabs = this.createTabs((CommandSender)null); + } + } + @SafeVarargs + public final void setAll(T... all) + { + this.setAll(Arrays.asList(all)); + } + + // Options + protected Map options = null; + public Map getOptions() { return options; } + public void setOptions(Map options) { this.options = options; } + + // Tabs + protected Collection tabs = null; + public Collection getTabs() { return this.tabs; } + public void setTabs(Collection tabs) { this.tabs = tabs; } // -------------------------------------------- // // OVERRIDE: TYPE @@ -74,14 +112,10 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A Collection all = this.getAll(sender); // Get Options - // NOTE: These keys are prepared. - // TODO: Optimization should be possible here. - // TODO: If the "all" never changes AKA are cached... we can cache the options as well. - // TODO: If they are different for visibility but do not change... we can - Map options = this.getOptions(all); + Map options = this.getOptions(); + if (options == null) options = this.createOptions(all); // Get Matches - // NOTE: I can probably not optimize this method further? List matches = this.getMatches(options, arg, false); // Exact @@ -172,10 +206,6 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A throw exception; } - // -------------------------------------------- // - // ABSTRACT - // -------------------------------------------- // - // -------------------------------------------- // // OVERRIDE: ALL ABLE // -------------------------------------------- // @@ -200,8 +230,6 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A return ret; } - public abstract Collection getAll(); - public boolean canList(CommandSender sender) { return true; @@ -222,11 +250,11 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A List ret = new MassiveList(); // Prepare - arg = this.prepareKey(arg); + arg = this.prepareOptionKey(arg); // Exact T exact = options.get(arg); - if (exact != null) return new MassiveList(exact); + if (exact != null) return Collections.singletonList(exact); // Fill for (Entry entry : options.entrySet()) @@ -275,7 +303,7 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A // OPTIONS // -------------------------------------------- // - public Map getOptions(Iterable all) + public Map createOptions(Iterable all) { // Create Map ret = new MassiveMap(); @@ -283,7 +311,7 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A // Fill for (T value : all) { - for (String key : this.getKeys(value)) + for (String key : this.createOptionKeys(value)) { ret.put(key, value); } @@ -294,21 +322,25 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A } // This method creates keys for a certain value. - // They are not comparable. - public List getKeys(T value) + // They ARE comparable. + public List createOptionKeys(T value) { // Create List ret = new MassiveList(); // Fill + String string; + for (String name : this.getNames(value)) { - ret.add(this.prepareKey(name)); + string = this.prepareOptionKey(name); + if (string != null) ret.add(string); } for (String id : this.getIds(value)) { - ret.add(this.prepareKey(id)); + string = this.prepareOptionKey(id); + if (string != null) ret.add(string); } // Return @@ -317,7 +349,7 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A // The purpose of this method is to strip down a string to a comparable string key. protected static Pattern PATTERN_KEY_UNWANTED = Pattern.compile("[_\\-\\s]+"); - public String prepareKey(String string) + public String prepareOptionKey(String string) { if (string == null) return null; string = string.trim(); @@ -333,6 +365,13 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A @Override public Collection getTabList(CommandSender sender, String arg) + { + Collection ret = this.getTabs(); + if (ret == null) ret = this.createTabs(sender); + return ret; + } + + public Set createTabs(CommandSender sender) { // Create Set ret = new MassiveSet(); @@ -340,29 +379,37 @@ public abstract class TypeAbstractChoice extends TypeAbstract implements A // Fill for (T value : this.getAll(sender)) { - ret.addAll(this.getTabs(value)); + ret.addAll(this.createTabs(value)); } // Return return ret; } - public Set getTabs(T value) + public Set createTabs(T value) { // Create Set ret = new MassiveTreeSet(CaseInsensitiveComparator.get()); // Fill - for (String name : this.getNames(value)) - { - ret.add(this.prepareTab(name, true)); - ret.add(this.prepareTab(name, false)); - } + String string; for (String id : this.getIds(value)) { - ret.add(this.prepareTab(id, true)); - ret.add(this.prepareTab(id, false)); + string = this.prepareTab(id, true); + if (string != null) ret.add(string); + + string = this.prepareTab(id, false); + if (string != null) ret.add(string); + } + + for (String name : this.getNames(value)) + { + string = this.prepareTab(name, true); + if (string != null) ret.add(string); + + string = this.prepareTab(name, false); + if (string != null) ret.add(string); } // Return diff --git a/src/com/massivecraft/massivecore/command/type/TypePermission.java b/src/com/massivecraft/massivecore/command/type/TypePermission.java index 71f76122..9877972e 100644 --- a/src/com/massivecraft/massivecore/command/type/TypePermission.java +++ b/src/com/massivecraft/massivecore/command/type/TypePermission.java @@ -28,5 +28,11 @@ public class TypePermission extends TypeAbstractChoice { return Bukkit.getPluginManager().getPermissions(); } + + @Override + public Permission getExactMatch(String arg) + { + return Bukkit.getPluginManager().getPermission(arg); + } } diff --git a/src/com/massivecraft/massivecore/command/type/TypePotionEffectType.java b/src/com/massivecraft/massivecore/command/type/TypePotionEffectType.java index b7680432..3f88131c 100644 --- a/src/com/massivecraft/massivecore/command/type/TypePotionEffectType.java +++ b/src/com/massivecraft/massivecore/command/type/TypePotionEffectType.java @@ -1,7 +1,5 @@ package com.massivecraft.massivecore.command.type; -import java.util.Arrays; -import java.util.Collection; import org.bukkit.potion.PotionEffectType; public class TypePotionEffectType extends TypeAbstractChoice @@ -12,6 +10,10 @@ public class TypePotionEffectType extends TypeAbstractChoice private static TypePotionEffectType i = new TypePotionEffectType(); public static TypePotionEffectType get() { return i; } + public TypePotionEffectType() + { + this.setAll(PotionEffectType.values()); + } // -------------------------------------------- // // OVERRIDE @@ -29,11 +31,5 @@ public class TypePotionEffectType extends TypeAbstractChoice { return String.valueOf(value.getId()); } - - @Override - public Collection getAll() - { - return Arrays.asList(PotionEffectType.values()); - } } diff --git a/src/com/massivecraft/massivecore/command/type/enumeration/TypeEnum.java b/src/com/massivecraft/massivecore/command/type/enumeration/TypeEnum.java index 9650472c..72c907b8 100644 --- a/src/com/massivecraft/massivecore/command/type/enumeration/TypeEnum.java +++ b/src/com/massivecraft/massivecore/command/type/enumeration/TypeEnum.java @@ -1,8 +1,5 @@ package com.massivecraft.massivecore.command.type.enumeration; -import java.util.Arrays; -import java.util.Collection; - import com.massivecraft.massivecore.command.type.TypeAbstractChoice; import com.massivecraft.massivecore.util.Txt; @@ -23,6 +20,8 @@ public class TypeEnum> extends TypeAbstractChoice { if ( ! clazz.isEnum()) throw new IllegalArgumentException("clazz must be enum"); this.clazz = clazz; + + this.setAll(getEnumValues(this.getClazz())); } // -------------------------------------------- // @@ -44,13 +43,7 @@ public class TypeEnum> extends TypeAbstractChoice @Override public String getIdInner(T value) { - return String.valueOf(value.ordinal()); - } - - @Override - public Collection getAll() - { - return Arrays.asList(getEnumValues(this.getClazz())); + return value.name(); } // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/command/type/enumeration/TypeEnvironment.java b/src/com/massivecraft/massivecore/command/type/enumeration/TypeEnvironment.java index 2a55e046..e5622a09 100644 --- a/src/com/massivecraft/massivecore/command/type/enumeration/TypeEnvironment.java +++ b/src/com/massivecraft/massivecore/command/type/enumeration/TypeEnvironment.java @@ -4,6 +4,8 @@ import java.util.Set; import org.bukkit.World.Environment; +import com.massivecraft.massivecore.collections.MassiveSet; + public class TypeEnvironment extends TypeEnum { // -------------------------------------------- // @@ -24,7 +26,7 @@ public class TypeEnvironment extends TypeEnum @Override public Set getNamesInner(Environment value) { - Set ret = super.getNamesInner(value); + Set ret = new MassiveSet(super.getNamesInner(value)); if (value == Environment.NORMAL) { diff --git a/src/com/massivecraft/massivecore/command/type/primitive/TypeBoolean.java b/src/com/massivecraft/massivecore/command/type/primitive/TypeBoolean.java index 47cee995..3bb27448 100644 --- a/src/com/massivecraft/massivecore/command/type/primitive/TypeBoolean.java +++ b/src/com/massivecraft/massivecore/command/type/primitive/TypeBoolean.java @@ -1,6 +1,5 @@ package com.massivecraft.massivecore.command.type.primitive; -import java.util.Collection; import java.util.Set; import org.bukkit.ChatColor; @@ -57,6 +56,10 @@ public class TypeBoolean extends TypeAbstractChoice { this.stringTrue = t; this.stringFalse = f; + this.setAll( + Boolean.TRUE, + Boolean.FALSE + ); } // -------------------------------------------- // @@ -102,14 +105,5 @@ public class TypeBoolean extends TypeAbstractChoice { return value.toString(); } - - @Override - public Collection getAll() - { - return new MassiveSet( - Boolean.TRUE, - Boolean.FALSE - ); - } } diff --git a/src/com/massivecraft/massivecore/command/type/store/TypeColl.java b/src/com/massivecraft/massivecore/command/type/store/TypeColl.java index 5e91b84a..37dd0268 100644 --- a/src/com/massivecraft/massivecore/command/type/store/TypeColl.java +++ b/src/com/massivecraft/massivecore/command/type/store/TypeColl.java @@ -24,4 +24,10 @@ public class TypeColl extends TypeAbstractChoice> return Coll.getMap().values(); } + @Override + public Coll getExactMatch(String arg) + { + return Coll.getMap().get(arg); + } + }