Search in sources :

Example 1 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class DurationValue method parseDateDuration.

private static DurationValue parseDateDuration(String year, Matcher matcher, boolean time) {
    long months = 0;
    long days = 0;
    if (year != null) {
        String month = matcher.group("longMonth");
        String day;
        if (month == null) {
            month = matcher.group("shortMonth");
            day = matcher.group("shortDay");
        } else {
            day = matcher.group("longDay");
        }
        months = parseLong(month);
        if (months > 12) {
            throw new InvalidArgumentException("months is out of range: " + month);
        }
        months += parseLong(year) * 12;
        days = parseLong(day);
        if (days > 31) {
            throw new InvalidArgumentException("days is out of range: " + day);
        }
    }
    int sign = "-".equals(matcher.group("sign")) ? -1 : 1;
    if (time) {
        if (matcher.group("longHour") != null) {
            return parseDuration(sign, months, days, matcher, true, "longHour", "longMinute", "longSecond", "longSub");
        } else {
            return parseDuration(sign, months, days, matcher, true, "shortHour", "shortMinute", "shortSecond", "shortSub");
        }
    } else {
        return duration(sign * months, sign * days, 0, 0);
    }
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) NumberValue.safeCastFloatingPoint(org.neo4j.values.storable.NumberValue.safeCastFloatingPoint)

Example 2 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class LocalDateTimeValue method builder.

