Search in sources :

Example 11 with InvalidArgumentException

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);
        }
    };
}
Also used : LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) UnsupportedTemporalUnitException(org.neo4j.exceptions.UnsupportedTemporalUnitException) LocalDate(java.time.LocalDate) TemporalParseException(org.neo4j.exceptions.TemporalParseException) DateTimeParseException(java.time.format.DateTimeParseException) InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) ZonedDateTime(java.time.ZonedDateTime) AnyValue(org.neo4j.values.AnyValue)

Example 12 with InvalidArgumentException

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));
}
Also used : LocalDateTime(java.time.LocalDateTime) InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) ZoneRulesException(java.time.zone.ZoneRulesException) ZoneId(java.time.ZoneId) TemporalParseException(org.neo4j.exceptions.TemporalParseException) ZoneOffset(java.time.ZoneOffset)

Example 13 with InvalidArgumentException

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;
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) UnsupportedTemporalUnitException(org.neo4j.exceptions.UnsupportedTemporalUnitException) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 14 with InvalidArgumentException

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;
    }
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) Point(org.neo4j.graphdb.spatial.Point)

Example 15 with InvalidArgumentException

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);
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) LocalDate(java.time.LocalDate)

Aggregations

InvalidArgumentException (org.neo4j.exceptions.InvalidArgumentException)17 LocalDate (java.time.LocalDate)4 AnyValue (org.neo4j.values.AnyValue)4 LocalTime (java.time.LocalTime)3 ZoneId (java.time.ZoneId)3 LocalDateTime (java.time.LocalDateTime)2 ZoneOffset (java.time.ZoneOffset)2 Test (org.junit.jupiter.api.Test)2 TemporalParseException (org.neo4j.exceptions.TemporalParseException)2 UnsupportedTemporalUnitException (org.neo4j.exceptions.UnsupportedTemporalUnitException)2 NumberValue.safeCastFloatingPoint (org.neo4j.values.storable.NumberValue.safeCastFloatingPoint)2 OffsetTime (java.time.OffsetTime)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeParseException (java.time.format.DateTimeParseException)1 UnsupportedTemporalTypeException (java.time.temporal.UnsupportedTemporalTypeException)1 ZoneRulesException (java.time.zone.ZoneRulesException)1 Matcher (java.util.regex.Matcher)1 FormatException (org.neo4j.cypher.internal.security.FormatException)1 SecureHasher (org.neo4j.cypher.internal.security.SecureHasher)1 CypherTypeException (org.neo4j.exceptions.CypherTypeException)1