diff --git a/src/com/massivecraft/massivecore/store/Coll.java b/src/com/massivecraft/massivecore/store/Coll.java index b8252928..83be9f6c 100644 --- a/src/com/massivecraft/massivecore/store/Coll.java +++ b/src/com/massivecraft/massivecore/store/Coll.java @@ -103,6 +103,19 @@ public class Coll extends CollAbstract protected Map id2entity; protected Map entity2id; + @Override + public String fixId(Object oid) + { + if (oid == null) return null; + + String ret = null; + if (oid instanceof String) ret = (String) oid; + else if (oid.getClass() == this.getEntityClass()) ret = this.entity2id.get(oid); + if (ret == null) return null; + + return this.isLowercasing() ? ret.toLowerCase() : ret; + } + @Override public Map getId2entity() { return Collections.unmodifiableMap(this.id2entity); } @Override public E getFixed(String id, boolean creative) @@ -135,23 +148,6 @@ public class Coll extends CollAbstract { return Collections.unmodifiableCollection(this.entity2id.keySet()); } - @Override public List getAll(Predictate where) { return MStoreUtil.uglySQL(this.getAll(), where, null, null, null); } - @Override public List getAll(Predictate where, Comparator orderby) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, null, null); } - @Override public List getAll(Predictate where, Comparator orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, null); } - @Override public List getAll(Predictate where, Comparator orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, offset); } - - @Override - public String fixId(Object oid) - { - if (oid == null) return null; - - String ret = null; - if (oid instanceof String) ret = (String) oid; - else if (oid.getClass() == this.getEntityClass()) ret = this.entity2id.get(oid); - if (ret == null) return null; - - return this.isLowercasing() ? ret.toLowerCase() : ret; - } // -------------------------------------------- // // BEHAVIOR @@ -816,7 +812,8 @@ public class Coll extends CollAbstract // CONSTRUCT // -------------------------------------------- // - public Coll(String name, Class entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing, Comparator idComparator, Comparator entityComparator) + @SuppressWarnings("unchecked") + public Coll(String name, Class entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing) { // Setup the name and the parsed parts this.name = name; @@ -842,8 +839,8 @@ public class Coll extends CollAbstract this.collDriverObject = db.createCollDriverObject(this); // STORAGE - this.id2entity = entityComparator != null ? new ConcurrentSkipListMap(idComparator) : new ConcurrentHashMap(); - this.entity2id = entityComparator != null ? new ConcurrentSkipListMap(entityComparator) : new ConcurrentHashMap(); + this.id2entity = new ConcurrentSkipListMap(NaturalOrderComparator.get()); + this.entity2id = Entity.class.isAssignableFrom(entityClass) ? new ConcurrentSkipListMap((Comparator) ComparatorEntityId.get()) : new ConcurrentHashMap(); // IDENTIFIED MODIFICATIONS this.identifiedModifications = new ConcurrentHashMap(); @@ -859,11 +856,6 @@ public class Coll extends CollAbstract }; } - public Coll(String name, Class entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing) - { - this(name, entityClass, db, plugin, creative, lowercasing, null, null); - } - public Coll(String name, Class entityClass, Db db, Plugin plugin) { this(name, entityClass, db, plugin, false, false); diff --git a/src/com/massivecraft/massivecore/store/CollAbstract.java b/src/com/massivecraft/massivecore/store/CollAbstract.java index a377b97c..44cd393e 100644 --- a/src/com/massivecraft/massivecore/store/CollAbstract.java +++ b/src/com/massivecraft/massivecore/store/CollAbstract.java @@ -1,7 +1,10 @@ package com.massivecraft.massivecore.store; +import java.util.Comparator; +import java.util.List; import java.util.Map.Entry; +import com.massivecraft.massivecore.Predictate; import com.massivecraft.massivecore.xlib.gson.JsonElement; @@ -21,6 +24,14 @@ public abstract class CollAbstract implements CollInterface // STORAGE // -------------------------------------------- // + @Override + public String fixIdOrThrow(Object oid) throws IllegalArgumentException + { + String ret = this.fixId(oid); + if (ret == null) throw new IllegalArgumentException(String.valueOf(oid) + " is not a valid id."); + return ret; + } + @Override public E get(Object oid) { @@ -45,13 +56,17 @@ public abstract class CollAbstract implements CollInterface return this.containsIdFixed(this.fixId(oid)); } - @Override - public String fixIdOrThrow(Object oid) throws IllegalArgumentException - { - String ret = this.fixId(oid); - if (ret == null) throw new IllegalArgumentException(String.valueOf(oid) + " is not a valid id."); - return ret; - } + @Override public List getAll(Predictate where) { return MStoreUtil.uglySQL(this.getAll(), where, null, null, null); } + @Override public List getAll(Predictate where, Comparator orderby) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, null, null); } + @Override public List getAll(Predictate where, Comparator orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, null); } + @Override public List getAll(Predictate where, Comparator orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, offset); } + @Override public List getAll(Predictate where, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, null, limit, null); } + @Override public List getAll(Predictate where, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, null, limit, offset); } + @Override public List getAll(Comparator orderby) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, null, null); } + @Override public List getAll(Comparator orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, limit, null); } + @Override public List getAll(Comparator orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, limit, offset); } + @Override public List getAll(Integer limit) { return MStoreUtil.uglySQL(this.getAll(), null, null, limit, null); } + @Override public List getAll(Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), null, null, limit, offset); } // -------------------------------------------- // // BEHAVIOR diff --git a/src/com/massivecraft/massivecore/store/CollInterface.java b/src/com/massivecraft/massivecore/store/CollInterface.java index 6d84d672..d4bd1599 100644 --- a/src/com/massivecraft/massivecore/store/CollInterface.java +++ b/src/com/massivecraft/massivecore/store/CollInterface.java @@ -16,6 +16,7 @@ public interface CollInterface // -------------------------------------------- // // WHAT DO WE HANDLE? // -------------------------------------------- // + public String getName(); public String getBasename(); public String getUniverse(); @@ -24,6 +25,7 @@ public interface CollInterface // -------------------------------------------- // // SUPPORTING SYSTEM // -------------------------------------------- // + public Plugin getPlugin(); public Db getDb(); @@ -32,6 +34,10 @@ public interface CollInterface // -------------------------------------------- // // STORAGE // -------------------------------------------- // + + public String fixId(Object oid); + public String fixIdOrThrow(Object oid) throws IllegalArgumentException; + public Map getId2entity(); public E get(Object oid); public E get(Object oid, boolean creative); @@ -45,14 +51,19 @@ public interface CollInterface public Map getEntity2id(); public String getId(Object entity); public boolean containsEntity(Object entity); + public Collection getAll(); public List getAll(Predictate where); public List getAll(Predictate where, Comparator orderby); public List getAll(Predictate where, Comparator orderby, Integer limit); public List getAll(Predictate where, Comparator orderby, Integer limit, Integer offset); - - public String fixId(Object oid); - public String fixIdOrThrow(Object oid) throws IllegalArgumentException; + public List getAll(Predictate where, Integer limit); + public List getAll(Predictate where, Integer limit, Integer offset); + public List getAll(Comparator orderby); + public List getAll(Comparator orderby, Integer limit); + public List getAll(Comparator orderby, Integer limit, Integer offset); + public List getAll(Integer limit); + public List getAll(Integer limit, Integer offset); // -------------------------------------------- // // BEHAVIOR diff --git a/src/com/massivecraft/massivecore/store/ComparatorEntityId.java b/src/com/massivecraft/massivecore/store/ComparatorEntityId.java index 01296020..6f16a8aa 100644 --- a/src/com/massivecraft/massivecore/store/ComparatorEntityId.java +++ b/src/com/massivecraft/massivecore/store/ComparatorEntityId.java @@ -30,12 +30,10 @@ public class ComparatorEntityId implements Comparator> String id2 = e2.getId(); int ret = NaturalOrderComparator.get().compare(id1, id2); + if (ret != 0) return ret; - // Only return 0 if they are the same. - // We avoid that with this ugly solution. - if (ret == 0) ret = -1; - - return ret; + // We should only return 0 if the items actually are equal. + return e2.hashCode() - e1.hashCode(); } } diff --git a/src/com/massivecraft/massivecore/store/SenderColl.java b/src/com/massivecraft/massivecore/store/SenderColl.java index 52dff7fb..b61eedfb 100644 --- a/src/com/massivecraft/massivecore/store/SenderColl.java +++ b/src/com/massivecraft/massivecore/store/SenderColl.java @@ -2,7 +2,6 @@ package com.massivecraft.massivecore.store; import java.util.ArrayList; import java.util.Collection; -import java.util.Comparator; import java.util.List; import org.bukkit.command.CommandSender; @@ -22,11 +21,6 @@ public class SenderColl> extends Coll implements Se // CONSTRUCT // -------------------------------------------- // - public SenderColl(String name, Class entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing, Comparator idComparator, Comparator entityComparator) - { - super(name, entityClass, db, plugin, creative, lowercasing, idComparator, entityComparator); - } - public SenderColl(String name, Class entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing) { super(name, entityClass, db, plugin, creative, lowercasing);