Sorting contract must not be broken. Thus the only sane sorting is by id.

This commit is contained in:
Olof Larsson 2015-08-03 16:16:49 +02:00
parent 98ba8cda53
commit 593dd65e2e
5 changed files with 56 additions and 46 deletions

View File

@ -103,6 +103,19 @@ public class Coll<E> extends CollAbstract<E>
protected Map<String, E> id2entity; protected Map<String, E> id2entity;
protected Map<E, String> entity2id; protected Map<E, String> 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<String, E> getId2entity() { return Collections.unmodifiableMap(this.id2entity); } @Override public Map<String, E> getId2entity() { return Collections.unmodifiableMap(this.id2entity); }
@Override @Override
public E getFixed(String id, boolean creative) public E getFixed(String id, boolean creative)
@ -135,23 +148,6 @@ public class Coll<E> extends CollAbstract<E>
{ {
return Collections.unmodifiableCollection(this.entity2id.keySet()); return Collections.unmodifiableCollection(this.entity2id.keySet());
} }
@Override public List<E> getAll(Predictate<? super E> where) { return MStoreUtil.uglySQL(this.getAll(), where, null, null, null); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, null, null); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, null); }
@Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> 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 // BEHAVIOR
@ -816,7 +812,8 @@ public class Coll<E> extends CollAbstract<E>
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public Coll(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing, Comparator<? super String> idComparator, Comparator<? super E> entityComparator) @SuppressWarnings("unchecked")
public Coll(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing)
{ {
// Setup the name and the parsed parts // Setup the name and the parsed parts
this.name = name; this.name = name;
@ -842,8 +839,8 @@ public class Coll<E> extends CollAbstract<E>
this.collDriverObject = db.createCollDriverObject(this); this.collDriverObject = db.createCollDriverObject(this);
// STORAGE // STORAGE
this.id2entity = entityComparator != null ? new ConcurrentSkipListMap<String, E>(idComparator) : new ConcurrentHashMap<String, E>(); this.id2entity = new ConcurrentSkipListMap<String, E>(NaturalOrderComparator.get());
this.entity2id = entityComparator != null ? new ConcurrentSkipListMap<E, String>(entityComparator) : new ConcurrentHashMap<E, String>(); this.entity2id = Entity.class.isAssignableFrom(entityClass) ? new ConcurrentSkipListMap<E, String>((Comparator<? super E>) ComparatorEntityId.get()) : new ConcurrentHashMap<E, String>();
// IDENTIFIED MODIFICATIONS // IDENTIFIED MODIFICATIONS
this.identifiedModifications = new ConcurrentHashMap<String, Modification>(); this.identifiedModifications = new ConcurrentHashMap<String, Modification>();
@ -859,11 +856,6 @@ public class Coll<E> extends CollAbstract<E>
}; };
} }
public Coll(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing)
{
this(name, entityClass, db, plugin, creative, lowercasing, null, null);
}
public Coll(String name, Class<E> entityClass, Db db, Plugin plugin) public Coll(String name, Class<E> entityClass, Db db, Plugin plugin)
{ {
this(name, entityClass, db, plugin, false, false); this(name, entityClass, db, plugin, false, false);

View File

@ -1,7 +1,10 @@
package com.massivecraft.massivecore.store; package com.massivecraft.massivecore.store;
import java.util.Comparator;
import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.massivecraft.massivecore.Predictate;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
@ -21,6 +24,14 @@ public abstract class CollAbstract<E> implements CollInterface<E>
// STORAGE // 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 @Override
public E get(Object oid) public E get(Object oid)
{ {
@ -45,13 +56,17 @@ public abstract class CollAbstract<E> implements CollInterface<E>
return this.containsIdFixed(this.fixId(oid)); return this.containsIdFixed(this.fixId(oid));
} }
@Override @Override public List<E> getAll(Predictate<? super E> where) { return MStoreUtil.uglySQL(this.getAll(), where, null, null, null); }
public String fixIdOrThrow(Object oid) throws IllegalArgumentException @Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, null, null); }
{ @Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, null); }
String ret = this.fixId(oid); @Override public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, orderby, limit, offset); }
if (ret == null) throw new IllegalArgumentException(String.valueOf(oid) + " is not a valid id."); @Override public List<E> getAll(Predictate<? super E> where, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), where, null, limit, null); }
return ret; @Override public List<E> getAll(Predictate<? super E> where, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), where, null, limit, offset); }
} @Override public List<E> getAll(Comparator<? super E> orderby) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, null, null); }
@Override public List<E> getAll(Comparator<? super E> orderby, Integer limit) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, limit, null); }
@Override public List<E> getAll(Comparator<? super E> orderby, Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), null, orderby, limit, offset); }
@Override public List<E> getAll(Integer limit) { return MStoreUtil.uglySQL(this.getAll(), null, null, limit, null); }
@Override public List<E> getAll(Integer limit, Integer offset) { return MStoreUtil.uglySQL(this.getAll(), null, null, limit, offset); }
// -------------------------------------------- // // -------------------------------------------- //
// BEHAVIOR // BEHAVIOR

