Skip to content

Commit

Permalink
Change to new exception type for Spatial
Browse files Browse the repository at this point in the history
  • Loading branch information
Lojjs committed Apr 5, 2018
1 parent e4cbe5a commit 001092e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
Expand Up @@ -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
Expand All @@ -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 )
Expand All @@ -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 )
Expand All @@ -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 )
Expand All @@ -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;
Expand Down
Expand Up @@ -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;

Expand All @@ -47,7 +49,7 @@ public class PointValue extends ScalarValue implements Point, Comparable<PointVa
{
if ( !Double.isFinite( c ) )
{
throw new IllegalArgumentException( "Cannot create a point with non-finite coordinate values: " + Arrays.toString(coordinate) );
throw new InvalidValuesArgumentException( "Cannot create a point with non-finite coordinate values: " + Arrays.toString(coordinate) );
}
}
}
Expand Down Expand Up @@ -291,7 +293,7 @@ public static PointValue parse( CharSequence text, CSVHeaderInformation fieldsFr
// Merge InputFields: Data fields override header fields
if ( !(fieldsFromHeader instanceof PointCSVHeaderInformation) )
{
throw new IllegalStateException( "Wrong header information type: " + fieldsFromHeader );
throw new InvalidValuesArgumentException( "Wrong header information type: " + fieldsFromHeader );
}
fieldsFromData.mergeWithHeader( (PointCSVHeaderInformation) fieldsFromHeader );
}
Expand All @@ -311,7 +313,7 @@ private static CoordinateReferenceSystem findSpecifiedCRS( PointCSVHeaderInforma
int sridValue = fields.srid;
if ( crsValue != null && sridValue != -1 )
{
throw new IllegalArgumentException( "Cannot specify both CRS and SRID" );
throw new InvalidValuesArgumentException( "Cannot specify both CRS and SRID" );
}
else if ( crsValue != null )
{
Expand Down Expand Up @@ -363,36 +365,36 @@ else if ( fields.height != null )
}
if ( !crs.isGeographic() )
{
throw new IllegalArgumentException( "Geographic points does not support coordinate reference system: " + crs +
throw new InvalidValuesArgumentException( "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 IllegalArgumentException( "A " + CoordinateReferenceSystem.Cartesian.getName() + " point must contain 'x' and 'y'" );
throw new InvalidValuesArgumentException( "A " + CoordinateReferenceSystem.Cartesian.getName() + " point must contain 'x' and 'y'" );
}
else if ( crs == CoordinateReferenceSystem.Cartesian_3D )
{
throw new IllegalArgumentException( "A " + CoordinateReferenceSystem.Cartesian_3D.getName() + " point must contain 'x', 'y' and 'z'" );
throw new InvalidValuesArgumentException( "A " + CoordinateReferenceSystem.Cartesian_3D.getName() + " point must contain 'x', 'y' and 'z'" );
}
else if ( crs == CoordinateReferenceSystem.WGS84 )
{
throw new IllegalArgumentException( "A " + CoordinateReferenceSystem.WGS84.getName() + " point must contain 'latitude' and 'longitude'" );
throw new InvalidValuesArgumentException( "A " + CoordinateReferenceSystem.WGS84.getName() + " point must contain 'latitude' and 'longitude'" );
}
else if ( crs == CoordinateReferenceSystem.WGS84_3D )
{
throw new IllegalArgumentException(
throw new InvalidValuesArgumentException(
"A " + CoordinateReferenceSystem.WGS84_3D.getName() + " point must contain 'latitude', 'longitude' and 'height'" );
}
throw new IllegalArgumentException( "A point must contain either 'x' and 'y' or 'latitude' and 'longitude'" );
throw new InvalidValuesArgumentException( "A point must contain either 'x' and 'y' or 'latitude' and 'longitude'" );
}

if ( crs.getDimension() != coordinates.length )
{
throw new IllegalArgumentException( "Cannot create point with " + crs.getDimension() + "D coordinate reference system and " + coordinates.length +
" coordinates. Please consider using equivalent " + coordinates.length + "D coordinate reference system" );
throw new InvalidValuesArgumentException( "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 );
}
Expand Down Expand Up @@ -421,19 +423,19 @@ public AnyValue get( String fieldName )
case "srid":
return Values.intValue( crs.getCode() );
default:
throw new IllegalArgumentException( "No such field: " + fieldName );
throw new InvalidValuesArgumentException( "No such field: " + fieldName );
}
}

private DoubleValue getNthCoordinate( int n, String fieldName, boolean onlyGeographic )
{
if ( onlyGeographic && !this.getCoordinateReferenceSystem().isGeographic() )
{
throw new IllegalArgumentException( "Field: " + fieldName + " is not available on cartesian point: " + this );
throw new InvalidValuesArgumentException( "Field: " + fieldName + " is not available on cartesian point: " + this );
}
else if ( n >= 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
{
Expand All @@ -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 ) );
}
}

Expand All @@ -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 ) );
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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 );
Expand All @@ -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 )
Expand All @@ -592,4 +594,16 @@ void mergeWithHeader( PointCSVHeaderInformation header )
this.srid = this.srid == -1 ? header.srid : this.srid;
}
}

private static <T extends Number> T assertConvertible( Supplier<T> func )
{
try
{
return func.get();
}
catch ( NumberFormatException e )
{
throw new InvalidValuesArgumentException( e.getMessage(), e );
}
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand All @@ -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() );
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 001092e

Please sign in to comment.