I have a number of setter methods which take an enum. These are based on incoming objects attribute. Rather than write a bunch of these is there a way around having to hard code say 10 different case statements. Would there be a way to create a reusable method?
我有一些设置枚举的setter方法。它们基于传入对象属性。与其写一堆这样的东西,还不如用硬编码,比如10个不同的case语句。是否有方法创建可重用的方法?
//Side class declared as
public final enum Side
//How I initialise side
static Side side = Side.SELL;//default
//method to set object
Obj.setSide(sideEnum(zasAlloc.getM_buySellCode()));
//How I am implementing it
public static Side sideEnum(String buysell)
{
if(buysell.equalsIgnoreCase("S"))
{
side = Side.SELL; //default
}
else if(buysell.equalsIgnoreCase("B"))
{
side = Side.BUY;
}
return side;
}
4 个解决方案
#1
25
You can implement that functionality in your Enum
.
您可以在Enum中实现该功能。
public enum Side {
BUY("B"), SELL("S"), ...
private String letter;
private Side(String letter) {
this.letter = letter;
}
public static Side fromLetter(String letter) {
for (side s : values() ){
if (s.letter.equals(letter)) return s;
}
return null;
}
}
You could also do this as a helper static method if you can't edit Side
.
如果不能编辑边,也可以将其作为助手静态方法。
public static Side fromString(String from) {
for (Side s: Side.values()) {
if (s.toString().startsWith(from)) {
return s;
}
}
throw new IllegalArgumentException( from );
}
The above method assumes your strings correspond to the names of you enums.
上面的方法假设您的字符串对应于您的枚举名。
#2
5
Enums have valueOf() method that can be used to convert from String. Is it what you are looking for?
枚举具有valueOf()方法,可用于从字符串转换。这就是你要找的吗?
#3
2
I ended up using a simple object map:
最后我使用了一个简单的对象映射:
private static HashMap<String, Side> sideMap = new HashMap<String, Side>(7);
static{
sideMap.put("B", Side.BUY);
sideMap.put("S", Side.SELL);
}
and simply using
并简单地使用
Obj.setSide(sideMap.get(zasAlloc.getM_buySellCode()));
#4
0
I think you need smth like:
我认为你需要像:
Obj.setSide(Side.valueOf(zasAlloc.getM_buySellCode()));
#1
25
You can implement that functionality in your Enum
.
您可以在Enum中实现该功能。
public enum Side {
BUY("B"), SELL("S"), ...
private String letter;
private Side(String letter) {
this.letter = letter;
}
public static Side fromLetter(String letter) {
for (side s : values() ){
if (s.letter.equals(letter)) return s;
}
return null;
}
}
You could also do this as a helper static method if you can't edit Side
.
如果不能编辑边,也可以将其作为助手静态方法。
public static Side fromString(String from) {
for (Side s: Side.values()) {
if (s.toString().startsWith(from)) {
return s;
}
}
throw new IllegalArgumentException( from );
}
The above method assumes your strings correspond to the names of you enums.
上面的方法假设您的字符串对应于您的枚举名。
#2
5
Enums have valueOf() method that can be used to convert from String. Is it what you are looking for?
枚举具有valueOf()方法,可用于从字符串转换。这就是你要找的吗?
#3
2
I ended up using a simple object map:
最后我使用了一个简单的对象映射:
private static HashMap<String, Side> sideMap = new HashMap<String, Side>(7);
static{
sideMap.put("B", Side.BUY);
sideMap.put("S", Side.SELL);
}
and simply using
并简单地使用
Obj.setSide(sideMap.get(zasAlloc.getM_buySellCode()));
#4
0
I think you need smth like:
我认为你需要像:
Obj.setSide(Side.valueOf(zasAlloc.getM_buySellCode()));