Skip to content

Commit 1e00d32

Browse files
committed
New types for 2D and 3D points
Introduced new types for points instead of a single generic interface that represents an arbitrary-dimensional point. This makes it cleaner what kind of points are supported by the database. It also removes the need for coordinate array creation and removes the `Coordinate` interface all together.
1 parent 4d14544 commit 1e00d32

File tree

17 files changed

+275
-274
lines changed

17 files changed

+275
-274
lines changed

driver/src/main/java/org/neo4j/driver/internal/InternalPoint.java

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2002-2018 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal;
20+
21+
import org.neo4j.driver.v1.types.Point2D;
22+
23+
public class InternalPoint2D implements Point2D
24+
{
25+
private final long srid;
26+
private final double x;
27+
private final double y;
28+
29+
public InternalPoint2D( long srid, double x, double y )
30+
{
31+
this.srid = srid;
32+
this.x = x;
33+
this.y = y;
34+
}
35+
36+
@Override
37+
public long srid()
38+
{
39+
return srid;
40+
}
41+
42+
@Override
43+
public double x()
44+
{
45+
return x;
46+
}
47+
48+
@Override
49+
public double y()
50+
{
51+
return y;
52+
}
53+
}

driver/src/main/java/org/neo4j/driver/internal/InternalCoordinate.java renamed to driver/src/main/java/org/neo4j/driver/internal/InternalPoint3D.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,44 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21-
import java.util.List;
21+
import org.neo4j.driver.v1.types.Point3D;
2222

23-
import org.neo4j.driver.v1.types.Coordinate;
24-
25-
import static java.util.Collections.unmodifiableList;
26-
27-
public class InternalCoordinate implements Coordinate
23+
public class InternalPoint3D implements Point3D
2824
{
29-
private final List<Double> values;
25+
private final long srid;
26+
private final double x;
27+
private final double y;
28+
private final double z;
3029

31-
public InternalCoordinate( List<Double> values )
30+
public InternalPoint3D( long srid, double x, double y, double z )
3231
{
33-
this.values = unmodifiableList( values );
32+
this.srid = srid;
33+
this.x = x;
34+
this.y = y;
35+
this.z = z;
3436
}
3537

3638
@Override
37-
public List<Double> values()
39+
public long srid()
3840
{
39-
return values;
41+
return srid;
4042
}
4143

4244
@Override
43-
public boolean equals( Object o )
45+
public double x()
4446
{
45-
if ( this == o )
46-
{
47-
return true;
48-
}
49-
if ( o == null || getClass() != o.getClass() )
50-
{
51-
return false;
52-
}
53-
InternalCoordinate that = (InternalCoordinate) o;
54-
return values.equals( that.values );
47+
return x;
5548
}
5649

5750
@Override
58-
public int hashCode()
51+
public double y()
5952
{
60-
return values.hashCode();
53+
return y;
6154
}
6255

6356
@Override
64-
public String toString()
57+
public double z()
6558
{
66-
return String.format( "Coordinate<%s>", values );
59+
return z;
6760
}
6861
}

driver/src/main/java/org/neo4j/driver/internal/messaging/PackStreamMessageFormatV2.java

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,26 @@
1919
package org.neo4j.driver.internal.messaging;
2020

2121
import java.io.IOException;
22-
import java.util.ArrayList;
23-
import java.util.List;
2422

25-
import org.neo4j.driver.internal.InternalCoordinate;
26-
import org.neo4j.driver.internal.InternalPoint;
23+
import org.neo4j.driver.internal.InternalPoint2D;
24+
import org.neo4j.driver.internal.InternalPoint3D;
2725
import org.neo4j.driver.internal.packstream.PackInput;
2826
import org.neo4j.driver.internal.packstream.PackOutput;
2927
import org.neo4j.driver.internal.value.InternalValue;
30-
import org.neo4j.driver.internal.value.PointValue;
28+
import org.neo4j.driver.internal.value.Point2DValue;
29+
import org.neo4j.driver.internal.value.Point3DValue;
3130
import org.neo4j.driver.v1.Value;
32-
import org.neo4j.driver.v1.types.Point;
31+
import org.neo4j.driver.v1.types.Point2D;
32+
import org.neo4j.driver.v1.types.Point3D;
3333

34-
import static org.neo4j.driver.internal.types.TypeConstructor.POINT_TyCon;
34+
import static org.neo4j.driver.internal.types.TypeConstructor.POINT_2D_TyCon;
35+
import static org.neo4j.driver.internal.types.TypeConstructor.POINT_3D_TyCon;
3536

