-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BaseToObjectValueWriter.java
553 lines (480 loc) · 14.6 KB
/
BaseToObjectValueWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.helpers;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.spatial.Point;
import org.neo4j.helpers.collection.ReverseArrayIterator;
import org.neo4j.values.AnyValueWriter;
import org.neo4j.values.storable.TextArray;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.virtual.EdgeValue;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.NodeValue;
import static org.neo4j.helpers.collection.Iterators.iteratorsEqual;
/**
* Base class for converting AnyValue to normal java objects.
* <p>
* This base class takes care of converting all "normal" java types such as
* number types, booleans, strings, arrays and lists. It leaves to the extending
* class to handle neo4j specific types such as nodes, edges and points.
*
* @param <E>
*/
public abstract class BaseToObjectValueWriter<E extends Exception> implements AnyValueWriter<E>
{
private final Deque<Writer> stack = new ArrayDeque<>();
public BaseToObjectValueWriter()
{
stack.push( new ObjectWriter() );
}
protected abstract Node newNodeProxyById( long id );
protected abstract Relationship newRelationshipProxyById( long id );
protected abstract Point newGeographicPoint( double longitude, double latitude, String name, int code,
String href );
protected abstract Point newCartesianPoint( double x, double y, String name, int code, String href );
public Object value()
{
assert stack.size() == 1;
return stack.getLast().value();
}
private void writeValue( Object value )
{
assert !stack.isEmpty();
Writer head = stack.peek();
head.write( value );
}
@Override
public void writeNodeReference( long nodeId ) throws RuntimeException
{
writeValue( newNodeProxyById( nodeId ) );
}
@Override
public void writeNode( long nodeId, TextArray ignore, MapValue properties ) throws RuntimeException
{
if ( nodeId >= 0 )
{
writeValue( newNodeProxyById( nodeId ) );
}
}
@Override
public void writeEdgeReference( long edgeId ) throws RuntimeException
{
writeValue( newRelationshipProxyById( edgeId ) );
}
@Override
public void writeEdge( long edgeId, long startNodeId, long endNodeId, TextValue type, MapValue properties )
throws RuntimeException
{
if ( edgeId >= 0 )
{
writeValue( newRelationshipProxyById( edgeId ) );
}
}
@Override
public void beginMap( int size ) throws RuntimeException
{
stack.push( new MapWriter( size ) );
}
@Override
public void endMap() throws RuntimeException
{
assert !stack.isEmpty();
writeValue( stack.pop().value() );
}
@Override
public void beginList( int size ) throws RuntimeException
{
stack.push( new ListWriter( size ) );
}
@Override
public void endList() throws RuntimeException
{
assert !stack.isEmpty();
writeValue( stack.pop().value() );
}
@Override
public void writePath( NodeValue[] nodes, EdgeValue[] edges ) throws RuntimeException
{
assert nodes != null;
assert nodes.length > 0;
assert edges != null;
assert nodes.length == edges.length + 1;
Node[] nodeProxies = new Node[nodes.length];
for ( int i = 0; i < nodes.length; i++ )
{
nodeProxies[i] = newNodeProxyById( nodes[i].id() );
}
Relationship[] relationship = new Relationship[edges.length];
for ( int i = 0; i < edges.length; i++ )
{
relationship[i] = newRelationshipProxyById( edges[i].id() );
}
writeValue( new Path()
{
@Override
public Node startNode()
{
return nodeProxies[0];
}
@Override
public Node endNode()
{
return nodeProxies[nodeProxies.length - 1];
}
@Override
public Relationship lastRelationship()
{
return relationship[relationship.length - 1];
}
@Override
public Iterable<Relationship> relationships()
{
return Arrays.asList( relationship );
}
@Override
public Iterable<Relationship> reverseRelationships()
{
return () -> new ReverseArrayIterator<>( relationship );
}
@Override
public Iterable<Node> nodes()
{
return Arrays.asList( nodeProxies );
}
@Override
public Iterable<Node> reverseNodes()
{
return () -> new ReverseArrayIterator<>( nodeProxies );
}
@Override
public int length()
{
return relationship.length;
}
@Override
public int hashCode()
{
if ( relationship.length == 0 )
{
return startNode().hashCode();
}
else
{
return Arrays.hashCode( relationship );
}
}
@Override
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
else if ( obj instanceof Path )
{
Path other = (Path) obj;
return startNode().equals( other.startNode() ) &&
iteratorsEqual( this.relationships().iterator(), other.relationships().iterator() );
}
else
{
return false;
}
}
@Override
public Iterator<PropertyContainer> iterator()
{
return new Iterator<PropertyContainer>()
{
Iterator<? extends PropertyContainer> current = nodes().iterator();
Iterator<? extends PropertyContainer> next = relationships().iterator();
public boolean hasNext()
{
return current.hasNext();
}
public PropertyContainer next()
{
try
{
return current.next();
}
finally
{
Iterator<? extends PropertyContainer> temp = current;
current = next;
next = temp;
}
}
public void remove()
{
next.remove();
}
};
}
} );
}
@Override
public void beginPoint( CoordinateReferenceSystem coordinateReferenceSystem ) throws RuntimeException
{
stack.push( new PointWriter( coordinateReferenceSystem ) );
}
@Override
public void endPoint() throws RuntimeException
{
assert !stack.isEmpty();
writeValue( stack.pop().value() );
}
@Override
public void writeNull() throws RuntimeException
{
writeValue( null );
}
@Override
public void writeBoolean( boolean value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeInteger( byte value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeInteger( short value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeInteger( int value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeInteger( long value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeFloatingPoint( float value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeFloatingPoint( double value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeString( String value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeString( char value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeString( char[] value, int offset, int length ) throws RuntimeException
{
writeValue( new String( value, offset, length ) );
}
@Override
public void beginArray( int size, ArrayType arrayType ) throws RuntimeException
{
stack.push( new ArrayWriter( size, arrayType ) );
}
@Override
public void endArray() throws RuntimeException
{
assert !stack.isEmpty();
writeValue( stack.pop().value() );
}
@Override
public void writeByteArray( byte[] value ) throws RuntimeException
{
writeValue( value );
}
@Override
public void writeArbitraryJavaObject( Object object )
{
writeValue( object );
}
private interface Writer
{
void write( Object value );
Object value();
}
private static class ObjectWriter implements Writer
{
private Object value;
@Override
public void write( Object value )
{
this.value = value;
}
@Override
public Object value()
{
return value;
}
}
private static class MapWriter implements Writer
{
private String key;
private boolean isKey = true;
private final HashMap<String,Object> map;
MapWriter( int size )
{
this.map = new HashMap<>( size );
}
@Override
public void write( Object value )
{
if ( isKey )
{
key = (String) value;
isKey = false;
}
else
{
map.put( key, value );
isKey = true;
}
}
@Override
public Object value()
{
return map;
}
}
private static class ArrayWriter implements Writer
{
protected final Object array;
private int index;
ArrayWriter( int size, ArrayType arrayType )
{
switch ( arrayType )
{
case SHORT:
this.array = Array.newInstance( short.class, size );
break;
case INT:
this.array = Array.newInstance( int.class, size );
break;
case BYTE:
this.array = Array.newInstance( byte.class, size );
break;
case LONG:
this.array = Array.newInstance( long.class, size );
break;
case FLOAT:
this.array = Array.newInstance( float.class, size );
break;
case DOUBLE:
this.array = Array.newInstance( double.class, size );
break;
case BOOLEAN:
this.array = Array.newInstance( boolean.class, size );
break;
case STRING:
this.array = Array.newInstance( String.class, size );
break;
case CHAR:
this.array = Array.newInstance( char.class, size );
break;
default:
this.array = new Object[size];
}
}
@Override
public void write( Object value )
{
Array.set( array, index++, value );
}
@Override
public Object value()
{
return array;
}
}
private static class ListWriter implements Writer
{
private final List<Object> list;
ListWriter( int size )
{
this.list = new ArrayList<>( size );
}
@Override
public void write( Object value )
{
list.add( value );
}
@Override
public Object value()
{
return list;
}
}
private class PointWriter implements Writer
{
//TODO it is quite silly that the point writer doesn't give me the whole thing at once
private final double[] coordinates = new double[2];
private int index;
private final CoordinateReferenceSystem crs;
PointWriter( CoordinateReferenceSystem crs )
{
this.crs = crs;
}
@Override
public void write( Object value )
{
coordinates[index++] = (double) value;
}
@Override
public Object value()
{
if ( crs.code() == CoordinateReferenceSystem.WGS84.code() )
{
return newGeographicPoint( coordinates[0], coordinates[1], crs.name, crs.code, crs.href );
}
else if ( crs.code() == CoordinateReferenceSystem.Cartesian.code() )
{
return newCartesianPoint( coordinates[0], coordinates[1], crs.name, crs.code, crs.href );
}
else
{
throw new IllegalArgumentException( crs + " is not a supported coordinate reference system" );
}
}
}
}