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);
}
}
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));
}
};
}
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);
}
};
}
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);
}
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());
}
Aggregations