diff --git a/src/com/massivecraft/mcore/HashCodeComparator.java b/src/com/massivecraft/mcore/HashCodeComparator.java new file mode 100644 index 00000000..fc3c0346 --- /dev/null +++ b/src/com/massivecraft/mcore/HashCodeComparator.java @@ -0,0 +1,53 @@ +/* + NaturalOrderComparator.java -- Perform 'natural order' comparisons of strings in Java. + Copyright (C) 2003 by Pierre-Luc Paour + + Based on the C version by Martin Pool, of which this is more or less a straight conversion. + Copyright (C) 2000 by Martin Pool + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + */ + +/* +This version has been slightly modified for usage in the MCore library. +Check out the original at: https://github.com/paour/natorder/blob/master/NaturalOrderComparator.java +*/ + +package com.massivecraft.mcore; + +import java.util.*; + +public class HashCodeComparator implements Comparator +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static transient HashCodeComparator i = new HashCodeComparator(); + public static HashCodeComparator get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public int compare(Object o1, Object o2) + { + return o2.hashCode() - o1.hashCode(); + } + +} diff --git a/src/com/massivecraft/mcore/store/Coll.java b/src/com/massivecraft/mcore/store/Coll.java index 1189804a..54a9b165 100644 --- a/src/com/massivecraft/mcore/store/Coll.java +++ b/src/com/massivecraft/mcore/store/Coll.java @@ -13,6 +13,7 @@ import java.util.concurrent.ConcurrentSkipListSet; import org.bukkit.plugin.Plugin; +import com.massivecraft.mcore.HashCodeComparator; import com.massivecraft.mcore.MCore; import com.massivecraft.mcore.MPlugin; import com.massivecraft.mcore.NaturalOrderComparator; @@ -223,6 +224,21 @@ public class Coll implements CollInterface eto.load(efrom); eto.setCustomData(efrom.getCustomData()); } + else if (ofrom instanceof JsonObject) + { + JsonObject jfrom = (JsonObject)ofrom; + JsonObject jto = (JsonObject)oto; + // Clear To + for (Entry entry : jto.entrySet()) + { + jto.remove(entry.getKey()); + } + // Copy + for (Entry entry : jfrom.entrySet()) + { + jto.add(entry.getKey(), entry.getValue()); + } + } else { Accessor.get(this.getEntityClass()).copy(ofrom, oto); @@ -522,6 +538,7 @@ public class Coll implements CollInterface } } + @SuppressWarnings("unchecked") @Override public synchronized void loadFromRemote(Object oid) { @@ -550,11 +567,21 @@ public class Coll implements CollInterface Long mtime = entry.getValue(); if (mtime == null) return; + // Calculate temp but handle raw cases. + E temp = null; + if (this.getEntityClass().isAssignableFrom(JsonObject.class)) + { + temp = (E) raw; + } + else + { + temp = this.getGson().fromJson(raw, this.getEntityClass()); + } E entity = this.get(id, false); if (entity != null) { // It did already exist - this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity); + this.copy(temp, entity); } else { @@ -562,7 +589,7 @@ public class Coll implements CollInterface entity = this.createNewInstance(); // Copy over data first - this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity); + this.copy(temp, entity); // Then attach! this.attach(entity, oid, false); @@ -798,6 +825,11 @@ public class Coll implements CollInterface this.collDriverObject = db.getCollDriverObject(this); // STORAGE + if (entityComparator == null && !Comparable.class.isAssignableFrom(entityClass)) + { + // Avoid "Classname cannot be cast to java.lang.Comparable" error in ConcurrentSkipListMap + entityComparator = HashCodeComparator.get(); + } this.ids = new ConcurrentSkipListSet(idComparator); this.id2entity = new ConcurrentSkipListMap(idComparator); this.entity2id = new ConcurrentSkipListMap(entityComparator);