From 001092e41a109ed5f3a71d03c88da6f32cc5e53f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louise=20S=C3=B6derstr=C3=B6m?= Date: Wed, 4 Apr 2018 16:14:15 +0200 Subject: [PATCH] Change to new exception type for Spatial --- .../storable/CoordinateReferenceSystem.java | 9 ++-- .../org/neo4j/values/storable/PointValue.java | 54 ++++++++++++------- .../java/org/neo4j/values/storable/Value.java | 7 +-- .../CoordinateReferenceSystemTest.java | 4 +- .../org/neo4j/values/storable/PointTest.java | 8 +-- 5 files changed, 51 insertions(+), 31 deletions(-) diff --git a/community/values/src/main/java/org/neo4j/values/storable/CoordinateReferenceSystem.java b/community/values/src/main/java/org/neo4j/values/storable/CoordinateReferenceSystem.java index fee336b0255dd..4859e0d96df08 100644 --- a/community/values/src/main/java/org/neo4j/values/storable/CoordinateReferenceSystem.java +++ b/community/values/src/main/java/org/neo4j/values/storable/CoordinateReferenceSystem.java @@ -22,6 +22,7 @@ import java.util.Objects; import org.neo4j.graphdb.spatial.CRS; +import org.neo4j.values.utils.InvalidValuesArgumentException; import org.neo4j.helpers.collection.Iterables; public class CoordinateReferenceSystem implements CRS @@ -48,7 +49,7 @@ public static CoordinateReferenceSystem get( int tableId, int code ) return type; } } - throw new IllegalArgumentException( "Unknown coordinate reference system: " + tableId + "-" + code ); + throw new InvalidValuesArgumentException( "Unknown coordinate reference system: " + tableId + "-" + code ); } public static CoordinateReferenceSystem get( CRS crs ) @@ -67,7 +68,7 @@ public static CoordinateReferenceSystem byName( String name ) } } - throw new IllegalArgumentException( "Unknown coordinate reference system: " + name ); + throw new InvalidValuesArgumentException( "Unknown coordinate reference system: " + name ); } public static CoordinateReferenceSystem get( String href ) @@ -79,7 +80,7 @@ public static CoordinateReferenceSystem get( String href ) return type; } } - throw new IllegalArgumentException( "Unknown coordinate reference system: " + href ); + throw new InvalidValuesArgumentException( "Unknown coordinate reference system: " + href ); } public static CoordinateReferenceSystem get( int code ) @@ -95,7 +96,7 @@ public static CoordinateReferenceSystem get( int code ) } } } - throw new IllegalArgumentException( "Unknown coordinate reference system code: " + code ); + throw new InvalidValuesArgumentException( "Unknown coordinate reference system code: " + code ); } private final String name; diff --git a/community/values/src/main/java/org/neo4j/values/storable/PointValue.java b/community/values/src/main/java/org/neo4j/values/storable/PointValue.java index f4f07954dffc2..b144c1405a787 100644 --- a/community/values/src/main/java/org/neo4j/values/storable/PointValue.java +++ b/community/values/src/main/java/org/neo4j/values/storable/PointValue.java @@ -22,12 +22,14 @@ import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.function.Supplier; import org.neo4j.graphdb.spatial.CRS; import org.neo4j.graphdb.spatial.Coordinate; import org.neo4j.graphdb.spatial.Point; import org.neo4j.values.AnyValue; import org.neo4j.values.ValueMapper; +import org.neo4j.values.utils.InvalidValuesArgumentException; import org.neo4j.values.utils.PrettyPrinter; import org.neo4j.values.virtual.MapValue; @@ -47,7 +49,7 @@ public class PointValue extends ScalarValue implements Point, Comparable= this.coordinate().length ) { - throw new IllegalArgumentException( "Field: " + fieldName + " is not available on point: " + this ); + throw new InvalidValuesArgumentException( "Field: " + fieldName + " is not available on point: " + this ); } else { @@ -456,7 +458,7 @@ private void checkUnassigned( Object key, String fieldName ) { if ( key != null ) { - throw new IllegalArgumentException( String.format( "Duplicate field '%s' is not allowed." , fieldName ) ); + throw new InvalidValuesArgumentException( String.format( "Duplicate field '%s' is not allowed." , fieldName ) ); } } @@ -476,7 +478,7 @@ else if ( value instanceof TextValue ) } else { - throw new IllegalArgumentException( String.format( "Cannot assign %s to field %s", value, key ) ); + throw new InvalidValuesArgumentException( String.format( "Cannot assign %s to field %s", value, key ) ); } } @@ -543,7 +545,7 @@ private void assignIntegral( String key, long value ) case "srid": if ( srid != -1 ) { - throw new IllegalArgumentException( "Duplicate field 'srid' is not allowed." ); + throw new InvalidValuesArgumentException( "Duplicate field 'srid' is not allowed." ); } srid = (int) value; break; @@ -565,10 +567,10 @@ public void assign( String key, String value ) case "longitude": case "latitude": case "height": - assignFloatingPoint( key, Double.parseDouble( value ) ); + assignFloatingPoint( key, assertConvertible( () -> Double.parseDouble( value ) ) ); break; case "srid": - assignIntegral( key, Integer.parseInt( value ) ); + assignIntegral( key, assertConvertible( () -> Integer.parseInt( value ) ) ); break; default: throwOnUnrecognizedKey( key ); @@ -577,7 +579,7 @@ public void assign( String key, String value ) private void throwOnUnrecognizedKey( String key ) { - throw new IllegalArgumentException( String.format( "Unknown key '%s' for creating new point", key ) ); + throw new InvalidValuesArgumentException( String.format( "Unknown key '%s' for creating new point", key ) ); } void mergeWithHeader( PointCSVHeaderInformation header ) @@ -592,4 +594,16 @@ void mergeWithHeader( PointCSVHeaderInformation header ) this.srid = this.srid == -1 ? header.srid : this.srid; } } + + private static T assertConvertible( Supplier func ) + { + try + { + return func.get(); + } + catch ( NumberFormatException e ) + { + throw new InvalidValuesArgumentException( e.getMessage(), e ); + } + } } diff --git a/community/values/src/main/java/org/neo4j/values/storable/Value.java b/community/values/src/main/java/org/neo4j/values/storable/Value.java index f7e91e41b8c3c..4c11b714f5eca 100644 --- a/community/values/src/main/java/org/neo4j/values/storable/Value.java +++ b/community/values/src/main/java/org/neo4j/values/storable/Value.java @@ -31,6 +31,7 @@ import org.neo4j.values.AnyValue; import org.neo4j.values.AnyValueWriter; import org.neo4j.values.SequenceValue; +import org.neo4j.values.utils.InvalidValuesArgumentException; import static java.lang.String.format; import static org.neo4j.values.storable.Values.NO_VALUE; @@ -238,19 +239,19 @@ static void parseHeaderInformation( CharSequence text, String type, CSVHeaderInf String errorMessage = format( "Failed to parse %s value: '%s'", type, text ); if ( !(mapMatcher.find() && mapMatcher.groupCount() == 1) ) { - throw new IllegalArgumentException( errorMessage ); + throw new InvalidValuesArgumentException( errorMessage ); } String mapContents = mapMatcher.group( 1 ); if ( mapContents.isEmpty() ) { - throw new IllegalArgumentException( errorMessage ); + throw new InvalidValuesArgumentException( errorMessage ); } Matcher matcher = keyValuePattern.matcher( mapContents ); if ( !(matcher.find()) ) { - throw new IllegalArgumentException( errorMessage ); + throw new InvalidValuesArgumentException( errorMessage ); } do diff --git a/community/values/src/test/java/org/neo4j/values/storable/CoordinateReferenceSystemTest.java b/community/values/src/test/java/org/neo4j/values/storable/CoordinateReferenceSystemTest.java index d634af2afa452..05a69a255569d 100644 --- a/community/values/src/test/java/org/neo4j/values/storable/CoordinateReferenceSystemTest.java +++ b/community/values/src/test/java/org/neo4j/values/storable/CoordinateReferenceSystemTest.java @@ -21,6 +21,8 @@ import org.junit.Test; +import org.neo4j.values.utils.InvalidValuesArgumentException; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.equalTo; @@ -46,7 +48,7 @@ public void shouldFailToGetWithIncorrectCode() CoordinateReferenceSystem.get( 42 ); fail( "Exception expected" ); } - catch ( IllegalArgumentException e ) + catch ( InvalidValuesArgumentException e ) { assertEquals( "Unknown coordinate reference system code: 42", e.getMessage() ); } diff --git a/community/values/src/test/java/org/neo4j/values/storable/PointTest.java b/community/values/src/test/java/org/neo4j/values/storable/PointTest.java index c82d84605e5f0..a8c3b3be33567 100644 --- a/community/values/src/test/java/org/neo4j/values/storable/PointTest.java +++ b/community/values/src/test/java/org/neo4j/values/storable/PointTest.java @@ -22,6 +22,8 @@ import org.hamcrest.CoreMatchers; import org.junit.Test; +import org.neo4j.values.utils.InvalidValuesArgumentException; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -140,7 +142,7 @@ public void shouldBeAbleToParseIncompletePointWithHeaderInformation() PointValue.parse( data ); // this shouldn't work fail( "Was able to parse point although latitude was missing" ); } - catch ( IllegalArgumentException e ) + catch ( InvalidValuesArgumentException e ) { // this is expected } @@ -187,14 +189,14 @@ public void shouldNotBeAbleToParseIncompletePoints() assertCannotParse( "{crs:WGS-84 , lat:1, y:2}" ); } - private IllegalArgumentException assertCannotParse( String text ) + private InvalidValuesArgumentException assertCannotParse( String text ) { PointValue value; try { value = PointValue.parse( text ); } - catch ( IllegalArgumentException e ) + catch ( InvalidValuesArgumentException e ) { return e; }