use of java.time.temporal.TemporalUnit in project neo4j by neo4j.
the class TemporalValue method updateFieldMapWithConflictingSubseconds.
static <TEMP extends Temporal, VALUE> VALUE updateFieldMapWithConflictingSubseconds(MapValue fields, TemporalUnit unit, TEMP temporal, BiFunction<MapValue, TEMP, VALUE> mapFunction) {
boolean conflictingMilliSeconds = unit == ChronoUnit.MILLIS && (fields.containsKey("microsecond") || fields.containsKey("nanosecond"));
boolean conflictingMicroSeconds = unit == ChronoUnit.MICROS && fields.containsKey("nanosecond");
if (conflictingMilliSeconds) {
AnyValue millis = Values.intValue(temporal.get(ChronoField.MILLI_OF_SECOND));
AnyValue micros = fields.get("microsecond");
AnyValue nanos = fields.get("nanosecond");
int newNanos = validNano(millis, micros, nanos);
TEMP newTemporal = (TEMP) temporal.with(ChronoField.NANO_OF_SECOND, newNanos);
MapValue filtered = fields.filter((k, ignore) -> !k.equals("microsecond") && !k.equals("nanosecond"));
return mapFunction.apply(filtered, newTemporal);
} else if (conflictingMicroSeconds) {
AnyValue micros = Values.intValue(temporal.get(ChronoField.MICRO_OF_SECOND));
AnyValue nanos = fields.get("nanosecond");
int newNanos = validNano(null, micros, nanos);
TEMP newTemporal = (TEMP) temporal.with(ChronoField.NANO_OF_SECOND, newNanos);
MapValue filtered = fields.filter((k, ignore) -> !k.equals("nanosecond"));
return mapFunction.apply(filtered, newTemporal);
} else {
return mapFunction.apply(fields, temporal);
}
}
use of java.time.temporal.TemporalUnit in project perun by CESNET.
the class Utils method prepareGracePeriodDate.
/**
* Prepares grace period date by values from given matcher.
* @param matcher matcher
* @return pair of field(ChronoUnit.YEARS, ChronoUnit.MONTHS, ChronoUnit.DAYS) and amount
* @throws InternalErrorException when given matcher contains invalid data
* @throws IllegalArgumentException when matcher does not match gracePeriod format
*/
public static Pair<Integer, TemporalUnit> prepareGracePeriodDate(Matcher matcher) {
if (!matcher.matches()) {
throw new IllegalArgumentException("Wrong format of gracePeriod.");
}
String countString = matcher.group(1);
int amount = Integer.valueOf(countString);
TemporalUnit field;
String dmyString = matcher.group(2);
switch(dmyString) {
case "d":
field = ChronoUnit.DAYS;
break;
case "m":
field = ChronoUnit.MONTHS;
break;
case "y":
field = ChronoUnit.YEARS;
break;
default:
throw new InternalErrorException("Wrong format of gracePeriod.");
}
return new Pair<>(amount, field);
}
use of java.time.temporal.TemporalUnit in project j2objc by google.
the class TCKDuration method factory_from_TemporalAmount_DaysNanos.
@Test
public void factory_from_TemporalAmount_DaysNanos() {
TemporalAmount amount = new TemporalAmount() {
@Override
public long get(TemporalUnit unit) {
if (unit == DAYS) {
return 23;
} else {
return 45;
}
}
@Override
public List<TemporalUnit> getUnits() {
List<TemporalUnit> list = new ArrayList<>();
list.add(DAYS);
list.add(NANOS);
return list;
}
@Override
public Temporal addTo(Temporal temporal) {
throw new UnsupportedOperationException();
}
@Override
public Temporal subtractFrom(Temporal temporal) {
throw new UnsupportedOperationException();
}
};
Duration t = Duration.from(amount);
assertEquals(t.getSeconds(), 23 * 86400);
assertEquals(t.getNano(), 45);
}
use of java.time.temporal.TemporalUnit in project j2objc by google.
the class TCKDuration method factory_from_TemporalAmount_Minutes_tooBig.
@Test(expected = ArithmeticException.class)
public void factory_from_TemporalAmount_Minutes_tooBig() {
TemporalAmount amount = new TemporalAmount() {
@Override
public long get(TemporalUnit unit) {
return (Long.MAX_VALUE / 60) + 2;
}
@Override
public List<TemporalUnit> getUnits() {
return Collections.<TemporalUnit>singletonList(MINUTES);
}
@Override
public Temporal addTo(Temporal temporal) {
throw new UnsupportedOperationException();
}
@Override
public Temporal subtractFrom(Temporal temporal) {
throw new UnsupportedOperationException();
}
};
Duration.from(amount);
}
use of java.time.temporal.TemporalUnit in project j2objc by google.
the class TCKDuration method test_duration_getUnits.
// -----------------------------------------------------------------------
@Test()
public void test_duration_getUnits() {
Duration duration = Duration.ofSeconds(5000, 1000);
List<TemporalUnit> units = duration.getUnits();
assertEquals("Period.getUnits length", units.size(), 2);
assertTrue("Period.getUnits contains ChronoUnit.SECONDS", units.contains(ChronoUnit.SECONDS));
assertTrue("contains ChronoUnit.NANOS", units.contains(ChronoUnit.NANOS));
}
Aggregations