如何最好地将Map <string,foo>转换为Map <myenum,foo> in wrapper </myenum,foo> </string,foo>

时间:2013-12-19 11:21:08

标签: java design-patterns enums hashmap code-organization

更新,添加评论中的问题:

  

将Map映射到Map的最佳方法是什么   Foo是一个Enum。从概念上讲,它看起来非常复杂   不是一个复杂的想法。

此代码起作用,它是简单MUD client的一部分。基本思想是Monitor保存数据(生命点等),这些数据将在游戏过程中显示给用户,可能是在辅助JPanel中;这些方面的东西。此报告的可能值位于Attribs Enum

在走太远之前,当我查看Monitor类以及创建单个实例所需的内容时,它似乎有点冗长。

如果我将convertToEnumEntry移出Monitor,那么该课程将无效。所以,班级不能变小,我不认为。是否有我可以使用的模式(或使用更好)?

此类中的所有内容都基于以下假设:如果convertToEnumEntry返回对Map.Entry的空引用,然后获取一个不适合{{1}的特定String }}:

Attribs

与其他键不同,敌人可以是任何enemy = stringEntry.getKey();。其他键仅限于String(属性)的Enum值。

Attribs

将所有内容都塞进一个类来解析和构建public class Monitor { private static Logger log = Logger.getLogger(Monitor.class.getName()); private Map<Attribs, Ratio> mapOfAttributes = new HashMap<>(); private String enemy = null; private Monitor() { } public Monitor(Map<String, Ratio> mapStringToRatio) { init(mapStringToRatio); } private void init(Map<String, Ratio> mapStringToRatio) { SimpleEntry<Attribs, Ratio> attribsEntry = null; for (Entry<String, Ratio> stringEntry : mapStringToRatio.entrySet()) { attribsEntry = null; attribsEntry = convertToEnumEntry(stringEntry); if (attribsEntry != null) { mapOfAttributes.put(attribsEntry.getKey(), attribsEntry.getValue()); } else { enemy = stringEntry.getKey(); //assumes key is enemy value mapOfAttributes.put(Attribs.ENEMY, stringEntry.getValue()); } } } private SimpleEntry<Attribs, Ratio> convertToEnumEntry(Entry<String, Ratio> stringEntry) { Ratio ratio = stringEntry.getValue(); SimpleEntry<Attribs, Ratio> attribEntry = null; for (Attribs attribute : Attribs.values()) { if (stringEntry.getKey().equalsIgnoreCase(attribute.name())) { attribEntry = new HashMap.SimpleEntry<>(attribute, ratio); } } return attribEntry; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("\nfighting\t\t" + enemy + "\n"); for (Map.Entry<Attribs, Ratio> e : mapOfAttributes.entrySet()) { sb.append("\n"); sb.append(e.getKey().name()); sb.append("\t"); sb.append(e.getValue().toString()); } sb.append("\n"); return sb.toString(); } } ,或将所有内容放入Map中是很诱人的。目前,Monitor是一种中间立场,因为它完成了大部分艰苦工作,但并不能完全建立一个完整的RegexMapBuilder

Monitor

为简洁起见,我省略了public class RegexMapBuilder { public static Map<String, Ratio> stringToRatiosMap(String input) { Map<String, Ratio> mapOfStringsToRatios = new HashMap<>(); Map<String, String> strings = stringMap(input); Pattern fraction = Pattern.compile("(\\d+)/(\\d+)"); Pattern wholeNumber = Pattern.compile("(\\d+)"); Pattern percent = Pattern.compile("(\\d+)%"); Matcher matcher; int numerator, denominator; Ratio ratio = null; //need numerator and denominator values for (Entry<String, String> e : strings.entrySet()) { matcher = wholeNumber.matcher(e.getValue()); while (matcher.find()) { numerator = Integer.parseInt(matcher.group(1)); denominator = 1; ratio = new Ratio(numerator, denominator); } matcher = fraction.matcher(e.getValue()); while (matcher.find()) { numerator = Integer.parseInt(matcher.group(1)); denominator = Integer.parseInt(matcher.group(2)); ratio = new Ratio(numerator, denominator); } matcher = percent.matcher(e.getValue()); while (matcher.find()) { numerator = Integer.parseInt(matcher.group(1)); denominator = 100; ratio = new Ratio(numerator, denominator); } mapOfStringsToRatios.put(e.getKey(), ratio); } return mapOfStringsToRatios; } private static Map<String, String> stringMap(String input) { Map<String, String> strings = new HashMap<>(); Pattern pattern = Pattern.compile("(\\w+): +(\\S+)"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { strings.put(matcher.group(1), matcher.group(2)); } return strings; } } ,但Enum只是此报告打印的一些属性的列表。唯一棘手的部分是有一个值实际上不是Enum类型。

1 个答案:

答案 0 :(得分:1)

我认为java.util.EnumMap可能正是您所寻找的。你可以使用Enum#valueOf从给定的字符串中查找枚举值,就像Sam Yonnou提到的那样。