use of java.time.Period in project drools by kiegroup.
the class EvalHelper method getDefinedValue.
public static PropertyValueResult getDefinedValue(final Object current, final String property) {
Object result;
if (current == null) {
return PropertyValueResult.notDefined();
} else if (current instanceof Map) {
result = ((Map) current).get(property);
if (result == null) {
// most cases "result" will be defined, so checking here only in case null was to signify missing key altogether.
if (!((Map) current).containsKey(property)) {
return PropertyValueResult.notDefined();
}
}
} else if (current instanceof Period) {
switch(property) {
case "years":
result = ((Period) current).getYears();
break;
case "months":
result = ((Period) current).getMonths() % 12;
break;
case "days":
result = ((Period) current).getDays() % 30;
break;
default:
return PropertyValueResult.notDefined();
}
} else if (current instanceof Duration) {
switch(property) {
case "days":
result = ((Duration) current).toDays();
break;
case "hours":
result = ((Duration) current).toHours() % 24;
break;
case "minutes":
result = ((Duration) current).toMinutes() % 60;
break;
case "seconds":
result = ((Duration) current).getSeconds() % 60;
break;
default:
return PropertyValueResult.notDefined();
}
} else if (current instanceof Temporal) {
switch(property) {
case "year":
result = ((Temporal) current).get(ChronoField.YEAR);
break;
case "month":
result = ((Temporal) current).get(ChronoField.MONTH_OF_YEAR);
break;
case "day":
result = ((Temporal) current).get(ChronoField.DAY_OF_MONTH);
break;
case "hour":
result = ((Temporal) current).get(ChronoField.HOUR_OF_DAY);
break;
case "minute":
result = ((Temporal) current).get(ChronoField.MINUTE_OF_HOUR);
break;
case "second":
result = ((Temporal) current).get(ChronoField.SECOND_OF_MINUTE);
break;
case "time offset":
case "timezone":
result = Duration.ofSeconds(((Temporal) current).get(ChronoField.OFFSET_SECONDS));
break;
case "weekday":
result = ((Temporal) current).get(ChronoField.DAY_OF_WEEK);
break;
default:
return PropertyValueResult.notDefined();
}
} else {
Method getter = getGenericAccessor(current.getClass(), property);
if (getter != null) {
try {
result = getter.invoke(current);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
return PropertyValueResult.of(Either.ofLeft(e));
}
} else {
// WORST-CASE: if code reached here, means that "property" is not defined on the "current" object at all.
return PropertyValueResult.notDefined();
}
}
// before returning, coerce "result" into number.
result = coerceNumber(result);
return PropertyValueResult.ofValue(result);
}
Aggregations