-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
VirtualValues.java
178 lines (151 loc) · 5.53 KB
/
VirtualValues.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.values.virtual;
import java.util.HashMap;
import java.util.List;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.ArrayValue;
import org.neo4j.values.storable.TextArray;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.virtual.PathValue.DirectPathValue;
/**
* Entry point to the virtual values library.
*/
@SuppressWarnings( "WeakerAccess" )
public final class VirtualValues
{
public static final MapValue EMPTY_MAP = MapValue.EMPTY;
public static final ListValue EMPTY_LIST = new ListValue.ArrayListValue( new AnyValue[0] );
private VirtualValues()
{
}
// DIRECT FACTORY METHODS
public static ListValue list( AnyValue... values )
{
return new ListValue.ArrayListValue( values );
}
public static ListValue fromList( List<AnyValue> values )
{
return new ListValue.JavaListListValue( values );
}
public static ListValue range( long start, long end, long step )
{
return new ListValue.IntegralRangeListValue( start, end, step );
}
public static ListValue fromArray( ArrayValue arrayValue )
{
return new ListValue.ArrayValueListValue( arrayValue );
}
public static ListValue slice( ListValue list, int from, int to )
{
int f = Math.max( from, 0 );
int t = Math.min( to, list.size() );
if ( f > t )
{
return EMPTY_LIST;
}
else
{
return new ListValue.ListSlice( list, f, t );
}
}
public static ListValue drop( ListValue list, int n )
{
int start = Math.max( 0, Math.min( n, list.size() ) );
return new ListValue.ListSlice( list, start, list.size() );
}
public static ListValue take( ListValue list, int n )
{
int end = Math.max( 0, Math.min( n, list.size() ) );
return new ListValue.ListSlice( list, 0, end );
}
/*
TOMBSTONE: TransformedListValue & FilteredListValue
This list value variant would lazily apply a transform/filter on a inner list. The lazy behavior made it hard
to guarantee that the transform/filter was still evaluable and correct on reading the transformed list, so
this was removed. If we want lazy values again, remember the problems of
- returning results out of Cypher combined with auto-closing iterators
- reading modified tx-state which was not visible at TransformedListValue creation
*/
public static ListValue reverse( ListValue list )
{
return new ListValue.ReversedList( list );
}
public static ListValue concat( ListValue... lists )
{
return new ListValue.ConcatList( lists );
}
public static ListValue appendToList( ListValue list, AnyValue value )
{
AnyValue[] newValues = new AnyValue[list.size() + 1];
System.arraycopy( list.asArray(), 0, newValues, 0, list.size() );
newValues[list.size()] = value;
return VirtualValues.list( newValues );
}
public static ListValue prependToList( ListValue list, AnyValue value )
{
AnyValue[] newValues = new AnyValue[list.size() + 1];
newValues[0] = value;
System.arraycopy( list.asArray(), 0, newValues, 1, list.size() );
return VirtualValues.list( newValues );
}
public static MapValue emptyMap()
{
return EMPTY_MAP;
}
public static MapValue map( String[] keys, AnyValue[] values )
{
assert keys.length == values.length;
HashMap<String,AnyValue> map = new HashMap<>( keys.length );
for ( int i = 0; i < keys.length; i++ )
{
map.put( keys[i], values[i] );
}
return new MapValue.MapWrappingMapValue( map );
}
public static NodeReference node( long id )
{
return new NodeReference( id );
}
public static RelationshipReference relationship( long id )
{
return new RelationshipReference( id );
}
public static PathValue path( NodeValue[] nodes, RelationshipValue[] relationships )
{
assert nodes != null;
assert relationships != null;
if ( (nodes.length + relationships.length) % 2 == 0 )
{
throw new IllegalArgumentException(
"Tried to construct a path that is not built like a path: even number of elements" );
}
return new DirectPathValue( nodes, relationships );
}
public static NodeValue nodeValue( long id, TextArray labels, MapValue properties )
{
return new NodeValue.DirectNodeValue( id, labels, properties );
}
public static RelationshipValue relationshipValue( long id, NodeValue startNode, NodeValue endNode, TextValue type,
MapValue properties )
{
return new RelationshipValue.DirectRelationshipValue( id, startNode, endNode, type, properties );
}
}