Work on supporting raw JsonObjects
This commit is contained in:
parent
66de46f0c9
commit
058cc49720
53
src/com/massivecraft/mcore/HashCodeComparator.java
Normal file
53
src/com/massivecraft/mcore/HashCodeComparator.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
NaturalOrderComparator.java -- Perform 'natural order' comparisons of strings in Java.
|
||||||
|
Copyright (C) 2003 by Pierre-Luc Paour <natorder@paour.com>
|
||||||
|
|
||||||
|
Based on the C version by Martin Pool, of which this is more or less a straight conversion.
|
||||||
|
Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
|
||||||
|
|
||||||
|
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<Object>
|
||||||
|
{
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -13,6 +13,7 @@ import java.util.concurrent.ConcurrentSkipListSet;
|
|||||||
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import com.massivecraft.mcore.HashCodeComparator;
|
||||||
import com.massivecraft.mcore.MCore;
|
import com.massivecraft.mcore.MCore;
|
||||||
import com.massivecraft.mcore.MPlugin;
|
import com.massivecraft.mcore.MPlugin;
|
||||||
import com.massivecraft.mcore.NaturalOrderComparator;
|
import com.massivecraft.mcore.NaturalOrderComparator;
|
||||||
@ -223,6 +224,21 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
eto.load(efrom);
|
eto.load(efrom);
|
||||||
eto.setCustomData(efrom.getCustomData());
|
eto.setCustomData(efrom.getCustomData());
|
||||||
}
|
}
|
||||||
|
else if (ofrom instanceof JsonObject)
|
||||||
|
{
|
||||||
|
JsonObject jfrom = (JsonObject)ofrom;
|
||||||
|
JsonObject jto = (JsonObject)oto;
|
||||||
|
// Clear To
|
||||||
|
for (Entry<String, JsonElement> entry : jto.entrySet())
|
||||||
|
{
|
||||||
|
jto.remove(entry.getKey());
|
||||||
|
}
|
||||||
|
// Copy
|
||||||
|
for (Entry<String, JsonElement> entry : jfrom.entrySet())
|
||||||
|
{
|
||||||
|
jto.add(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Accessor.get(this.getEntityClass()).copy(ofrom, oto);
|
Accessor.get(this.getEntityClass()).copy(ofrom, oto);
|
||||||
@ -522,6 +538,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public synchronized void loadFromRemote(Object oid)
|
public synchronized void loadFromRemote(Object oid)
|
||||||
{
|
{
|
||||||
@ -550,11 +567,21 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
Long mtime = entry.getValue();
|
Long mtime = entry.getValue();
|
||||||
if (mtime == null) return;
|
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);
|
E entity = this.get(id, false);
|
||||||
if (entity != null)
|
if (entity != null)
|
||||||
{
|
{
|
||||||
// It did already exist
|
// It did already exist
|
||||||
this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity);
|
this.copy(temp, entity);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -562,7 +589,7 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
entity = this.createNewInstance();
|
entity = this.createNewInstance();
|
||||||
|
|
||||||
// Copy over data first
|
// Copy over data first
|
||||||
this.copy(this.getGson().fromJson(raw, this.getEntityClass()), entity);
|
this.copy(temp, entity);
|
||||||
|
|
||||||
// Then attach!
|
// Then attach!
|
||||||
this.attach(entity, oid, false);
|
this.attach(entity, oid, false);
|
||||||
@ -798,6 +825,11 @@ public class Coll<E> implements CollInterface<E>
|
|||||||
this.collDriverObject = db.getCollDriverObject(this);
|
this.collDriverObject = db.getCollDriverObject(this);
|
||||||
|
|
||||||
// STORAGE
|
// 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<String>(idComparator);
|
this.ids = new ConcurrentSkipListSet<String>(idComparator);
|
||||||
this.id2entity = new ConcurrentSkipListMap<String, E>(idComparator);
|
this.id2entity = new ConcurrentSkipListMap<String, E>(idComparator);
|
||||||
this.entity2id = new ConcurrentSkipListMap<E, String>(entityComparator);
|
this.entity2id = new ConcurrentSkipListMap<E, String>(entityComparator);
|
||||||
|
Loading…
Reference in New Issue
Block a user