View File

@ -16,6 +16,7 @@ public interface CollInterface<E>
// -------------------------------------------- // // -------------------------------------------- //
// WHAT DO WE HANDLE? // WHAT DO WE HANDLE?
// -------------------------------------------- // // -------------------------------------------- //
public String getName(); public String getName();
public String getBasename(); public String getBasename();
public String getUniverse(); public String getUniverse();
@ -24,6 +25,7 @@ public interface CollInterface<E>
// -------------------------------------------- // // -------------------------------------------- //
// SUPPORTING SYSTEM // SUPPORTING SYSTEM
// -------------------------------------------- // // -------------------------------------------- //
public Plugin getPlugin(); public Plugin getPlugin();
public Db getDb(); public Db getDb();
@ -32,6 +34,10 @@ public interface CollInterface<E>
// -------------------------------------------- // // -------------------------------------------- //
// STORAGE // STORAGE
// -------------------------------------------- // // -------------------------------------------- //
public String fixId(Object oid);
public String fixIdOrThrow(Object oid) throws IllegalArgumentException;
public Map<String, E> getId2entity(); public Map<String, E> getId2entity();
public E get(Object oid); public E get(Object oid);
public E get(Object oid, boolean creative); public E get(Object oid, boolean creative);
@ -45,14 +51,19 @@ public interface CollInterface<E>
public Map<E, String> getEntity2id(); public Map<E, String> getEntity2id();
public String getId(Object entity); public String getId(Object entity);
public boolean containsEntity(Object entity); public boolean containsEntity(Object entity);
public Collection<E> getAll(); public Collection<E> getAll();
public List<E> getAll(Predictate<? super E> where); public List<E> getAll(Predictate<? super E> where);
public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby); public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby);
public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit); public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit);
public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset); public List<E> getAll(Predictate<? super E> where, Comparator<? super E> orderby, Integer limit, Integer offset);
public List<E> getAll(Predictate<? super E> where, Integer limit);
public String fixId(Object oid); public List<E> getAll(Predictate<? super E> where, Integer limit, Integer offset);
public String fixIdOrThrow(Object oid) throws IllegalArgumentException; public List<E> getAll(Comparator<? super E> orderby);
public List<E> getAll(Comparator<? super E> orderby, Integer limit);
public List<E> getAll(Comparator<? super E> orderby, Integer limit, Integer offset);
public List<E> getAll(Integer limit);
public List<E> getAll(Integer limit, Integer offset);
// -------------------------------------------- // // -------------------------------------------- //
// BEHAVIOR // BEHAVIOR

View File

@ -30,12 +30,10 @@ public class ComparatorEntityId implements Comparator<Entity<?>>
String id2 = e2.getId(); String id2 = e2.getId();
int ret = NaturalOrderComparator.get().compare(id1, id2); int ret = NaturalOrderComparator.get().compare(id1, id2);
if (ret != 0) return ret;
// Only return 0 if they are the same. // We should only return 0 if the items actually are equal.
// We avoid that with this ugly solution. return e2.hashCode() - e1.hashCode();
if (ret == 0) ret = -1;
return ret;
} }
} }

View File

@ -2,7 +2,6 @@ package com.massivecraft.massivecore.store;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.List; import java.util.List;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -22,11 +21,6 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E> implements Se
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public SenderColl(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing, Comparator<? super String> idComparator, Comparator<? super E> entityComparator)
{
super(name, entityClass, db, plugin, creative, lowercasing, idComparator, entityComparator);
}
public SenderColl(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing) public SenderColl(String name, Class<E> entityClass, Db db, Plugin plugin, boolean creative, boolean lowercasing)
{ {
super(name, entityClass, db, plugin, creative, lowercasing); super(name, entityClass, db, plugin, creative, lowercasing);