更新,从留言中加入了一个问题:如何最好地转换地图<String,Foo>到地图<MyEnum,Foo>在包装
什么来获得地图进入地图, 富是一个枚举的最佳方式。在概念上,它似乎非常复杂,不是一个复杂的想法。
此代码功能,它是一个简单的MUD client的一部分。基本思想是Monitor
保存数据(生命值等),这些数据将在游戏过程中显示给用户,可能在次要的JPanel
;沿着这些线路。此报告的可能值在Attribs
中为Enum
。
在沿着这条路走得太远之前,当我看到Monitor
类以及创建单个实例需要多少时间时,它看起来不仅仅有点冗长。
如果我将convertToEnumEntry
从Monitor
中移出,那么该类将不起作用。所以,班级不能变小,我不这么认为。有没有我可以使用的模式(或使用更好)?
一切都在这个类停留在假设,如果convertToEnumEntry
返回空引用到Map.Entry
来再拿到一个,具体String
不适合Attribs
:
enemy = stringEntry.getKey();
不像其他键,敌人可以是任何String
。其他键仅限于Attribs
(属性)的Enum
值。
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
,但Attribs
是只是本报告打印的一些属性列表。唯一棘手的部分是有一个值不是真正的枚举类型。
你的问题是什么? –
什么是将地图转换为地图的最佳方式其中Foo是Enum。在概念上,它似乎非常复杂,而不是一个复杂的想法。 –
Thufir
为什么不使用'Attribs attr = Attribs.valueOf(stringValue.toUpperCase());'为'String - > Attribs'算法? – SamYonnou