Skip to content

Commit

Permalink
Added check for #isAssignable else throw Standard ConversionException
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Nov 3, 2020
1 parent 7733f8b commit bf8beb1
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,45 +122,49 @@ protected Class<?> getDefaultType() {
*/
@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
Objects.requireNonNull(value, "Value can't be null.");
final String stringValue = value.toString();
if (Color.class.isAssignableFrom(type)) {
Objects.requireNonNull(value, "Value can't be null.");
final String stringValue = value.toString();

switch (stringValue.toLowerCase()) {
case "white":
return type.cast(Color.WHITE);
case "lightgray":
return type.cast(Color.LIGHT_GRAY);
case "gray":
return type.cast(Color.GRAY);
case "darkgray":
return type.cast(Color.DARK_GRAY);
case "black":
return type.cast(Color.BLACK);
case "red":
return type.cast(Color.RED);
case "pink":
return type.cast(Color.PINK);
case "orange":
return type.cast(Color.ORANGE);
case "yellow":
return type.cast(Color.YELLOW);
case "green":
return type.cast(Color.GREEN);
case "magenta":
return type.cast(Color.MAGENTA);
case "cyan":
return type.cast(Color.CYAN);
case "blue":
return type.cast(Color.BLUE);
default:
// Do nothing.
}
switch (stringValue.toLowerCase()) {
case "white":
return type.cast(Color.WHITE);
case "lightgray":
return type.cast(Color.LIGHT_GRAY);
case "gray":
return type.cast(Color.GRAY);
case "darkgray":
return type.cast(Color.DARK_GRAY);
case "black":
return type.cast(Color.BLACK);
case "red":
return type.cast(Color.RED);
case "pink":
return type.cast(Color.PINK);
case "orange":
return type.cast(Color.ORANGE);
case "yellow":
return type.cast(Color.YELLOW);
case "green":
return type.cast(Color.GREEN);
case "magenta":
return type.cast(Color.MAGENTA);
case "cyan":
return type.cast(Color.CYAN);
case "blue":
return type.cast(Color.BLUE);
default:
// Do nothing.
}

if (stringValue.startsWith(HEX_COLOR_PREFIX)) {
return type.cast(parseWebColor(stringValue));
}

if (stringValue.startsWith(HEX_COLOR_PREFIX)) {
return type.cast(parseWebColor(stringValue));
return type.cast(Color.decode(stringValue));
}

return type.cast(Color.decode(stringValue));
throw conversionException(type, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,30 @@ protected Class<?> getDefaultType() {
*/
@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
Objects.requireNonNull(value, "Dimensions can not be null.");
final String stringValue = value.toString();
if (Dimension.class.isAssignableFrom(type)) {
Objects.requireNonNull(value, "Dimensions can not be null.");
final String stringValue = value.toString();

if (stringValue.isEmpty()) {
throw new IllegalArgumentException("Dimensions can not be empty.");
}
if (stringValue.isEmpty()) {
throw new IllegalArgumentException("Dimensions can not be empty.");
}

Matcher matcher = DIMENSION_PATTERN.matcher(stringValue);
Matcher matcher = DIMENSION_PATTERN.matcher(stringValue);

if (!matcher.matches()) {
throw new IllegalArgumentException("Dimension doesn't match format: {width/height} or {width}x{height}");
}
if (!matcher.matches()) {
throw new IllegalArgumentException(
"Dimension doesn't match format: {width/height} or {width}x{height}");
}

String x = matcher.group(1);
String y = matcher.group(2);
String x = matcher.group(1);
String y = matcher.group(2);

int xValue = Integer.parseInt(x);
int yValue = (y == null || x.equals(y)) ? xValue : Integer.parseInt(y);
int xValue = Integer.parseInt(x);
int yValue = (y == null || x.equals(y)) ? xValue : Integer.parseInt(y);

return type.cast(new Dimension(xValue, yValue));
}

return type.cast(new Dimension(xValue, yValue));
throw conversionException(type, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ protected Class<?> getDefaultType() {
*/
@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
Objects.requireNonNull(value, "Value can't be null.");
final String stringValue = value.toString();
if (InetAddress.class.isAssignableFrom(type)) {
Objects.requireNonNull(value, "Value can't be null.");
final String stringValue = value.toString();

try {
return type.cast(InetAddress.getByName(stringValue));
} catch (UnknownHostException ex) {
throw new IllegalArgumentException("Unable to get IP address of the named host.", ex);
try {
return type.cast(InetAddress.getByName(stringValue));
} catch (UnknownHostException ex) {
throw new IllegalArgumentException("Unable to get IP address of the named host.", ex);
}
}

throw conversionException(type, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ protected Class<?> getDefaultType() {
*/
@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
Objects.requireNonNull(value, "Value can't be null.");
final String stringValue = value.toString();
if (Locale.class.isAssignableFrom(type)) {
Objects.requireNonNull(value, "Value can't be null.");
final String stringValue = value.toString();

return type.cast(Locale.forLanguageTag(stringValue));
return type.cast(Locale.forLanguageTag(stringValue));
}

throw conversionException(type, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ protected Class<?> getDefaultType() {
*/
@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
Objects.requireNonNull(value, "Regular expression can't be null.");
final String stringValue = value.toString();
if (Pattern.class.isAssignableFrom(type)) {
Objects.requireNonNull(value, "Regular expression can't be null.");
final String stringValue = value.toString();

return type.cast(Pattern.compile(stringValue));
return type.cast(Pattern.compile(stringValue));
}

throw conversionException(type, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,33 @@ protected Class<?> getDefaultType() {
*/
@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
Objects.requireNonNull(value, "Value must not be null.");
final String stringValue = value.toString();
if (Point.class.isAssignableFrom(type)) {
Objects.requireNonNull(value, "Value must not be null.");
final String stringValue = value.toString();

if (stringValue.isEmpty()) {
throw new IllegalArgumentException("A point can not be empty.");
}
if (stringValue.isEmpty()) {
throw new IllegalArgumentException("A point can not be empty.");
}

final int lastCharIndex = stringValue.length() - 1;
final int lastCharIndex = stringValue.length() - 1;

if (stringValue.charAt(0) != '(' || stringValue.charAt(lastCharIndex) != ')') {
throw new IllegalArgumentException("Point coordinates must be enclosed in brackets.");
}
if (stringValue.charAt(0) != '(' || stringValue.charAt(lastCharIndex) != ')') {
throw new IllegalArgumentException("Point coordinates must be enclosed in brackets.");
}

final String coordinates = stringValue.substring(1, lastCharIndex);
final String[] xy = POINT_SPLIT.split(coordinates);

final String coordinates = stringValue.substring(1, lastCharIndex);
final String[] xy = POINT_SPLIT.split(coordinates);
if (xy.length != 2) {
throw new IllegalArgumentException("Point must have an x coordinate, and y coordinate only, " +
"expecting the following format: (40, 200)");
}

if (xy.length != 2) {
throw new IllegalArgumentException("Point must have an x coordinate, and y coordinate only, " +
"expecting the following format: (40, 200)");
final int x = Integer.parseInt(xy[0]);
final int y = Integer.parseInt(xy[1]);
return type.cast(new Point(x, y));
}

final int x = Integer.parseInt(xy[0]);
final int y = Integer.parseInt(xy[1]);
return type.cast(new Point(x, y));
throw conversionException(type, value);
}
}

0 comments on commit bf8beb1

Please sign in to comment.