If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Thursday, September 17, 2009

Getting the most out of Enums

Below an enum I created for some project...

Shoot and give comments !!



public enum DataType{

String(
"java.lang.String",
"String",
Operator.EQUALS,
Operator.CONTAINS,
Operator.IS_EMPTY,
Operator.IS_NULL,
Operator.IN
) {
public Object newInstance(Object value) {
String object = null;
if (value == null) {
return object;
}
try {
object = java.lang.String.valueOf(value.toString());
} catch (Exception e) { }
return object;
}
},
Integer(
"java.lang.Integer",
"Integer",
Operator.EQUALS,
Operator.IS_NULL,
Operator.GT,
Operator.GTE,
Operator.LT,
Operator.LTE,
Operator.BETWEEN
) {
public Object newInstance(Object value) {
Integer object = null;
if (value == null) {
return object;
}
try {
object = java.lang.Integer.valueOf(value.toString());
} catch (Exception e) { }
return object;
}
},
Long(
"java.lang.Long",
"Long"
) {

public Object newInstance(Object value) {
Long object = null;
if (value == null) {
return object;
}
try {
object = java.lang.Long.valueOf(value.toString());
} catch (Exception e) { }
return object;
}
},
Double(
"java.lang.Double",
"Double",
Operator.EQUALS,
Operator.IS_NULL,
Operator.GT,
Operator.GTE,
Operator.LT,
Operator.LTE,
Operator.BETWEEN
) {
public Object newInstance(Object value) {
Double object = null;
if (value == null) {
return object;
}
try {
object = java.lang.Double.valueOf(value.toString());
} catch (Exception e) { }
return object;
}
},
Date(
"java.util.Date",
"Date"
) {
public Object newInstance(Object value) {
Long time = null;
Date object = null;
if (value == null) {
return object;
}
try {
time = java.lang.Long.valueOf(value.toString());
object = new Date(time);
} catch (Exception e) { }
return object;
}
},
Character(
"java.lang.Character",
"Character"
) {
public Object newInstance(Object value) {
Character object = null;
if (value == null) {
return object;
}
try {
String stringvalue = value.toString();
if (stringvalue.length() == 1 ) {
object = new Character(stringvalue.charAt(0));
}
} catch (Exception e) { }
return object;
}
},
BigDecimal(
"java.math.BigDecimal",
"BigDecimal"
) {
public Object newInstance(Object value) {
BigDecimal object = null;
if (value == null) {
return object;
}
try {
Double doubleValue = java.lang.Double.valueOf(value.toString());
object = java.math.BigDecimal.valueOf(doubleValue);
} catch (Exception e) { }
return object;
}
},
Boolean(
"java.lang.Boolean",
"Boolean"
) {
public Object newInstance(Object value) {
Boolean object = null;
if (value == null) {
return object;
}
try {
object = java.lang.Boolean.valueOf(value.toString());
} catch (Exception e) { }
return object;
}
};

private String fullName;
private String shortName;
private List validOperators;

DataType(String fullName, String shortName, Operator...operators) {
this.fullName = fullName;
this.shortName = shortName;
validOperators = new ArrayList();
for (Operator operator : operators) {
validOperators.add(operator);
}
}

public String getFullName() {
return this.fullName;
}

public String getShortName() {
return this.shortName;
}

public List getValidOperators() {
return this.validOperators;
}

public abstract Object newInstance(Object value);

public String toString() {
return this.shortName;
}

}

No comments:

Post a Comment