[#2171] Allow for converting booleans to numbers through
org.jooq.tools.Convert: TRUE => 1, FALSE => 0
This commit is contained in:
parent
a1d15b13d2
commit
decf60f0d6
@ -462,6 +462,10 @@ public final class Convert {
|
||||
|
||||
// Various number types are converted between each other via String
|
||||
else if (toClass == Byte.class || toClass == byte.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? Byte.valueOf((byte) 1) : Byte.valueOf((byte) 0));
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) Byte.valueOf(new BigDecimal(from.toString().trim()).byteValue());
|
||||
}
|
||||
@ -470,6 +474,10 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == Short.class || toClass == short.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? Short.valueOf((short) 1) : Short.valueOf((short) 0));
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) Short.valueOf(new BigDecimal(from.toString().trim()).shortValue());
|
||||
}
|
||||
@ -478,6 +486,10 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == Integer.class || toClass == int.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? Integer.valueOf(1) : Integer.valueOf(0));
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) Integer.valueOf(new BigDecimal(from.toString().trim()).intValue());
|
||||
}
|
||||
@ -486,21 +498,28 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == Long.class || toClass == long.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? Long.valueOf(1L) : Long.valueOf(0L));
|
||||
}
|
||||
|
||||
if (java.util.Date.class.isAssignableFrom(fromClass)) {
|
||||
return (U) Long.valueOf(((java.util.Date) from).getTime());
|
||||
}
|
||||
else {
|
||||
try {
|
||||
return (U) Long.valueOf(new BigDecimal(from.toString().trim()).longValue());
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) Long.valueOf(new BigDecimal(from.toString().trim()).longValue());
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ... this also includes unsigned number types
|
||||
else if (toClass == UByte.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? ubyte(1) : ubyte(0));
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) ubyte(new BigDecimal(from.toString().trim()).shortValue());
|
||||
}
|
||||
@ -509,6 +528,10 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == UShort.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? ushort(1) : ushort(0));
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) ushort(new BigDecimal(from.toString().trim()).intValue());
|
||||
}
|
||||
@ -517,6 +540,10 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == UInteger.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? uint(1) : uint(0));
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) uint(new BigDecimal(from.toString().trim()).longValue());
|
||||
}
|
||||
@ -525,21 +552,28 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == ULong.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? ulong(1) : ulong(0));
|
||||
}
|
||||
|
||||
if (java.util.Date.class.isAssignableFrom(fromClass)) {
|
||||
return (U) ulong(((java.util.Date) from).getTime());
|
||||
}
|
||||
else {
|
||||
try {
|
||||
return (U) ulong(new BigDecimal(from.toString().trim()).toBigInteger().toString());
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) ulong(new BigDecimal(from.toString().trim()).toBigInteger().toString());
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ... and floating point / fixed point types
|
||||
else if (toClass == Float.class || toClass == float.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? Float.valueOf(1.0f) : Float.valueOf(0.0f));
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) Float.valueOf(from.toString().trim());
|
||||
}
|
||||
@ -548,6 +582,10 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == Double.class || toClass == double.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? Double.valueOf(1.0) : Double.valueOf(0.0));
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) Double.valueOf(from.toString().trim());
|
||||
}
|
||||
@ -556,6 +594,10 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == BigDecimal.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? BigDecimal.ONE : BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) new BigDecimal(from.toString().trim());
|
||||
}
|
||||
@ -564,6 +606,10 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == BigInteger.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? BigInteger.ONE : BigInteger.ZERO);
|
||||
}
|
||||
|
||||
try {
|
||||
return (U) new BigDecimal(from.toString().trim()).toBigInteger();
|
||||
}
|
||||
@ -585,6 +631,10 @@ public final class Convert {
|
||||
}
|
||||
}
|
||||
else if (toClass == Character.class || toClass == char.class) {
|
||||
if (fromClass == Boolean.class || fromClass == boolean.class) {
|
||||
return (U) (((Boolean) from) ? Character.valueOf('1') : Character.valueOf('0'));
|
||||
}
|
||||
|
||||
if (from.toString().length() < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user