summaryrefslogtreecommitdiffstats
path: root/src/com/iciql/util
diff options
context:
space:
mode:
authorJames Moger <james.moger@gmail.com>2011-08-04 22:22:24 -0400
committerJames Moger <james.moger@gmail.com>2011-08-04 22:22:24 -0400
commita1ab11053107c8995b3f3e850fa14a2374c2013a (patch)
tree750b96cfd386187d2086760d9e1b716e77f785db /src/com/iciql/util
parentb055a2a49335c78fdc754e38a7e8ab863b2a5515 (diff)
downloadiciql-a1ab11053107c8995b3f3e850fa14a2374c2013a.tar.gz
iciql-a1ab11053107c8995b3f3e850fa14a2374c2013a.zip
Customizable enumId() mapping (issue 2)
Diffstat (limited to 'src/com/iciql/util')
-rw-r--r--src/com/iciql/util/Utils.java45
1 files changed, 29 insertions, 16 deletions
diff --git a/src/com/iciql/util/Utils.java b/src/com/iciql/util/Utils.java
index 39875fb..302dd4d 100644
--- a/src/com/iciql/util/Utils.java
+++ b/src/com/iciql/util/Utils.java
@@ -37,6 +37,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
+import com.iciql.Iciql.EnumId;
+import com.iciql.Iciql.EnumType;
import com.iciql.IciqlException;
/**
@@ -209,10 +211,7 @@ public class Utils {
if (targetType.isAssignableFrom(currentType)) {
return o;
}
- // convert enum
- if (targetType.isEnum()) {
- return convertEnum(o, targetType);
- }
+
// convert from CLOB/TEXT/VARCHAR to String
if (targetType == String.class) {
if (Clob.class.isAssignableFrom(currentType)) {
@@ -257,16 +256,18 @@ public class Utils {
}
}
}
-
- throw new IciqlException("Can not convert the value " + o + " from " + currentType + " to "
- + targetType);
+ throw new IciqlException("Can not convert the value {0} from {1} to {2}", o, currentType, targetType);
}
-
- private static Object convertEnum(Object o, Class<?> targetType) {
+
+ public static Object convertEnum(Object o, Class<?> targetType, EnumType type) {
if (o == null) {
return null;
}
Class<?> currentType = o.getClass();
+ if (targetType.isAssignableFrom(currentType)) {
+ return o;
+ }
+
// convert from VARCHAR/TEXT/INT to Enum
Enum<?>[] values = (Enum[]) targetType.getEnumConstants();
if (Clob.class.isAssignableFrom(currentType)) {
@@ -297,16 +298,28 @@ public class Utils {
} else if (Number.class.isAssignableFrom(currentType)) {
// INT field
int n = ((Number) o).intValue();
-
- // ORDINAL mapping
- for (Enum<?> value : values) {
- if (value.ordinal() == n) {
- return value;
+ if (type.equals(EnumType.ORDINAL)) {
+ // ORDINAL mapping
+ for (Enum<?> value : values) {
+ if (value.ordinal() == n) {
+ return value;
+ }
+ }
+ } else if (type.equals(EnumType.ENUMID)) {
+ if (!EnumId.class.isAssignableFrom(targetType)) {
+ throw new IciqlException("Can not convert the value {0} from {1} to {2} using ENUMID", o,
+ currentType, targetType);
+ }
+ // ENUMID mapping
+ for (Enum<?> value : values) {
+ EnumId enumid = (EnumId) value;
+ if (enumid.enumId() == n) {
+ return value;
+ }
}
}
}
- throw new IciqlException("Can not convert the value " + o + " from " + currentType + " to "
- + targetType);
+ throw new IciqlException("Can not convert the value {0} from {1} to {2}", o, currentType, targetType);
}
/**