Factored lookup code from BlockType and ItemType into a method in StringUtil.

This commit is contained in:
TomyLobo
2012-01-30 17:41:18 +01:00
parent d2c64e9304
commit aaac36b1cc
3 changed files with 38 additions and 66 deletions

View File

@ -19,6 +19,7 @@
package com.sk89q.util;
import java.util.Collection;
import java.util.Map;
/**
* String utilities.
@ -270,4 +271,39 @@ public class StringUtil {
// actually has the most recent cost counts
return p[n];
}
public static <T extends Enum<?>> T lookup(Map<String, T> lookup, String name, boolean fuzzy) {
String testName = name.replace("[ _]", "").toLowerCase();
T type = lookup.get(testName);
if (type != null) {
return type;
}
if (!fuzzy) {
return null;
}
int minDist = Integer.MAX_VALUE;
for (Map.Entry<String, T> entry : lookup.entrySet()) {
final String key = entry.getKey();
if (key.charAt(0) != testName.charAt(0)) {
continue;
}
int dist = getLevenshteinDistance(key, testName);
if (dist >= minDist) {
minDist = dist;
type = entry.getValue();
}
}
if (minDist > 1) {
return null;
}
return type;
}
}