Possibly solved the faction creation issue. Changed the best id match to something more reasonable

This commit is contained in:
Olof Larsson 2011-10-23 12:07:20 +02:00
parent 32624e0339
commit 4f7fd6dd96
4 changed files with 43 additions and 26 deletions

View File

@ -146,7 +146,7 @@ public class Factions extends EntityCollection<Faction>
return null; return null;
} }
public Faction getBestTagMatch(String pattern) public Faction getBestTagMatch(String searchFor)
{ {
Map<String, Faction> tag2faction = new HashMap<String, Faction>(); Map<String, Faction> tag2faction = new HashMap<String, Faction>();
@ -156,7 +156,7 @@ public class Factions extends EntityCollection<Faction>
tag2faction.put(ChatColor.stripColor(faction.getTag()), faction); tag2faction.put(ChatColor.stripColor(faction.getTag()), faction);
} }
String tag = TextUtil.getWhereLongestCommonStartCI(tag2faction.keySet(), pattern); String tag = TextUtil.getBestStartWithCI(tag2faction.keySet(), searchFor);
if (tag == null) return null; if (tag == null) return null;
return tag2faction.get(tag); return tag2faction.get(tag);
} }

View File

@ -53,13 +53,13 @@ public class CmdCreate extends FCommand
sendMessage(tagValidationErrors); sendMessage(tagValidationErrors);
return; return;
} }
// if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay
if ( ! payForCommand(Conf.econCostCreate, "to create a new faction", "for creating a new faction")) return; if ( ! payForCommand(Conf.econCostCreate, "to create a new faction", "for creating a new faction")) return;
Faction faction = Factions.i.create(); Faction faction = Factions.i.create();
// TODO: Why would this even happen??? // TODO: Why would this even happen??? Auto increment clash??
if (faction == null) if (faction == null)
{ {
msg("<b>There was an internal error while trying to create your faction. Please try again."); msg("<b>There was an internal error while trying to create your faction. Please try again.");

View File

@ -97,7 +97,7 @@ public abstract class EntityCollection<E extends Entity>
public E getBestIdMatch(String pattern) public E getBestIdMatch(String pattern)
{ {
String id = TextUtil.getWhereLongestCommonStartCI(this.id2entity.keySet(), pattern); String id = TextUtil.getBestStartWithCI(this.id2entity.keySet(), pattern);
if (id == null) return null; if (id == null) return null;
return this.id2entity.get(id); return this.id2entity.get(id);
} }
@ -106,12 +106,12 @@ public abstract class EntityCollection<E extends Entity>
// CREATE // CREATE
// -------------------------------------------- // // -------------------------------------------- //
public E create() public synchronized E create()
{ {
return this.create(this.getNextId()); return this.create(this.getNextId());
} }
public E create(String id) public synchronized E create(String id)
{ {
if ( ! this.isIdFree(id)) return null; if ( ! this.isIdFree(id)) return null;
@ -225,21 +225,23 @@ public abstract class EntityCollection<E extends Entity>
public String getNextId() public String getNextId()
{ {
String next = Integer.toString(this.nextId); while ( ! isIdFree(this.nextId) )
do
{ {
this.nextId += 1; this.nextId += 1;
} while ( ! isIdFree(Integer.toString(this.nextId)) ); }
return Integer.toString(this.nextId);
return next;
} }
public boolean isIdFree(String id) public boolean isIdFree(String id)
{ {
return ! this.id2entity.containsKey(id); return ! this.id2entity.containsKey(id);
} }
public boolean isIdFree(int id)
{
return this.isIdFree(Integer.toString(id));
}
protected void fillIds() protected synchronized void fillIds()
{ {
this.nextId = 1; this.nextId = 1;
for(Entry<String, E> entry : this.id2entity.entrySet()) for(Entry<String, E> entry : this.id2entity.entrySet())
@ -251,16 +253,21 @@ public abstract class EntityCollection<E extends Entity>
} }
} }
protected synchronized void updateNextIdForId(int id)
{
if (this.nextId < id)
{
this.nextId = id + 1;
}
}
protected void updateNextIdForId(String id) protected void updateNextIdForId(String id)
{ {
try try
{ {
int idAsInt = Integer.parseInt(id); int idAsInt = Integer.parseInt(id);
if (this.nextId < idAsInt) this.updateNextIdForId(idAsInt);
{ }
this.nextId = idAsInt + 1; catch (Exception ignored) { }
}
} catch (Exception ignored) {}
} }
} }

View File

@ -285,7 +285,7 @@ public class TextUtil
// String comparison // String comparison
// -------------------------------------------- // // -------------------------------------------- //
public static int commonStartLength(String a, String b) /*private static int commonStartLength(String a, String b)
{ {
int len = a.length() < b.length() ? a.length() : b.length(); int len = a.length() < b.length() ? a.length() : b.length();
int i; int i;
@ -294,19 +294,29 @@ public class TextUtil
if (a.charAt(i) != b.charAt(i)) break; if (a.charAt(i) != b.charAt(i)) break;
} }
return i; return i;
} }*/
public static String getWhereLongestCommonStartCI(Collection<String> candidates, String pattern) public static String getBestStartWithCI(Collection<String> candidates, String start)
{ {
String ret = null; String ret = null;
int best = 0; int best = 0;
pattern = pattern.toLowerCase();
start = start.toLowerCase();
int minlength = start.length();
for (String candidate : candidates) for (String candidate : candidates)
{ {
int csl = commonStartLength(pattern, candidate.toLowerCase()); if (candidate.length() < minlength) continue;
if (csl > best) if ( ! candidate.toLowerCase().startsWith(start)) continue;
// The closer to zero the better
int lendiff = candidate.length() - minlength;
if (lendiff == 0)
{ {
best = csl; return candidate;
}
if (lendiff < best ||best == 0)
{
best = lendiff;
ret = candidate; ret = candidate;
} }
} }