From aaac36b1cc328ae2213a3fac437dbc4e7a2d8624 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Mon, 30 Jan 2012 17:41:18 +0100 Subject: [PATCH] Factored lookup code from BlockType and ItemType into a method in StringUtil. --- src/main/java/com/sk89q/util/StringUtil.java | 36 +++++++++++++++++++ .../com/sk89q/worldedit/blocks/BlockType.java | 34 +----------------- .../com/sk89q/worldedit/blocks/ItemType.java | 34 +----------------- 3 files changed, 38 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/sk89q/util/StringUtil.java b/src/main/java/com/sk89q/util/StringUtil.java index c0f7b727b..b706cb633 100644 --- a/src/main/java/com/sk89q/util/StringUtil.java +++ b/src/main/java/com/sk89q/util/StringUtil.java @@ -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 lookup(Map 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 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; + } } diff --git a/src/main/java/com/sk89q/worldedit/blocks/BlockType.java b/src/main/java/com/sk89q/worldedit/blocks/BlockType.java index aebd29597..997bb6e61 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/BlockType.java +++ b/src/main/java/com/sk89q/worldedit/blocks/BlockType.java @@ -23,7 +23,6 @@ import java.util.HashSet; import java.util.Map; import java.util.HashMap; import java.util.EnumSet; -import java.util.Map.Entry; import java.util.Random; import java.util.Set; @@ -235,38 +234,7 @@ public enum BlockType { * @return */ public static BlockType lookup(String name, boolean fuzzy) { - String testName = name.replace(" ", "").toLowerCase(); - - if (testName.length() == 0) { - return null; - } - - BlockType type = lookup.get(testName); - - if (type != null) { - return type; - } - - if (!fuzzy) { - return null; - } - - int minDist = -1; - - for (Entry entry : lookup.entrySet()) { - if (entry.getKey().charAt(0) != testName.charAt(0)) { - continue; - } - - int dist = StringUtil.getLevenshteinDistance(entry.getKey(), testName); - - if ((dist < minDist || minDist == -1) && dist < 2) { - minDist = dist; - type = entry.getValue(); - } - } - - return type; + return StringUtil.lookup(lookup, name, fuzzy); } /** diff --git a/src/main/java/com/sk89q/worldedit/blocks/ItemType.java b/src/main/java/com/sk89q/worldedit/blocks/ItemType.java index e6f995d3f..89ed8cf7e 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/ItemType.java +++ b/src/main/java/com/sk89q/worldedit/blocks/ItemType.java @@ -24,7 +24,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.HashMap; import java.util.EnumSet; -import java.util.Map.Entry; import java.util.Set; import com.sk89q.util.StringUtil; @@ -413,38 +412,7 @@ public enum ItemType { * @return */ public static ItemType lookup(String name, boolean fuzzy) { - String testName = name.replace(" ", "").toLowerCase(); - - if (testName.length() == 0) { - return null; - } - - ItemType type = lookup.get(testName); - - if (type != null) { - return type; - } - - if (!fuzzy) { - return null; - } - - int minDist = -1; - - for (Entry entry : lookup.entrySet()) { - if (entry.getKey().charAt(0) != testName.charAt(0)) { - continue; - } - - int dist = StringUtil.getLevenshteinDistance(entry.getKey(), testName); - - if ((dist < minDist || minDist == -1) && dist < 2) { - minDist = dist; - type = entry.getValue(); - } - } - - return type; + return StringUtil.lookup(lookup, name, fuzzy); } /**