Performance Fixes to TypeAbstractSelect

This commit is contained in:
Olof Larsson 2015-11-08 19:07:40 +01:00
parent fdd530998a
commit 92ec2c91f3
8 changed files with 108 additions and 64 deletions

View File

@ -201,7 +201,7 @@ public class InventoryAdapter implements JsonDeserializer<Inventory>, JsonSerial
size = jsonSize.getAsInt(); size = jsonSize.getAsInt();
// This is a "Custom" Inventory (content only). // This is a "Custom" Inventory (content only).
ret = Mixin.createInventory(null, size, null); ret = Mixin.createInventory(null, size, "");
} }
// Now process content // Now process content

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.command.type; package com.massivecraft.massivecore.command.type;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -51,10 +53,46 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
return ! TypeAbstractChoice.class.equals(ReflectionUtil.getSuperclassDeclaringMethod(this.getClass(), true, "canSee")); return ! TypeAbstractChoice.class.equals(ReflectionUtil.getSuperclassDeclaringMethod(this.getClass(), true, "canSee"));
} }
// TODO: cache stuff... the options? // -------------------------------------------- //
protected boolean cachable = false; // FIELDS: CACHE
public boolean isCachable() { return this.cachable; } // -------------------------------------------- //
public void setCachable(boolean cachable) { this.cachable = cachable; }
// All: You should either setAll or override getAll.
protected Collection<T> all = null;
public Collection<T> getAll() { return all; }
public void setAll(Collection<T> all)
{
if (all != null) all = Collections.unmodifiableCollection(new MassiveList<T>(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<String, T> options = null;
public Map<String, T> getOptions() { return options; }
public void setOptions(Map<String, T> options) { this.options = options; }
// Tabs
protected Collection<String> tabs = null;
public Collection<String> getTabs() { return this.tabs; }
public void setTabs(Collection<String> tabs) { this.tabs = tabs; }
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE: TYPE // OVERRIDE: TYPE
@ -74,14 +112,10 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
Collection<T> all = this.getAll(sender); Collection<T> all = this.getAll(sender);
// Get Options // Get Options
// NOTE: These keys are prepared. Map<String, T> options = this.getOptions();
// TODO: Optimization should be possible here. if (options == null) options = this.createOptions(all);
// 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<String, T> options = this.getOptions(all);
// Get Matches // Get Matches
// NOTE: I can probably not optimize this method further?
List<T> matches = this.getMatches(options, arg, false); List<T> matches = this.getMatches(options, arg, false);
// Exact // Exact
@ -172,10 +206,6 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
throw exception; throw exception;
} }
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE: ALL ABLE // OVERRIDE: ALL ABLE
// -------------------------------------------- // // -------------------------------------------- //
@ -200,8 +230,6 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
return ret; return ret;
} }
public abstract Collection<T> getAll();
public boolean canList(CommandSender sender) public boolean canList(CommandSender sender)
{ {
return true; return true;
@ -222,11 +250,11 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
List<T> ret = new MassiveList<T>(); List<T> ret = new MassiveList<T>();
// Prepare // Prepare
arg = this.prepareKey(arg); arg = this.prepareOptionKey(arg);
// Exact // Exact
T exact = options.get(arg); T exact = options.get(arg);
if (exact != null) return new MassiveList<T>(exact); if (exact != null) return Collections.singletonList(exact);
// Fill // Fill
for (Entry<String, T> entry : options.entrySet()) for (Entry<String, T> entry : options.entrySet())
@ -275,7 +303,7 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
// OPTIONS // OPTIONS
// -------------------------------------------- // // -------------------------------------------- //
public Map<String, T> getOptions(Iterable<T> all) public Map<String, T> createOptions(Iterable<T> all)
{ {
// Create // Create
Map<String, T> ret = new MassiveMap<String, T>(); Map<String, T> ret = new MassiveMap<String, T>();
@ -283,7 +311,7 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
// Fill // Fill
for (T value : all) for (T value : all)
{ {
for (String key : this.getKeys(value)) for (String key : this.createOptionKeys(value))
{ {
ret.put(key, value); ret.put(key, value);
} }
@ -294,21 +322,25 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
} }
// This method creates keys for a certain value. // This method creates keys for a certain value.
// They are not comparable. // They ARE comparable.
public List<String> getKeys(T value) public List<String> createOptionKeys(T value)
{ {
// Create // Create
List<String> ret = new MassiveList<String>(); List<String> ret = new MassiveList<String>();
// Fill // Fill
String string;
for (String name : this.getNames(value)) 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)) for (String id : this.getIds(value))
{ {
ret.add(this.prepareKey(id)); string = this.prepareOptionKey(id);
if (string != null) ret.add(string);
} }
// Return // Return
@ -317,7 +349,7 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
// The purpose of this method is to strip down a string to a comparable string key. // 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]+"); protected static Pattern PATTERN_KEY_UNWANTED = Pattern.compile("[_\\-\\s]+");
public String prepareKey(String string) public String prepareOptionKey(String string)
{ {
if (string == null) return null; if (string == null) return null;
string = string.trim(); string = string.trim();
@ -333,6 +365,13 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
@Override @Override
public Collection<String> getTabList(CommandSender sender, String arg) public Collection<String> getTabList(CommandSender sender, String arg)
{
Collection<String> ret = this.getTabs();
if (ret == null) ret = this.createTabs(sender);
return ret;
}
public Set<String> createTabs(CommandSender sender)
{ {
// Create // Create
Set<String> ret = new MassiveSet<String>(); Set<String> ret = new MassiveSet<String>();
@ -340,29 +379,37 @@ public abstract class TypeAbstractChoice<T> extends TypeAbstract<T> implements A
// Fill // Fill
for (T value : this.getAll(sender)) for (T value : this.getAll(sender))
{ {
ret.addAll(this.getTabs(value)); ret.addAll(this.createTabs(value));
} }
// Return // Return
return ret; return ret;
} }
public Set<String> getTabs(T value) public Set<String> createTabs(T value)
{ {
// Create // Create
Set<String> ret = new MassiveTreeSet<String, CaseInsensitiveComparator>(CaseInsensitiveComparator.get()); Set<String> ret = new MassiveTreeSet<String, CaseInsensitiveComparator>(CaseInsensitiveComparator.get());
// Fill // Fill
for (String name : this.getNames(value)) String string;
{
ret.add(this.prepareTab(name, true));
ret.add(this.prepareTab(name, false));
}
for (String id : this.getIds(value)) for (String id : this.getIds(value))
{ {
ret.add(this.prepareTab(id, true)); string = this.prepareTab(id, true);
ret.add(this.prepareTab(id, false)); 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 // Return

View File

@ -28,5 +28,11 @@ public class TypePermission extends TypeAbstractChoice<Permission>
{ {
return Bukkit.getPluginManager().getPermissions(); return Bukkit.getPluginManager().getPermissions();
} }
@Override
public Permission getExactMatch(String arg)
{
return Bukkit.getPluginManager().getPermission(arg);
}
} }

View File

@ -1,7 +1,5 @@
package com.massivecraft.massivecore.command.type; package com.massivecraft.massivecore.command.type;
import java.util.Arrays;
import java.util.Collection;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
public class TypePotionEffectType extends TypeAbstractChoice<PotionEffectType> public class TypePotionEffectType extends TypeAbstractChoice<PotionEffectType>
@ -12,6 +10,10 @@ public class TypePotionEffectType extends TypeAbstractChoice<PotionEffectType>
private static TypePotionEffectType i = new TypePotionEffectType(); private static TypePotionEffectType i = new TypePotionEffectType();
public static TypePotionEffectType get() { return i; } public static TypePotionEffectType get() { return i; }
public TypePotionEffectType()
{
this.setAll(PotionEffectType.values());
}
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE // OVERRIDE
@ -29,11 +31,5 @@ public class TypePotionEffectType extends TypeAbstractChoice<PotionEffectType>
{ {
return String.valueOf(value.getId()); return String.valueOf(value.getId());
} }
@Override
public Collection<PotionEffectType> getAll()
{
return Arrays.asList(PotionEffectType.values());
}
} }

View File

@ -1,8 +1,5 @@
package com.massivecraft.massivecore.command.type.enumeration; 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.command.type.TypeAbstractChoice;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
@ -23,6 +20,8 @@ public class TypeEnum<T extends Enum<T>> extends TypeAbstractChoice<T>
{ {
if ( ! clazz.isEnum()) throw new IllegalArgumentException("clazz must be enum"); if ( ! clazz.isEnum()) throw new IllegalArgumentException("clazz must be enum");
this.clazz = clazz; this.clazz = clazz;
this.setAll(getEnumValues(this.getClazz()));
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -44,13 +43,7 @@ public class TypeEnum<T extends Enum<T>> extends TypeAbstractChoice<T>
@Override @Override
public String getIdInner(T value) public String getIdInner(T value)
{ {
return String.valueOf(value.ordinal()); return value.name();
}
@Override
public Collection<T> getAll()
{
return Arrays.asList(getEnumValues(this.getClazz()));
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -4,6 +4,8 @@ import java.util.Set;
import org.bukkit.World.Environment; import org.bukkit.World.Environment;
import com.massivecraft.massivecore.collections.MassiveSet;
public class TypeEnvironment extends TypeEnum<Environment> public class TypeEnvironment extends TypeEnum<Environment>
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -24,7 +26,7 @@ public class TypeEnvironment extends TypeEnum<Environment>
@Override @Override
public Set<String> getNamesInner(Environment value) public Set<String> getNamesInner(Environment value)
{ {
Set<String> ret = super.getNamesInner(value); Set<String> ret = new MassiveSet<String>(super.getNamesInner(value));
if (value == Environment.NORMAL) if (value == Environment.NORMAL)
{ {

View File

@ -1,6 +1,5 @@
package com.massivecraft.massivecore.command.type.primitive; package com.massivecraft.massivecore.command.type.primitive;
import java.util.Collection;
import java.util.Set; import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -57,6 +56,10 @@ public class TypeBoolean extends TypeAbstractChoice<Boolean>
{ {
this.stringTrue = t; this.stringTrue = t;
this.stringFalse = f; this.stringFalse = f;
this.setAll(
Boolean.TRUE,
Boolean.FALSE
);
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -102,14 +105,5 @@ public class TypeBoolean extends TypeAbstractChoice<Boolean>
{ {
return value.toString(); return value.toString();
} }
@Override
public Collection<Boolean> getAll()
{
return new MassiveSet<Boolean>(
Boolean.TRUE,
Boolean.FALSE
);
}
} }

View File

@ -24,4 +24,10 @@ public class TypeColl extends TypeAbstractChoice<Coll<?>>
return Coll.getMap().values(); return Coll.getMap().values();
} }
@Override
public Coll<?> getExactMatch(String arg)
{
return Coll.getMap().get(arg);
}
} }