Search in sources :

Example 46 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_minus.

/* J2ObjC removed: Only "gregorian" and "julian" calendars are supported.
    @Test(expected=DateTimeException.class)
    @UseDataProvider("data_of_calendars")
    public void test_plus_wrongChrono(Chronology chrono) {
        ChronoPeriod period = chrono.period(1, 2, 3);
        ChronoPeriod isoPeriod = Period.of(2, 3, 4);
        ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4);
        // one of these two will fail
        period.plus(isoPeriod);
        period.plus(thaiPeriod);
    } */
@Test()
@UseDataProvider("data_of_calendars")
public void test_minus(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    ChronoPeriod period2 = chrono.period(2, 3, 4);
    ChronoPeriod result = period.minus(period2);
    assertEquals(result, chrono.period(-1, -1, -1));
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 47 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_equals_equal.

// -----------------------------------------------------------------------
@Test()
@UseDataProvider("data_of_calendars")
public void test_equals_equal(Chronology chrono) {
    ChronoPeriod a1 = chrono.period(1, 2, 3);
    ChronoPeriod a2 = chrono.period(1, 2, 3);
    assertEquals(a1, a1);
    assertEquals(a1, a2);
    assertEquals(a2, a1);
    assertEquals(a2, a2);
    assertEquals(a1.hashCode(), a2.hashCode());
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 48 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_addTo.

/* J2ObjC removed: Only "gregorian" and "julian" calendars are supported.
    @Test(expected=DateTimeException.class)
    @UseDataProvider("data_of_calendars")
    public void test_minus_wrongChrono(Chronology chrono) {
        ChronoPeriod period = chrono.period(1, 2, 3);
        ChronoPeriod isoPeriod = Period.of(2, 3, 4);
        ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4);
        // one of these two will fail
        period.minus(isoPeriod);
        period.minus(thaiPeriod);
    } */
// -----------------------------------------------------------------------
@Test()
@UseDataProvider("data_of_calendars")
public void test_addTo(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    ChronoLocalDate date = chrono.dateNow();
    Temporal result = period.addTo(date);
    assertEquals(result, date.plus(14, MONTHS).plus(3, DAYS));
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) ChronoLocalDate(java.time.chrono.ChronoLocalDate) Temporal(java.time.temporal.Temporal) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 49 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project drools by kiegroup.

the class InstanceOfNode method evaluate.

@Override
public Object evaluate(EvaluationContext ctx) {
    Object value = expression.evaluate(ctx);
    Type t = type.evaluate(ctx);
    if (t != BuiltInType.DURATION) {
        return t.isInstanceOf(value);
    } else {
        switch(type.getText()) {
            case SimpleType.YEARS_AND_MONTHS_DURATION:
                return value instanceof ChronoPeriod;
            case SimpleType.DAYS_AND_TIME_DURATION:
                return value instanceof Duration;
            default:
                return t.isInstanceOf(value);
        }
    }
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) SimpleType(org.kie.dmn.feel.lang.SimpleType) Type(org.kie.dmn.feel.lang.Type) Duration(java.time.Duration)

Example 50 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project drools by kiegroup.

the class EvalHelper method isEqual.

/**
 * Compares left and right for equality applying FEEL semantics to specific data types
 *
 * @param left
 * @param right
 * @param ctx
 * @return
 */
public static Boolean isEqual(Object left, Object right, EvaluationContext ctx) {
    if (left == null || right == null) {
        return left == right;
    }
    // and vice-versa
    if (left instanceof Collection && !(right instanceof Collection) && ((Collection) left).size() == 1) {
        left = ((Collection) left).toArray()[0];
    } else if (right instanceof Collection && !(left instanceof Collection) && ((Collection) right).size() == 1) {
        right = ((Collection) right).toArray()[0];
    }
    if (left instanceof Range && right instanceof Range) {
        return isEqual((Range) left, (Range) right);
    } else if (left instanceof Iterable && right instanceof Iterable) {
        return isEqual((Iterable) left, (Iterable) right);
    } else if (left instanceof Map && right instanceof Map) {
        return isEqual((Map) left, (Map) right);
    } else if (left instanceof ChronoPeriod && right instanceof ChronoPeriod) {
        // periods have special compare semantics in FEEL as it ignores "days". Only months and years are compared
        Long l = ComparablePeriod.toTotalMonths((ChronoPeriod) left);
        Long r = ComparablePeriod.toTotalMonths((ChronoPeriod) right);
        return isEqual(l, r);
    } else if (left instanceof TemporalAccessor && right instanceof TemporalAccessor) {
        // Handle specific cases when both time / datetime
        TemporalAccessor l = (TemporalAccessor) left;
        TemporalAccessor r = (TemporalAccessor) right;
        if (BuiltInType.determineTypeFromInstance(left) == BuiltInType.TIME && BuiltInType.determineTypeFromInstance(right) == BuiltInType.TIME) {
            return isEqual(valuet(l), valuet(r));
        } else if (BuiltInType.determineTypeFromInstance(left) == BuiltInType.DATE_TIME && BuiltInType.determineTypeFromInstance(right) == BuiltInType.DATE_TIME) {
            return isEqual(valuedt(l, r.query(TemporalQueries.zone())), valuedt(r, l.query(TemporalQueries.zone())));
        }
    // fallback; continue:
    }
    return compare(left, right, ctx, (l, r) -> l.compareTo(r) == 0);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) TemporalAccessor(java.time.temporal.TemporalAccessor) AtomicLong(java.util.concurrent.atomic.AtomicLong) Collection(java.util.Collection) Range(org.kie.dmn.feel.runtime.Range) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ChronoPeriod (java.time.chrono.ChronoPeriod)55 Test (org.testng.annotations.Test)26 Test (org.junit.Test)21 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)14 ChronoLocalDate (java.time.chrono.ChronoLocalDate)6 MinguoDate (java.time.chrono.MinguoDate)5 List (java.util.List)5 BigDecimal (java.math.BigDecimal)4 Period (java.time.Period)4 ChronoUnit (java.time.temporal.ChronoUnit)4 Temporal (java.time.temporal.Temporal)4 TemporalAccessor (java.time.temporal.TemporalAccessor)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Predicate (java.util.function.Predicate)4 Before (org.junit.Before)4 ComparablePeriod (org.kie.dmn.feel.lang.types.impl.ComparablePeriod)4 Duration (java.time.Duration)3 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)3 TemporalUnit (java.time.temporal.TemporalUnit)3