use of java.time.chrono.ChronoPeriod in project j2objc by google.
the class TCKChronoPeriod method test_plus.
// -----------------------------------------------------------------------
@Test()
@UseDataProvider("data_of_calendars")
public void test_plus(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoPeriod period2 = chrono.period(2, 3, 4);
ChronoPeriod result = period.plus(period2);
assertEquals(result, chrono.period(3, 5, 7));
}
use of java.time.chrono.ChronoPeriod in project j2objc by google.
the class TCKChronoPeriod method test_negated.
/* J2ObjC removed: Only "gregorian" and "julian" calendars are supported.
@Test(expected=DateTimeException.class)
@UseDataProvider("data_of_calendars")
public void test_subtractFrom_wrongChrono(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoLocalDate isoDate = LocalDate.of(2000, 1, 1);
ChronoLocalDate thaiDate = ThaiBuddhistChronology.INSTANCE.date(2000, 1, 1);
// one of these two will fail
period.subtractFrom(isoDate);
period.subtractFrom(thaiDate);
} */
// -----------------------------------------------------------------------
@Test()
@UseDataProvider("data_of_calendars")
public void test_negated(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
assertEquals(period.negated(), chrono.period(-1, -2, -3));
}
use of java.time.chrono.ChronoPeriod in project j2objc by google.
the class Period method from.
// -----------------------------------------------------------------------
/**
* Obtains an instance of {@code Period} from a temporal amount.
* <p>
* This obtains a period based on the specified amount.
* A {@code TemporalAmount} represents an amount of time, which may be
* date-based or time-based, which this factory extracts to a {@code Period}.
* <p>
* The conversion loops around the set of units from the amount and uses
* the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
* and {@link ChronoUnit#DAYS DAYS} units to create a period.
* If any other units are found then an exception is thrown.
* <p>
* If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
*
* @param amount the temporal amount to convert, not null
* @return the equivalent period, not null
* @throws DateTimeException if unable to convert to a {@code Period}
* @throws ArithmeticException if the amount of years, months or days exceeds an int
*/
public static Period from(TemporalAmount amount) {
if (amount instanceof Period) {
return (Period) amount;
}
if (amount instanceof ChronoPeriod) {
if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) {
throw new DateTimeException("Period requires ISO chronology: " + amount);
}
}
Objects.requireNonNull(amount, "amount");
int years = 0;
int months = 0;
int days = 0;
for (TemporalUnit unit : amount.getUnits()) {
long unitAmount = amount.get(unit);
if (unit == ChronoUnit.YEARS) {
years = Math.toIntExact(unitAmount);
} else if (unit == ChronoUnit.MONTHS) {
months = Math.toIntExact(unitAmount);
} else if (unit == ChronoUnit.DAYS) {
days = Math.toIntExact(unitAmount);
} else {
throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit);
}
}
return create(years, months, days);
}
use of java.time.chrono.ChronoPeriod in project drools by kiegroup.
the class TypeUtil method formatValue.
public static String formatValue(final Object val, final boolean wrapForCodeUsage) {
if (val instanceof String) {
return formatString(val.toString(), wrapForCodeUsage);
} else if (val instanceof LocalDate) {
return formatDate((LocalDate) val, wrapForCodeUsage);
} else if (val instanceof LocalTime || val instanceof OffsetTime) {
return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else if (val instanceof LocalDateTime || val instanceof OffsetDateTime) {
return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else if (val instanceof ZonedDateTime) {
TemporalAccessor ta = (TemporalAccessor) val;
ZoneId zone = ta.query(TemporalQueries.zone());
if (!(zone instanceof ZoneOffset)) {
// it is a ZoneRegion
return formatDateTimeString(DateAndTimeFunction.REGION_DATETIME_FORMATTER.format((TemporalAccessor) val), wrapForCodeUsage);
} else {
return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
}
} else if (val instanceof Duration) {
return formatDuration((Duration) val, wrapForCodeUsage);
} else if (val instanceof ChronoPeriod) {
return formatPeriod((ChronoPeriod) val, wrapForCodeUsage);
} else if (val instanceof TemporalAccessor) {
TemporalAccessor ta = (TemporalAccessor) val;
if (ta.query(TemporalQueries.localDate()) == null && ta.query(TemporalQueries.localTime()) != null && ta.query(TemporalQueries.zoneId()) != null) {
return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else {
return String.valueOf(val);
}
} else if (val instanceof List) {
return formatList((List) val, wrapForCodeUsage);
} else if (val instanceof Range) {
return formatRange((Range) val, wrapForCodeUsage);
} else if (val instanceof Map) {
return formatContext((Map) val, wrapForCodeUsage);
} else {
return String.valueOf(val);
}
}
use of java.time.chrono.ChronoPeriod in project drools by kiegroup.
the class EvalHelper method compare.
/**
* Compares left and right operands using the given predicate and returns TRUE/FALSE accordingly
*
* @param left
* @param right
* @param ctx
* @param op
* @return
*/
public static Boolean compare(Object left, Object right, EvaluationContext ctx, BiPredicate<Comparable, Comparable> op) {
if (left == null || right == null) {
return null;
} 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 op.test(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 op.test(valuet(l), valuet(r));
} else if (BuiltInType.determineTypeFromInstance(left) == BuiltInType.DATE_TIME && BuiltInType.determineTypeFromInstance(right) == BuiltInType.DATE_TIME) {
return op.test(valuedt(l, r.query(TemporalQueries.zone())), valuedt(r, l.query(TemporalQueries.zone())));
}
// fallback; continue:
}
// last fallback:
if ((left instanceof String && right instanceof String) || (left instanceof Number && right instanceof Number) || (left instanceof Boolean && right instanceof Boolean) || (left instanceof Comparable && left.getClass().isAssignableFrom(right.getClass()))) {
Comparable l = (Comparable) left;
Comparable r = (Comparable) right;
return op.test(l, r);
}
return null;
}
Aggregations