2222
2323import java .lang .reflect .Type ;
2424import java .util .Map ;
25- import java .util .concurrent .Future ;
2625import java .util .regex .Pattern ;
2726
2827import com .arangodb .ArangoDBException ;
2928import com .arangodb .internal .velocystream .Communication ;
3029import com .arangodb .internal .velocystream .Connection ;
30+ import com .arangodb .util .ArangoUtil ;
3131import com .arangodb .velocypack .VPack ;
3232import com .arangodb .velocypack .VPackParser ;
3333import com .arangodb .velocypack .VPackSlice ;
3434import com .arangodb .velocypack .exception .VPackException ;
35- import com .arangodb .velocypack .exception .VPackParserException ;
3635import com .arangodb .velocystream .Response ;
3736
3837/**
@@ -48,40 +47,24 @@ public static interface ResponseDeserializer<T> {
4847 private static final String REGEX_DOCUMENT_KEY = "[^/]+" ;
4948 private static final String REGEX_DOCUMENT_ID = "[^/]+/[^/]+" ;
5049
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 ;
5754
5855 protected ArangoExecutor (final Communication <R , C > communication , final VPack vpacker , final VPack vpackerNull ,
5956 final VPackParser vpackParser , final DocumentCache documentCache , final CollectionCache collectionCache ) {
6057 super ();
6158 this .communication = communication ;
62- this .vpacker = vpacker ;
63- this .vpackerNull = vpackerNull ;
64- this .vpackParser = vpackParser ;
6559 this .documentCache = documentCache ;
6660 this .collectionCache = collectionCache ;
61+ util = new ArangoUtil (vpacker , vpackerNull , vpackParser );
6762 }
6863
6964 public Communication <R , C > communication () {
7065 return communication ;
7166 }
7267
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-
8568 public DocumentCache documentCache () {
8669 return documentCache ;
8770 }
@@ -90,6 +73,10 @@ protected CollectionCache collectionCache() {
9073 return collectionCache ;
9174 }
9275
76+ protected ArangoUtil util () {
77+ return util ;
78+ }
79+
9380 protected String createPath (final String ... params ) {
9481 final StringBuilder sb = new StringBuilder ();
9582 for (int i = 0 ; i < params .length ; i ++) {
@@ -117,94 +104,34 @@ protected void validateName(final String type, final String regex, final CharSeq
117104 }
118105
119106 @ 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 ) {
125108 return (T ) ((type != Void .class && response .getBody () != null ) ? deserialize (response .getBody (), type ) : null );
126109 }
127110
128- @ SuppressWarnings ("unchecked" )
129111 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 );
141113 }
142114
143115 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 );
155117 }
156118
157119 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 );
170121 }
171122
172123 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 );
178125 }
179126
180127 protected VPackSlice serialize (final Object entity , final Type type , final boolean serializeNullValues )
181128 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 );
188130 }
189131
190132 protected VPackSlice serialize (final Object entity , final Type type , final Map <String , Object > additionalFields )
191133 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 );
197135 }
198136
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- }
210137}
0 commit comments