Bugfixes and removed the matcher method within the coll.

This commit is contained in:
Olof Larsson 2012-08-31 18:13:41 +02:00
parent f706ea7ac2
commit e286f923b1
10 changed files with 120 additions and 60 deletions

View File

@ -27,7 +27,7 @@ public class AHPlayer extends AHBase<Player>
}
else
{
Player player = Bukkit.getServer().getPlayer(str);
Player player = Bukkit.getServer().getPlayerExact(str);
if (player != null)
{
return player;

View File

@ -4,8 +4,8 @@ import org.bukkit.command.CommandSender;
import com.massivecraft.mcore4.MPlugin;
import com.massivecraft.mcore4.persist.IClassManager;
import com.massivecraft.mcore4.persist.Persist;
import com.massivecraft.mcore4.util.PlayerUtil;
import com.massivecraft.mcore4.util.Txt;
public abstract class AHPlayerWrapper<T> extends AHBase<T>
{
@ -36,7 +36,7 @@ public abstract class AHPlayerWrapper<T> extends AHBase<T>
}
else if (style != null && style.equalsIgnoreCase("matchany"))
{
ret = manager.get(Persist.getBestCIStart(PlayerUtil.getAllVisitorNames(), str));
ret = manager.get(Txt.getBestCIStart(PlayerUtil.getAllVisitorNames(), str));
if (ret != null)
{
return ret;

View File

@ -0,0 +1,63 @@
package com.massivecraft.mcore4.cmd.arg;
import java.util.Collection;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore4.MPlugin;
import com.massivecraft.mcore4.store.Coll;
import com.massivecraft.mcore4.util.PlayerUtil;
import com.massivecraft.mcore4.util.Txt;
public class AHPlayerWrapperNew<E> extends AHBase<E>
{
final Coll<E, ?> coll;
public Coll<E, ?> coll() { return this.coll; };
public AHPlayerWrapperNew(Coll<E, ?> coll)
{
this.coll = coll;
}
@Override
public E parse(String str, String style, CommandSender sender, MPlugin p)
{
this.error.clear();
if (str == null) return null;
E ret;
if (style != null && style.equalsIgnoreCase("match"))
{
@SuppressWarnings("unchecked")
Collection<String> ids = (Collection<String>) this.coll().ids();
String id = Txt.getBestCIStart(ids, str);
ret = this.coll().get(id);
if (ret != null)
{
return ret;
}
this.error.add("<b>No player name begins with \"<p>"+str+"<b>\".");
}
else if (style != null && style.equalsIgnoreCase("matchany"))
{
ret = this.coll().get(Txt.getBestCIStart(PlayerUtil.getAllVisitorNames(), str));
if (ret != null)
{
return ret;
}
this.error.add("<b>No player name begins with \"<p>"+str+"<b>\".");
}
else
{
ret = this.coll().get(str);
if (ret != null)
{
return ret;
}
this.error.add("<b>No player with the exact name \"<p>"+str+"<b>\".");
}
return null;
}
}

View File

@ -150,33 +150,6 @@ public class Persist
return new ArrayList<T>(ret.subList(fromIndex, toIndex));
}
public static String getBestCIStart(Collection<String> candidates, String start)
{
String ret = null;
int best = 0;
start = start.toLowerCase();
int minlength = start.length();
for (String candidate : candidates)
{
if (candidate.length() < minlength) continue;
if ( ! candidate.toLowerCase().startsWith(start)) continue;
// The closer to zero the better
int lendiff = candidate.length() - minlength;
if (lendiff == 0)
{
return candidate;
}
if (lendiff < best || best == 0)
{
best = lendiff;
ret = candidate;
}
}
return ret;
}
// http://stackoverflow.com/questions/2864840/treemap-sort-by-value
public static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map, final boolean ascending)
{

View File

@ -15,6 +15,7 @@ import com.massivecraft.mcore4.Predictate;
import com.massivecraft.mcore4.persist.IClassManager;
import com.massivecraft.mcore4.persist.Persist;
import com.massivecraft.mcore4.util.DiscUtil;
import com.massivecraft.mcore4.util.Txt;
import com.massivecraft.mcore4.xlib.gson.Gson;
public abstract class GsonClassManager<T> implements IClassManager<T>
@ -489,7 +490,7 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
{
String start = this.idFix(oid);
if (start == null) return null;
String id = Persist.getBestCIStart(this.ids, start);
String id = Txt.getBestCIStart(this.ids, start);
return this.get(id);
}
}

View File

@ -95,13 +95,6 @@ public class Coll<E, L> implements CollInterface<E, L>
return this.create(id, noteChange);
}
@Override
public E getBestMatch(Object oid)
{
// TODO Auto-generated method stub
return null;
}
// Get the id for this entity.
protected Map<E, L> entity2id = new ConcurrentHashMap<E, L>();
@Override public Map<E, L> entity2id() { return Collections.unmodifiableMap(this.entity2id); }
@ -389,37 +382,46 @@ public class Coll<E, L> implements CollInterface<E, L>
boolean existsLocal = (localEntity != null);
boolean existsRemote = (remoteMtime != null);
if ( ! existsLocal && ! existsRemote) return ModificationState.UNKNOWN;
if (existsLocal && existsRemote)
{
Long lastMtime = this.lastMtime.get(id);
if (remoteMtime.equals(lastMtime) == false) return ModificationState.REMOTE_ALTER;
Object lastRaw = this.lastRaw.get(id);
Object currentRaw = this.storeAdapter.read(this, localEntity);
if (currentRaw.equals(lastRaw) == false) return ModificationState.LOCAL_ALTER;
if (this.examineHasLocalAlter(id, localEntity)) return ModificationState.LOCAL_ALTER;
}
else if (existsLocal)
{
if ( ! this.lastDefault.contains(id)) return ModificationState.REMOTE_DETACH;
if (this.lastDefault.contains(id))
{
if (this.examineHasLocalAlter(id, localEntity)) return ModificationState.LOCAL_ALTER;
}
else
{
return ModificationState.REMOTE_DETACH;
}
}
else if (existsRemote)
{
return ModificationState.REMOTE_ATTACH;
}
else if ( ! existsLocal && ! existsRemote)
{
return ModificationState.UNKNOWN;
}
return ModificationState.NONE;
}
protected boolean examineHasLocalAlter(L id, E entity)
{
Object lastRaw = this.lastRaw.get(id);
Object currentRaw = this.storeAdapter.read(this, entity);
return (currentRaw.equals(lastRaw) == false);
}
@Override
public void syncId(L id)
{
ModificationState mstate = this.examineId(id);
//System.out.println("syncId \""+id+"\" "+mstate);
//mplugin.log("syncId: It seems", id, "has state", mstate);
switch (mstate)
{
@ -480,6 +482,7 @@ public class Coll<E, L> implements CollInterface<E, L>
{
Long remoteMtime = id2RemoteMtime.get(id);
ModificationState state = this.examineId(id, remoteMtime);
//mplugin.log("findSuspects: It seems", id, "has state", state);
if (state.modified())
{
//System.out.println("It seems "+id+" has state "+state);

View File

@ -44,7 +44,6 @@ public interface CollInterface<E, L>
public Map<L, E> id2entity();
public E get(Object oid);
public E get(Object oid, boolean creative);
public E getBestMatch(Object oid);
public Map<E, L> entity2id();
public L id(E entity);

View File

@ -36,20 +36,41 @@ public abstract class Entity<E extends Entity<E, L>, L>
return ! this.attached();
}
// TODO: Mark as dirty.
// TODO: Perhaps even brute force methods to save or load from remote.
/*public boolean save()
{
return this.getColl().saveEntity(getThis());
}*/
public L getId()
{
if (this.getColl() == null) return null;
return this.getColl().id(getThis());
}
// TODO: Perhaps even brute force methods to save or load from remote.
public void changed()
{
L id = this.getId();
if (id == null) return;
this.getColl().changedIds.add(id);
}
public void sync()
{
L id = this.getId();
if (id == null) return;
this.getColl().syncId(id);
}
public void saveToRemote()
{
L id = this.getId();
if (id == null) return;
this.getColl().saveToRemote(id);
}
public void loadFromRemote()
{
L id = this.getId();
if (id == null) return;
this.getColl().loadFromRemote(id);
}
@Override
public String toString()
{

View File

@ -25,7 +25,7 @@ public class ExamineThread<E, L> extends Thread
//long after = System.currentTimeMillis();
//XCore.p.log(this.getName()+ " complete. Took "+ (after-before) +"ms.");
//coll.mplugin().log(this.getName()+ " complete. Took "+ (after-before) +"ms.");
Thread.sleep(5000);
}

View File

@ -27,6 +27,7 @@ public class MStore
{
try
{
if (uri.equalsIgnoreCase("default")) return MCore.getDb();
return getDb(new URI(uri));
}
catch (URISyntaxException e)
@ -39,7 +40,6 @@ public class MStore
public static Db<?> getDb(URI uri)
{
String scheme = uri.getScheme();
if (scheme.equalsIgnoreCase("default")) return MCore.getDb();
Driver<?> driver = getDriver(scheme);
if (driver == null) return null;
return driver.db(uri.toString());