private static DateTimeValue.DateTimeBuilder<LocalDateTimeValue> builder(Supplier<ZoneId> defaultZone) {
    return new DateTimeValue.DateTimeBuilder<>(defaultZone) {

        @Override
        protected boolean supportsTimeZone() {
            return false;
        }

        @Override
        protected boolean supportsEpoch() {
            return false;
        }

        @Override
        public LocalDateTimeValue buildInternal() {
            boolean selectingDate = fields.containsKey(TemporalFields.date);
            boolean selectingTime = fields.containsKey(TemporalFields.time);
            boolean selectingDateTime = fields.containsKey(TemporalFields.datetime);
            LocalDateTime result;
            if (selectingDateTime) {
                AnyValue dtField = fields.get(TemporalFields.datetime);
                if (!(dtField instanceof TemporalValue)) {
                    throw new InvalidArgumentException(String.format("Cannot construct local date time from: %s", dtField));
                }
                TemporalValue dt = (TemporalValue) dtField;
                result = LocalDateTime.of(dt.getDatePart(), dt.getLocalTimePart());
            } else if (selectingTime || selectingDate) {
                LocalTime time;
                if (selectingTime) {
                    AnyValue timeField = fields.get(TemporalFields.time);
                    if (!(timeField instanceof TemporalValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct local time from: %s", timeField));
                    }
                    TemporalValue t = (TemporalValue) timeField;
                    time = t.getLocalTimePart();
                } else {
                    time = LocalTimeValue.DEFAULT_LOCAL_TIME;
                }
                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 = LocalDateTime.of(date, time);
            } else {
                result = DEFAULT_LOCAL_DATE_TIME;
            }
            if (fields.containsKey(TemporalFields.week) && !selectingDate && !selectingDateTime) {
                // 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);
            return localDateTime(result);
        }

        private LocalDateTime getLocalDateTimeOf(AnyValue temporal) {
            if (temporal instanceof TemporalValue) {
                TemporalValue v = (TemporalValue) temporal;
                LocalDate datePart = v.getDatePart();
                LocalTime timePart = v.getLocalTimePart();
                return LocalDateTime.of(datePart, timePart);
            }
            throw new InvalidArgumentException(String.format("Cannot construct date from: %s", temporal));
        }

        @Override
        protected LocalDateTimeValue selectDateTime(AnyValue datetime) {
            if (datetime instanceof LocalDateTimeValue) {
                return (LocalDateTimeValue) datetime;
            }
            return localDateTime(getLocalDateTimeOf(datetime));
        }
    };
}
Also used : LocalDateTime(java.time.LocalDateTime) InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) LocalTime(java.time.LocalTime) AnyValue(org.neo4j.values.AnyValue) LocalDate(java.time.LocalDate)

Example 3 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class LocalTimeValue method builder.

private static TimeValue.TimeBuilder<LocalTimeValue> builder(Supplier<ZoneId> defaultZone) {
    return new TimeValue.TimeBuilder<>(defaultZone) {

        @Override
        protected boolean supportsTimeZone() {
            return false;
        }

        @Override
        public LocalTimeValue buildInternal() {
            LocalTime result;
            if (fields.containsKey(TemporalFields.time)) {
                AnyValue time = fields.get(TemporalFields.time);
                if (!(time instanceof TemporalValue)) {
                    throw new InvalidArgumentException(String.format("Cannot construct local time from: %s", time));
                }
                result = ((TemporalValue) time).getLocalTimePart();
            } else {
                result = DEFAULT_LOCAL_TIME;
            }
            result = assignAllFields(result);
            return localTime(result);
        }

        @Override
        protected LocalTimeValue selectTime(AnyValue time) {
            if (!(time instanceof TemporalValue)) {
                throw new InvalidArgumentException(String.format("Cannot construct local time from: %s", time));
            }
            TemporalValue v = (TemporalValue) time;
            LocalTime lt = v.getLocalTimePart();
            return localTime(lt);
        }
    };
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) LocalTime(java.time.LocalTime) AnyValue(org.neo4j.values.AnyValue)

Example 4 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class PointValue method fromInputFields.

/**
 * This contains the logic to decide the default coordinate reference system based on the input fields
 */
private static PointValue fromInputFields(PointBuilder fields) {
    CoordinateReferenceSystem crs = findSpecifiedCRS(fields);
    double[] coordinates;
    if (fields.x != null && fields.y != null) {
        coordinates = fields.z != null ? new double[] { fields.x, fields.y, fields.z } : new double[] { fields.x, fields.y };
        if (crs == null) {
            crs = coordinates.length == 3 ? CoordinateReferenceSystem.Cartesian_3D : CoordinateReferenceSystem.Cartesian;
        }
    } else if (fields.latitude != null && fields.longitude != null) {
        if (fields.z != null) {
            coordinates = new double[] { fields.longitude, fields.latitude, fields.z };
        } else if (fields.height != null) {
            coordinates = new double[] { fields.longitude, fields.latitude, fields.height };
        } else {
            coordinates = new double[] { fields.longitude, fields.latitude };
        }
        if (crs == null) {
            crs = coordinates.length == 3 ? CoordinateReferenceSystem.WGS84_3D : CoordinateReferenceSystem.WGS84;
        }
        if (!crs.isGeographic()) {
            throw new InvalidArgumentException("Geographic points does not support coordinate reference system: " + crs + ". This is set either in the csv header or the actual data column");
        }
    } else {
        if (crs == CoordinateReferenceSystem.Cartesian) {
            throw new InvalidArgumentException("A " + CoordinateReferenceSystem.Cartesian.getName() + " point must contain 'x' and 'y'");
        } else if (crs == CoordinateReferenceSystem.Cartesian_3D) {
            throw new InvalidArgumentException("A " + CoordinateReferenceSystem.Cartesian_3D.getName() + " point must contain 'x', 'y' and 'z'");
        } else if (crs == CoordinateReferenceSystem.WGS84) {
            throw new InvalidArgumentException("A " + CoordinateReferenceSystem.WGS84.getName() + " point must contain 'latitude' and 'longitude'");
        } else if (crs == CoordinateReferenceSystem.WGS84_3D) {
            throw new InvalidArgumentException("A " + CoordinateReferenceSystem.WGS84_3D.getName() + " point must contain 'latitude', 'longitude' and 'height'");
        }
        throw new InvalidArgumentException("A point must contain either 'x' and 'y' or 'latitude' and 'longitude'");
    }
    if (crs.getDimension() != coordinates.length) {
        throw new InvalidArgumentException("Cannot create point with " + crs.getDimension() + "D coordinate reference system and " + coordinates.length + " coordinates. Please consider using equivalent " + coordinates.length + "D coordinate reference system");
    }
    return Values.pointValue(crs, coordinates);
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException)

Example 5 with InvalidArgumentException

use of org.neo4j.exceptions.InvalidArgumentException in project neo4j by neo4j.

the class CoordinateReferenceSystemTest method shouldFailToGetWithIncorrectCode.

@Test
void shouldFailToGetWithIncorrectCode() {
    InvalidArgumentException exception = assertThrows(InvalidArgumentException.class, () -> CoordinateReferenceSystem.get(42));
    assertEquals("Unknown coordinate reference system code: 42", exception.getMessage());
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) Test(org.junit.jupiter.api.Test)

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