Skip to content

Commit

Permalink
Support BigDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
torshid committed Nov 4, 2021
1 parent 8fa6093 commit dc0958e
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions src/main/java/com/turkraft/springfilter/parser/StringConverter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.turkraft.springfilter.parser;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.time.Instant;
Expand All @@ -10,6 +11,7 @@
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

import com.turkraft.springfilter.FilterParameters;

public class StringConverter {
Expand All @@ -29,16 +31,14 @@ public static Object convert(String input, Class<?> expectedType) {
return input;
}

if (char.class.isAssignableFrom(expectedType)
|| Character.class.isAssignableFrom(expectedType)) {
if (char.class.isAssignableFrom(expectedType) || Character.class.isAssignableFrom(expectedType)) {
if (input.length() != 1) {
throw new ClassCastException("The input '" + input + "' could not be cast to Char");
}
return input.charAt(0);
}

if (boolean.class.isAssignableFrom(expectedType)
|| Boolean.class.isAssignableFrom(expectedType)) {
if (boolean.class.isAssignableFrom(expectedType) || Boolean.class.isAssignableFrom(expectedType)) {
return Boolean.valueOf(input);
}

Expand All @@ -63,17 +63,15 @@ public static Object convert(String input, Class<?> expectedType) {
try {
return LocalDateTime.parse(input, FilterParameters.LOCALDATETIME_FORMATTER);
} catch (DateTimeParseException e) {
throw new ClassCastException(
"The input '" + input + "' could not be parsed to LocalDateTime");
throw new ClassCastException("The input '" + input + "' could not be parsed to LocalDateTime");
}
}

if (OffsetDateTime.class.isAssignableFrom(expectedType)) {
try {
return OffsetDateTime.parse(input, FilterParameters.OFFSETDATETIME_FORMATTER);
} catch (DateTimeParseException e) {
throw new ClassCastException(
"The input '" + input + "' could not be parsed to OffsetDateTime");
throw new ClassCastException("The input '" + input + "' could not be parsed to OffsetDateTime");
}
}

Expand All @@ -99,36 +97,40 @@ public static Object convert(String input, Class<?> expectedType) {
return e;
}
}
throw new ClassCastException("The input '" + input + "' didn't match any enum constant of "
+ expectedType.getSimpleName());
throw new ClassCastException(
"The input '" + input + "' didn't match any enum constant of " + expectedType.getSimpleName());
}

if (BigDecimal.class.isAssignableFrom(expectedType)) {
try {
return new BigDecimal(input);
} catch (NumberFormatException ex) {
throw new ClassCastException("The input '" + input + "' could not be parsed to BigDecimal");
}
}

ParsePosition position = new ParsePosition(0);
Number number = NUMBER_FORMAT.parse(input.toString(), position);
Number number = NUMBER_FORMAT.parse(input, position);

if (number != null) {

if (short.class.isAssignableFrom(expectedType)
|| Short.class.isAssignableFrom(expectedType)) {
if (short.class.isAssignableFrom(expectedType) || Short.class.isAssignableFrom(expectedType)) {
return number.shortValue();
}

if (int.class.isAssignableFrom(expectedType)
|| Integer.class.isAssignableFrom(expectedType)) {
if (int.class.isAssignableFrom(expectedType) || Integer.class.isAssignableFrom(expectedType)) {
return number.intValue();
}

if (long.class.isAssignableFrom(expectedType) || Long.class.isAssignableFrom(expectedType)) {
return number.longValue();
}

if (float.class.isAssignableFrom(expectedType)
|| Float.class.isAssignableFrom(expectedType)) {
if (float.class.isAssignableFrom(expectedType) || Float.class.isAssignableFrom(expectedType)) {
return number.floatValue();
}

if (double.class.isAssignableFrom(expectedType)
|| Double.class.isAssignableFrom(expectedType)) {
if (double.class.isAssignableFrom(expectedType) || Double.class.isAssignableFrom(expectedType)) {
return number.doubleValue();
}

Expand All @@ -144,12 +146,15 @@ public static String convert(Object input) {
return null;
}

if (input instanceof Boolean || input instanceof Number || input instanceof Character
|| input instanceof String || input instanceof Enum || input instanceof UUID
|| input.getClass().isPrimitive()) {
if (input instanceof Boolean || input instanceof Number || input instanceof Character || input instanceof String
|| input instanceof Enum || input instanceof UUID || input.getClass().isPrimitive()) {
return input.toString();
}

else if (input instanceof BigDecimal) {
return "'" + input + "'";
}

else if (input instanceof Date) {
return FilterParameters.DATE_FORMATTER.format(input);
}
Expand All @@ -175,11 +180,10 @@ else if (input instanceof Instant) {
}

public static boolean isSupportedAsInput(Object input) {
return input instanceof Boolean || input instanceof Number || input instanceof Character
|| input instanceof String || input instanceof Enum || input instanceof UUID
|| input.getClass().isPrimitive() || input instanceof Date || input instanceof LocalDate
|| input instanceof LocalDateTime || input instanceof OffsetDateTime
|| input instanceof Instant;
return input instanceof Boolean || input instanceof Number || input instanceof Character || input instanceof String
|| input instanceof Enum || input instanceof UUID || input.getClass().isPrimitive() || input instanceof Date
|| input instanceof LocalDate || input instanceof LocalDateTime || input instanceof OffsetDateTime
|| input instanceof Instant || input instanceof BigDecimal;
}

public static String cleanStringInput(String input) {
Expand Down

0 comments on commit dc0958e

Please sign in to comment.