Search in sources :

Example 46 with Period

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);
}
Also used : Temporal(java.time.temporal.Temporal) Period(java.time.Period) Duration(java.time.Duration) Method(java.lang.reflect.Method) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Period (java.time.Period)46 Test (org.testng.annotations.Test)27 Duration (java.time.Duration)7 LocalDate (java.time.LocalDate)7 LocalDateTime (java.time.LocalDateTime)5 LocalTime (java.time.LocalTime)4 ZonedDateTime (java.time.ZonedDateTime)4 Temporal (java.time.temporal.Temporal)4 OffsetTime (java.time.OffsetTime)3 DateTimeParseException (java.time.format.DateTimeParseException)3 Map (java.util.Map)3 OffsetDateTime (java.time.OffsetDateTime)2 ZoneId (java.time.ZoneId)2 ZoneOffset (java.time.ZoneOffset)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 TemporalAmount (java.time.temporal.TemporalAmount)2 LocalTimeCell (org.knime.core.data.time.localtime.LocalTimeCell)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)1