3637
public class PackStreamMessageFormatV2 extends PackStreamMessageFormatV1
3738
{
38-
private static final byte POINT_STRUCT_TYPE = 'X';
39-
private static final int POINT_STRUCT_SIZE = 3;
39+
private static final byte POINT_2D_STRUCT_TYPE = 'X';
40+
private static final byte POINT_3D_STRUCT_TYPE = 'Y';
41+
private static final int POINT_STRUCT_SIZE = 2;
4042

4143
@Override
4244
public MessageFormat.Writer newWriter( PackOutput output, boolean byteArraySupportEnabled )
@@ -64,22 +66,32 @@ private static class WriterV2 extends WriterV1
6466
@Override
6567
void packInternalValue( InternalValue value ) throws IOException
6668
{
67-
if ( value.typeConstructor() == POINT_TyCon )
69+
if ( value.typeConstructor() == POINT_2D_TyCon )
6870
{
69-
packPoint( value.asPoint() );
71+
packPoint2D( value.asPoint2D() );
72+
}
73+
else if ( value.typeConstructor() == POINT_3D_TyCon )
74+
{
75+
packPoint3D( value.asPoint3D() );
7076
}
7177
else
7278
{
7379
super.packInternalValue( value );
7480
}
7581
}
7682

77-
private void packPoint( Point point ) throws IOException
83+
private void packPoint2D( Point2D point ) throws IOException
84+
{
85+
packer.packStructHeader( POINT_STRUCT_SIZE, POINT_2D_STRUCT_TYPE );
86+
packer.pack( point.srid() );
87+
// packer.pack( point.x(), point.y() );
88+
}
89+
90+
private void packPoint3D( Point3D point ) throws IOException
7891
{
79-
packer.packStructHeader( POINT_STRUCT_SIZE, POINT_STRUCT_TYPE );
80-
packer.pack( point.crsTableId() );
81-
packer.pack( point.crsCode() );
82-
packer.pack( point.coordinate().values() );
92+
packer.packStructHeader( POINT_STRUCT_SIZE, POINT_3D_STRUCT_TYPE );
93+
packer.pack( point.srid() );
94+
// packer.pack( point.x(), point.y(), point.z() );
8395
}
8496
}
8597

@@ -93,31 +105,32 @@ private static class ReaderV2 extends ReaderV1
93105
@Override
94106
Value unpackStruct( long size, byte type ) throws IOException
95107
{
96-
if ( type == POINT_STRUCT_TYPE )
108+
if ( type == POINT_2D_STRUCT_TYPE )
109+
{
110+
ensureCorrectStructSize( POINT_2D_TyCon.typeName(), POINT_STRUCT_SIZE, size );
111+
return unpackPoint2D();
112+
}
113+
else if ( type == POINT_3D_STRUCT_TYPE )
97114
{
98-
ensureCorrectStructSize( POINT_TyCon.typeName(), POINT_STRUCT_SIZE, size );
99-
return unpackPoint();
115+
ensureCorrectStructSize( POINT_3D_TyCon.typeName(), POINT_STRUCT_SIZE, size );
116+
return unpackPoint3D();
100117
}
101118
else
102119
{
103120
return super.unpackStruct( size, type );
104121
}
105122
}
106123

107-
private Value unpackPoint() throws IOException
124+
private Value unpackPoint2D() throws IOException
108125
{
109-
long crsTableId = unpacker.unpackLong();
110-
long crsCode = unpacker.unpackLong();
111-
112-
int coordinateSize = (int) unpacker.unpackListHeader();
113-
List<Double> coordinate = new ArrayList<>( coordinateSize );
114-
for ( int i = 0; i < coordinateSize; i++ )
115-
{
116-
coordinate.add( unpacker.unpackDouble() );
117-
}
126+
long srid = unpacker.unpackLong();
127+
return new Point2DValue( new InternalPoint2D( srid, 0.0, 0.0 ) );
128+
}
118129

119-
Point point = new InternalPoint( crsTableId, crsCode, new InternalCoordinate( coordinate ) );
120-
return new PointValue( point );
130+
private Value unpackPoint3D() throws IOException
131+
{
132+
long srid = unpacker.unpackLong();
133+
return new Point3DValue( new InternalPoint3D( srid, 0.0, 0.0, 0.0 ) );
121134
}
122135
}
123136
}

driver/src/main/java/org/neo4j/driver/internal/packstream/ByteArrayIncompatiblePacker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ public ByteArrayIncompatiblePacker( PackOutput out )
2727
super( out );
2828
}
2929

30+
@Override
3031
public void packBytesHeader( int size ) throws IOException
3132
{
3233
throw new PackStream.UnPackable( "Packing bytes is not supported " +
33-
"as the current server this driver connected to does not support unpack bytes." );
34+
"as the current server this driver connected to does not support unpack bytes." );
3435
}
3536
}

0 commit comments

Comments
 (0)