use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.
the class DateTimeValue method builder.
static DateTimeBuilder<DateTimeValue> builder(Supplier<ZoneId> defaultZone) {
return new DateTimeBuilder<>(defaultZone) {
@Override
protected boolean supportsTimeZone() {
return true;
}
@Override
protected boolean supportsEpoch() {
return true;
}
private final ZonedDateTime defaultZonedDateTime = ZonedDateTime.of(TemporalFields.year.defaultValue, TemporalFields.month.defaultValue, TemporalFields.day.defaultValue, TemporalFields.hour.defaultValue, TemporalFields.minute.defaultValue, TemporalFields.second.defaultValue, TemporalFields.nanosecond.defaultValue, timezone());
@Override
public DateTimeValue buildInternal() {
boolean selectingDate = fields.containsKey(TemporalFields.date);
boolean selectingTime = fields.containsKey(TemporalFields.time);
boolean selectingDateTime = fields.containsKey(TemporalFields.datetime);
boolean selectingEpoch = fields.containsKey(TemporalFields.epochSeconds) || fields.containsKey(TemporalFields.epochMillis);
boolean selectingTimeZone;
ZonedDateTime result;
if (selectingDateTime) {
AnyValue dtField = fields.get(TemporalFields.datetime);
if (!(dtField instanceof TemporalValue)) {
throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", dtField));
}
TemporalValue dt = (TemporalValue) dtField;
LocalTime timePart = dt.getTimePart(defaultZone).toLocalTime();
ZoneId zoneId = dt.getZoneId(defaultZone);
result = ZonedDateTime.of(dt.getDatePart(), timePart, zoneId);
selectingTimeZone = dt.supportsTimeZone();
} else if (selectingEpoch) {
if (fields.containsKey(TemporalFields.epochSeconds)) {
AnyValue epochField = fields.get(TemporalFields.epochSeconds);
if (!(epochField instanceof IntegralValue)) {
throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", epochField));
}
IntegralValue epochSeconds = (IntegralValue) epochField;
result = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochSeconds.longValue() * 1000), timezone()));
} else {
AnyValue epochField = fields.get(TemporalFields.epochMillis);
if (!(epochField instanceof IntegralValue)) {
throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", epochField));
}
IntegralValue epochMillis = (IntegralValue) epochField;
result = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis.longValue()), timezone()));
}
selectingTimeZone = false;
} else if (selectingTime || selectingDate) {
LocalTime time;
ZoneId zoneId;
if (selectingTime) {
AnyValue timeField = fields.get(TemporalFields.time);
if (!(timeField instanceof TemporalValue)) {
throw new InvalidArgumentException(String.format("Cannot construct time from: %s", timeField));
}
TemporalValue t = (TemporalValue) timeField;
time = t.getTimePart(defaultZone).toLocalTime();
zoneId = t.getZoneId(defaultZone);
selectingTimeZone = t.supportsTimeZone();
} else {
time = LocalTimeValue.DEFAULT_LOCAL_TIME;
zoneId = timezone();
selectingTimeZone = false;
}
LocalDate date;
if (selectingDate) {
AnyValue dateField = fields.get(TemporalFields.date);
if (!(dateField instanceof TemporalValue)) {
throw new InvalidArgumentException(String.format("Cannot construct date from: %s", dateField));
}
TemporalValue t = (TemporalValue) dateField;
date = t.getDatePart();
} else {
date = DateValue.DEFAULT_CALENDER_DATE;
}
result = ZonedDateTime.of(date, time, zoneId);
} else {
result = defaultZonedDateTime;
selectingTimeZone = false;
}
if (fields.containsKey(TemporalFields.week) && !selectingDate && !selectingDateTime && !selectingEpoch) {
// Be sure to be in the start of the week based year (which can be later than 1st Jan)
result = result.with(IsoFields.WEEK_BASED_YEAR, safeCastIntegral(TemporalFields.year.name(), fields.get(TemporalFields.year), TemporalFields.year.defaultValue)).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1).with(ChronoField.DAY_OF_WEEK, 1);
}
result = assignAllFields(result);
if (timezone != null) {
if (((selectingTime || selectingDateTime) && selectingTimeZone) || selectingEpoch) {
try {
result = result.withZoneSameInstant(timezone());
} catch (DateTimeParseException e) {
throw new TemporalParseException(e.getMessage(), e.getParsedString(), e.getErrorIndex(), e);
}
} else {
result = result.withZoneSameLocal(timezone());
}
}
return datetime(result);
}
@Override
protected DateTimeValue selectDateTime(AnyValue datetime) {
if (datetime instanceof DateTimeValue) {
DateTimeValue value = (DateTimeValue) datetime;
ZoneId zone = optionalTimezone();
return zone == null ? value : new DateTimeValue(ZonedDateTime.of(value.temporal().toLocalDateTime(), zone));
}
if (datetime instanceof LocalDateTimeValue) {
return new DateTimeValue(ZonedDateTime.of(((LocalDateTimeValue) datetime).temporal(), timezone()));
}
throw new UnsupportedTemporalUnitException("Cannot select datetime from: " + datetime);
}
};
}
use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.
the class DateTimeValue method parse.
private static DateTimeValue parse(Matcher matcher, Supplier<ZoneId> defaultZone) {
LocalDateTime local = LocalDateTime.of(parseDate(matcher), optTime(matcher));
String zoneName = matcher.group("zoneName");
ZoneOffset offset = parseOffset(matcher);
ZoneId zone;
if (zoneName != null) {
zone = parseZoneName(zoneName);
if (offset != null) {
try {
if (!zone.getRules().isValidOffset(local, offset)) {
throw new InvalidArgumentException("Timezone and offset do not match: " + matcher.group());
}
} catch (ZoneRulesException e) {
throw new TemporalParseException(e.getMessage(), e);
}
}
} else if (offset != null) {
zone = offset;
} else {
zone = defaultZone.get();
}
return new DateTimeValue(ZonedDateTime.ofLocal(local, zone, offset));
}
use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.
the class TemporalValue method until.
@Override
public final long until(Temporal endExclusive, TemporalUnit unit) {
if (!(endExclusive instanceof TemporalValue)) {
throw new InvalidArgumentException("Can only compute durations between TemporalValues.");
}
TemporalValue from = this;
TemporalValue to = (TemporalValue) endExclusive;
if (unit.isTimeBased()) {
from = attachTime(from);
to = attachTime(to);
} else if (from.isSupported(ChronoField.SECOND_OF_DAY) && !to.isSupported(ChronoField.SECOND_OF_DAY)) {
to = attachTime(to);
} else if (to.isSupported(ChronoField.SECOND_OF_DAY) && !from.isSupported(ChronoField.SECOND_OF_DAY)) {
from = attachTime(from);
}
if (from.isSupported(ChronoField.MONTH_OF_YEAR) && !to.isSupported(ChronoField.MONTH_OF_YEAR)) {
to = attachDate(to, from.getDatePart());
} else if (to.isSupported(ChronoField.MONTH_OF_YEAR) && !from.isSupported(ChronoField.MONTH_OF_YEAR)) {
from = attachDate(from, to.getDatePart());
}
if (from.supportsTimeZone() && !to.supportsTimeZone()) {
to = attachTimeZone(to, from.getZoneId(from::getZoneOffset));
} else if (to.supportsTimeZone() && !from.supportsTimeZone()) {
from = attachTimeZone(from, to.getZoneId(to::getZoneOffset));
}
long until;
try {
until = from.temporal().until(to, unit);
} catch (UnsupportedTemporalTypeException e) {
throw new UnsupportedTemporalUnitException(e.getMessage(), e);
}
return until;
}
use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.
the class PointValue method findSpecifiedCRS.
private static CoordinateReferenceSystem findSpecifiedCRS(PointBuilder fields) {
String crsValue = fields.crs;
int sridValue = fields.srid;
if (crsValue != null && sridValue != -1) {
throw new InvalidArgumentException("Cannot specify both CRS and SRID");
} else if (crsValue != null) {
return CoordinateReferenceSystem.byName(crsValue);
} else if (sridValue != -1) {
return CoordinateReferenceSystem.get(sridValue);
} else {
return null;
}
}
use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.
the class DateValue method localWeekDate.
private static LocalDate localWeekDate(int year, int week, int dayOfWeek) {
// the fourth is guaranteed to be in week 1 by definition
LocalDate weekOne = LocalDate.of(year, 1, 4);
LocalDate withWeek = weekOne.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, week);
// week 53 of years that don't have 53 weeks, so we have to guard for this:
if (week == 53 && withWeek.get(IsoFields.WEEK_BASED_YEAR) != year) {
throw new InvalidArgumentException(String.format("Year %d does not contain %d weeks.", year, week));
}
return withWeek.with(ChronoField.DAY_OF_WEEK, dayOfWeek);
}
Aggregations