-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ArrayEncoder.java
255 lines (225 loc) · 7.36 KB
/
ArrayEncoder.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
/*
* Copyright (c) 2002-2018 "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.kernel.api.index;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.util.Base64;
import org.neo4j.string.UTF8;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.storable.DateTimeValue;
import org.neo4j.values.storable.DateValue;
import org.neo4j.values.storable.DurationValue;
import org.neo4j.values.storable.LocalDateTimeValue;
import org.neo4j.values.storable.LocalTimeValue;
import org.neo4j.values.storable.TimeValue;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueWriter;
import org.neo4j.values.storable.Values;
public final class ArrayEncoder
{
private static final Base64.Encoder base64Encoder = Base64.getEncoder();
private ArrayEncoder()
{
throw new AssertionError( "Not for instantiation!" );
}
public static String encode( Value array )
{
if ( !Values.isArrayValue( array ) )
{
throw new IllegalArgumentException( "Only works with arrays" );
}
ValueEncoder encoder = new ValueEncoder();
array.writeTo( encoder );
return encoder.result();
}
static class ValueEncoder implements ValueWriter<RuntimeException>
{
private StringBuilder builder;
ValueEncoder()
{
builder = new StringBuilder();
}
public String result()
{
return builder.toString();
}
@Override
public void writeNull()
{
}
@Override
public void writeBoolean( boolean value )
{
builder.append( value );
builder.append( '|' );
}
@Override
public void writeInteger( byte value )
{
builder.append( (double)value );
builder.append( '|' );
}
@Override
public void writeInteger( short value )
{
builder.append( (double)value );
builder.append( '|' );
}
@Override
public void writeInteger( int value )
{
builder.append( (double)value );
builder.append( '|' );
}
@Override
public void writeInteger( long value )
{
builder.append( (double)value );
builder.append( '|' );
}
@Override
public void writeFloatingPoint( float value )
{
builder.append( (double)value );
builder.append( '|' );
}
@Override
public void writeFloatingPoint( double value )
{
builder.append( value );
builder.append( '|' );
}
@Override
public void writeString( String value )
{
builder.append( base64Encoder.encodeToString( UTF8.encode( value ) ) );
builder.append( '|' );
}
@Override
public void writeString( char value )
{
builder.append( base64Encoder.encodeToString( UTF8.encode( Character.toString( value ) ) ) );
builder.append( '|' );
}
@Override
public void writePoint( CoordinateReferenceSystem crs, double[] coordinate ) throws RuntimeException
{
builder.append( crs.getTable().getTableId() );
builder.append( ':' );
builder.append( crs.getCode() );
builder.append( ':' );
int index = 0;
for ( double c : coordinate )
{
if ( index > 0 )
{
builder.append( ';' );
}
builder.append( c );
index++;
}
builder.append( '|' );
}
@Override
public void writeDuration( long months, long days, long seconds, int nanos ) throws RuntimeException
{
builder.append( DurationValue.duration( months, days, seconds, nanos ).prettyPrint() );
builder.append( '|' );
}
@Override
public void writeDate( LocalDate localDate ) throws RuntimeException
{
builder.append( DateValue.date( localDate ).prettyPrint() );
builder.append( '|' );
}
@Override
public void writeLocalTime( LocalTime localTime ) throws RuntimeException
{
builder.append( LocalTimeValue.localTime( localTime ).prettyPrint() );
builder.append( '|' );
}
@Override
public void writeTime( OffsetTime offsetTime ) throws RuntimeException
{
builder.append( TimeValue.time( offsetTime ).prettyPrint() );
builder.append( '|' );
}
@Override
public void writeLocalDateTime( LocalDateTime localDateTime ) throws RuntimeException
{
builder.append( LocalDateTimeValue.localDateTime( localDateTime ).prettyPrint() );
builder.append( '|' );
}
@Override
public void writeDateTime( ZonedDateTime zonedDateTime ) throws RuntimeException
{
builder.append( DateTimeValue.datetime( zonedDateTime ).prettyPrint() );
builder.append( '|' );
}
@Override
public void beginArray( int size, ArrayType arrayType )
{
if ( size > 0 )
{
builder.append( typeChar( arrayType ) );
}
}
@Override
public void endArray()
{
}
@Override
public void writeByteArray( byte[] value )
{
builder.append( 'D' );
for ( byte b : value )
{
builder.append( (double)b );
builder.append( '|' );
}
}
private char typeChar( ArrayType arrayType )
{
switch ( arrayType )
{
case BOOLEAN: return 'Z';
case BYTE: return 'D';
case SHORT: return 'D';
case INT: return 'D';
case LONG: return 'D';
case FLOAT: return 'D';
case DOUBLE: return 'D';
case CHAR: return 'L';
case STRING: return 'L';
case POINT: return 'P';
case ZONED_DATE_TIME: return 'T';
case LOCAL_DATE_TIME: return 'T';
case DATE: return 'T';
case ZONED_TIME: return 'T';
case LOCAL_TIME: return 'T';
case DURATION: return 'A';
default: throw new UnsupportedOperationException( "Not supported array type: " + arrayType );
}
}
}
}