22
22
23
23
import java .lang .reflect .Type ;
24
24
import java .util .Map ;
25
- import java .util .concurrent .Future ;
26
25
import java .util .regex .Pattern ;
27
26
28
27
import com .arangodb .ArangoDBException ;
29
28
import com .arangodb .internal .velocystream .Communication ;
30
29
import com .arangodb .internal .velocystream .Connection ;
30
+ import com .arangodb .util .ArangoUtil ;
31
31
import com .arangodb .velocypack .VPack ;
32
32
import com .arangodb .velocypack .VPackParser ;
33
33
import com .arangodb .velocypack .VPackSlice ;
34
34
import com .arangodb .velocypack .exception .VPackException ;
35
- import com .arangodb .velocypack .exception .VPackParserException ;
36
35
import com .arangodb .velocystream .Response ;
37
36
38
37
/**
@@ -48,40 +47,24 @@ public static interface ResponseDeserializer<T> {
48
47
private static final String REGEX_DOCUMENT_KEY = "[^/]+" ;
49
48
private static final String REGEX_DOCUMENT_ID = "[^/]+/[^/]+" ;
50
49
51
- protected final Communication <R , C > communication ;
52
- protected final VPack vpacker ;
53
- protected final VPack vpackerNull ;
54
- protected final VPackParser vpackParser ;
55
- protected final DocumentCache documentCache ;
56
- protected final CollectionCache collectionCache ;
50
+ private final Communication <R , C > communication ;
51
+ private final DocumentCache documentCache ;
52
+ private final CollectionCache collectionCache ;
53
+ private final ArangoUtil util ;
57
54
58
55
protected ArangoExecutor (final Communication <R , C > communication , final VPack vpacker , final VPack vpackerNull ,
59
56
final VPackParser vpackParser , final DocumentCache documentCache , final CollectionCache collectionCache ) {
60
57
super ();
61
58
this .communication = communication ;
62
- this .vpacker = vpacker ;
63
- this .vpackerNull = vpackerNull ;
64
- this .vpackParser = vpackParser ;
65
59
this .documentCache = documentCache ;
66
60
this .collectionCache = collectionCache ;
61
+ util = new ArangoUtil (vpacker , vpackerNull , vpackParser );
67
62
}
68
63
69
64
public Communication <R , C > communication () {
70
65
return communication ;
71
66
}
72
67
73
- protected VPack vpack () {
74
- return vpacker ;
75
- }
76
-
77
- protected VPack vpackNull () {
78
- return vpackerNull ;
79
- }
80
-
81
- protected VPackParser vpackParser () {
82
- return vpackParser ;
83
- }
84
-
85
68
public DocumentCache documentCache () {
86
69
return documentCache ;
87
70
}
@@ -90,6 +73,10 @@ protected CollectionCache collectionCache() {
90
73
return collectionCache ;
91
74
}
92
75
76
+ protected ArangoUtil util () {
77
+ return util ;
78
+ }
79
+
93
80
protected String createPath (final String ... params ) {
94
81
final StringBuilder sb = new StringBuilder ();
95
82
for (int i = 0 ; i < params .length ; i ++) {
@@ -117,94 +104,34 @@ protected void validateName(final String type, final String regex, final CharSeq
117
104
}
118
105
119
106
@ SuppressWarnings ("unchecked" )
120
- protected <T > T createResult (
121
- final VPack vpack ,
122
- final VPackParser vpackParser ,
123
- final Type type ,
124
- final Response response ) {
107
+ protected <T > T createResult (final Type type , final Response response ) {
125
108
return (T ) ((type != Void .class && response .getBody () != null ) ? deserialize (response .getBody (), type ) : null );
126
109
}
127
110
128
- @ SuppressWarnings ("unchecked" )
129
111
protected <T > T deserialize (final VPackSlice vpack , final Type type ) throws ArangoDBException {
130
- try {
131
- final T doc ;
132
- if (type == String .class && !vpack .isString ()) {
133
- doc = (T ) vpackParser .toJson (vpack );
134
- } else {
135
- doc = vpacker .deserialize (vpack , type );
136
- }
137
- return doc ;
138
- } catch (final VPackException e ) {
139
- throw new ArangoDBException (e );
140
- }
112
+ return util .deserialize (vpack , type );
141
113
}
142
114
143
115
protected VPackSlice serialize (final Object entity ) throws ArangoDBException {
144
- try {
145
- final VPackSlice vpack ;
146
- if (String .class .isAssignableFrom (entity .getClass ())) {
147
- vpack = vpackParser .fromJson ((String ) entity );
148
- } else {
149
- vpack = vpacker .serialize (entity );
150
- }
151
- return vpack ;
152
- } catch (final VPackException e ) {
153
- throw new ArangoDBException (e );
154
- }
116
+ return util .serialize (entity );
155
117
}
156
118
157
119
protected VPackSlice serialize (final Object entity , final boolean serializeNullValues ) throws ArangoDBException {
158
- try {
159
- final VPackSlice vpack ;
160
- if (String .class .isAssignableFrom (entity .getClass ())) {
161
- vpack = vpackParser .fromJson ((String ) entity , serializeNullValues );
162
- } else {
163
- final VPack vp = serializeNullValues ? vpackerNull : vpacker ;
164
- vpack = vp .serialize (entity );
165
- }
166
- return vpack ;
167
- } catch (final VPackException e ) {
168
- throw new ArangoDBException (e );
169
- }
120
+ return util .serialize (entity , serializeNullValues );
170
121
}
171
122
172
123
protected VPackSlice serialize (final Object entity , final Type type ) throws ArangoDBException {
173
- try {
174
- return vpacker .serialize (entity , type );
175
- } catch (final VPackException e ) {
176
- throw new ArangoDBException (e );
177
- }
124
+ return util .serialize (entity , type );
178
125
}
179
126
180
127
protected VPackSlice serialize (final Object entity , final Type type , final boolean serializeNullValues )
181
128
throws ArangoDBException {
182
- try {
183
- final VPack vp = serializeNullValues ? vpackerNull : vpacker ;
184
- return vp .serialize (entity , type );
185
- } catch (final VPackException e ) {
186
- throw new ArangoDBException (e );
187
- }
129
+ return util .serialize (entity , type , serializeNullValues );
188
130
}
189
131
190
132
protected VPackSlice serialize (final Object entity , final Type type , final Map <String , Object > additionalFields )
191
133
throws ArangoDBException {
192
- try {
193
- return vpacker .serialize (entity , type , additionalFields );
194
- } catch (final VPackParserException e ) {
195
- throw new ArangoDBException (e );
196
- }
134
+ return util .serialize (entity , type , additionalFields );
197
135
}
198
136
199
- protected <T > T unwrap (final Future <T > future ) throws ArangoDBException {
200
- try {
201
- return future .get ();
202
- } catch (final Exception e ) {
203
- final Throwable cause = e .getCause ();
204
- if (cause != null && ArangoDBException .class .isAssignableFrom (cause .getClass ())) {
205
- throw ArangoDBException .class .cast (cause );
206
- }
207
- throw new ArangoDBException (e );
208
- }
209
- }
210
137
}
0 commit comments