package frame.variant;
|
|
import java.math.BigDecimal;
|
import java.text.ParseException;
|
import java.util.Date;
|
|
import org.apache.log4j.Logger;
|
|
import frame.util.Util;
|
import frame.variant.translator.Translator;
|
|
public class Value {
|
|
private Object data;
|
private ValueType type;
|
private static Logger logger;
|
|
|
static {
|
logger = Logger.getLogger(Value.class);
|
}
|
|
public Value() {
|
data = null;
|
}
|
|
public Value(Boolean value) {
|
setData(value);
|
}
|
|
public Value(Integer value) {
|
setData(value);
|
}
|
|
public Value(Long value) {
|
setData(value);
|
}
|
|
public Value(Double value) {
|
setData(value);
|
}
|
|
public Value(BigDecimal value) {
|
setData(value);
|
}
|
|
public Value(Date value) {
|
setData(value);
|
}
|
|
public Value(String value) {
|
setData(value);
|
}
|
|
public void setData(Boolean value) {
|
this.data = value;
|
type = ValueType.Int;
|
}
|
|
public void setData(Integer value) {
|
this.data = value;
|
type = ValueType.Int;
|
}
|
|
public void setData(Long value) {
|
this.data = value;
|
type = ValueType.Long;
|
}
|
|
public void setData(Double value) {
|
this.data = value;
|
type = ValueType.Double;
|
}
|
|
public void setData(BigDecimal value) {
|
this.data = value;
|
type = ValueType.Decimal;
|
}
|
|
public void setData(Date value) {
|
this.data = value;
|
type = ValueType.Date;
|
}
|
|
public void setData(String value) {
|
if ("null".equalsIgnoreCase(value)) {
|
this.data = null;
|
}
|
else {
|
this.data = value;
|
}
|
|
type = ValueType.String;
|
}
|
|
public void setData(Object value, ValueType type) {
|
this.data = value;
|
this.type = type;
|
}
|
|
public ValueType getType() {
|
return type;
|
}
|
|
public Integer getInt() {
|
Integer result = null;
|
|
if (data != null) {
|
Class<?> clazz = data.getClass();
|
|
if (clazz == Integer.class) {
|
result = (Integer)data;
|
}
|
else if (clazz == BigDecimal.class) {
|
result = ((BigDecimal)data).intValue();
|
}
|
else if (clazz == String.class) {
|
result = Integer.parseInt((String)data);
|
}
|
else {
|
String value_str = String.valueOf(data);
|
result = Integer.parseInt(value_str);
|
}
|
}
|
else {
|
result = 0;
|
}
|
|
return result;
|
}
|
|
public int getInt(int defaultValue) {
|
if (data == null) {
|
return defaultValue;
|
}
|
|
Integer result = defaultValue;
|
|
try {
|
result = getInt();
|
|
if (result == null) {
|
return defaultValue;
|
}
|
}
|
catch(Exception e) {
|
return defaultValue;
|
}
|
|
return result;
|
}
|
|
public Double getDouble() {
|
if (type == ValueType.Int) {
|
return Double.valueOf((Integer)data);
|
}
|
else {
|
try {
|
return (Double)data;
|
}
|
catch(Exception e) {
|
return Double.valueOf(String.valueOf(data));
|
}
|
}
|
}
|
|
public BigDecimal getBigDecimal() {
|
if (type == ValueType.Int) {
|
return BigDecimal.valueOf((Integer)data);
|
}
|
else {
|
try {
|
return BigDecimal.valueOf((Double)data);
|
}
|
catch(Exception e) {
|
return BigDecimal.valueOf(Double.valueOf(String.valueOf(data)));
|
}
|
}
|
}
|
|
public Long getLong() {
|
return (Long) data;
|
}
|
|
public Date getDate() throws ParseException {
|
Date result = null;
|
|
if (ValueType.Date == type) {
|
result = (Date)data;
|
}
|
else if (ValueType.String == type) {
|
result = Util.StringToDate(data.toString());
|
}
|
else if (ValueType.Int == type) {
|
logger.error("can not parse int to date");
|
}
|
else if (ValueType.Double == type) {
|
logger.error("can not parse double to date");
|
}
|
|
return result;
|
}
|
|
public java.sql.Timestamp getSqlDate() {
|
return new java.sql.Timestamp(((Date)data).getTime());
|
}
|
|
public String getString() {
|
return Translator.toString(data, null);
|
}
|
|
public String getString(String defaultValue) {
|
if (data != null) {
|
if (ValueType.Date == type) {
|
String result = data.toString();
|
if (result.length() > 15) {
|
return result.substring(0, 16);
|
}
|
}
|
|
return data.toString();
|
}
|
else {
|
return defaultValue;
|
}
|
}
|
|
public boolean getBoolean() {
|
String str = getString();
|
|
boolean result = "T".equalsIgnoreCase(str);
|
result = result || "True".equalsIgnoreCase(str);
|
result = result || "Y".equalsIgnoreCase(str);
|
result = result || "Yes".equalsIgnoreCase(str);
|
|
return result;
|
}
|
|
public boolean getBoolean(boolean defaultValue) {
|
String str = getString();
|
|
if (Util.isEmptyStr(str)) {
|
return defaultValue;
|
}
|
|
boolean result = "T".equalsIgnoreCase(str);
|
result = result || "True".equalsIgnoreCase(str);
|
result = result || "Y".equalsIgnoreCase(str);
|
result = result || "Yes".equalsIgnoreCase(str);
|
|
return result;
|
}
|
|
public Object getData() {
|
return data;
|
}
|
|
public boolean isNull() {
|
return data == null;
|
}
|
|
public boolean isEmpty() {
|
String str = getString();
|
return Util.isEmptyStr(str);
|
}
|
|
public void setNull() {
|
data = null;
|
}
|
|
public void clear() {
|
data = null;
|
}
|
|
public String toString() {
|
return getString();
|
}
|
|
}
|