我有一个java枚举,必须有一些默认值,如
for the
String some default values
int some degault values
etc
我创建了一个类似下面的枚举
package com.hexgen.tools;
public enum DefaultParamsValues {
STRING ("HEXGEN"),
INTEGER(2013),
DATE(new org.joda.time.LocalDate()),
BOOLEAN(true),
BIGINTEGER(BigInteger.valueOf(Long.MAX_VALUE)),
LONG("1898.48");
private String defaultString;
private int defaultInteger;
private LocalDate defaultDate;
private boolean defaultBoolean;
private long defaultLong;
private BigInteger defaultBigInteger;
public DefaultParamsValues(String strValue,int intValue,LocalDate date,boolean booleanValue,long longValue,BigInteger bigintVlaue){
this.defaultString = strValue;
this.defaultInteger = intValue;
this.defaultDate = date;
this.defaultBoolean = booleanValue;
this.defaultLong=longValue;
this.defaultBigInteger = bigintVlaue;
}
}
但它提出了很多问题,有人会帮我创建一个包含基本类型基本值的枚举吗?
编辑:这就是我解决它的方式:package com.test.poc;
import java.math.BigInteger;
import org.joda.time.LocalDate;
public enum DefaultParamValues {
STRING("HEXGEN"),
INTEGER(123),
DATE(new LocalDate()),
BOOLEAN(true),
LONGVALUE(123123),
BIGINTEGER(BigInteger.valueOf(Long.MAX_VALUE));
private String defaultString;
private int defaultInteger;
private LocalDate defaultDate;
private boolean defaultBoolean;
private long defaultLong;
private BigInteger defaultBigInteger;
private DefaultParamValues(String strDefaultValue) {
defaultString = strDefaultValue;
}
private DefaultParamValues(int intDefaultValue) {
defaultInteger = intDefaultValue;
}
private DefaultParamValues(LocalDate dateDefaultValue) {
defaultDate = dateDefaultValue;
}
private DefaultParamValues(boolean booleanDefaultValue) {
defaultBoolean = booleanDefaultValue;
}
private DefaultParamValues(long longDefaultValue) {
defaultLong = longDefaultValue;
}
private DefaultParamValues(BigInteger bigIntegerDefaultValue) {
defaultBigInteger = bigIntegerDefaultValue;
}
public String getDefaultString() {
return defaultString;
}
public int getDefaultInt() {
return defaultInteger;
}
public LocalDate getDefaultDate() {
return defaultDate;
}
public boolean getDefaultBoolean() {
return defaultBoolean;
}
public long getDefaultLong() {
return defaultLong;
}
public BigInteger getDefaultBigInteger() {
return defaultBigInteger;
}
}
由于
答案 0 :(得分:2)
首先,枚举可能不适合您的目的。你应该使用带有常量的最终类。
但如果你想要它效率低下而且麻烦,你可以这样做:
public enum GenericEnum {
STRING("HEXGEN"),
INTEGER(2013),
DATE(new Date()),
BOOLEAN(true),
BIGINTEGER(BigInteger.valueOf(Long.MAX_VALUE)),
LONG("1898.48");
private String defaultString;
private int defaultInteger;
private Date defaultDate;
private boolean defaultBoolean;
private long defaultLong;
private BigInteger defaultBigInteger;
GenericEnum(Object value) {
if(String.class.isAssignableFrom(value.getClass())) {
this.defaultString = (String) value;
} else if (Integer.class.isAssignableFrom(value.getClass())) {
this.defaultInteger = (Integer) value;
}
[...]
}
}
或重载构造函数:
private GenericEnum(String val) {
this.defaultString = val;
}
private GenericEnum(int val) {
this.defaultInteger = val;
}
[...]