26
26
import java .util .List ;
27
27
import java .util .Map ;
28
28
29
+ import org .neo4j .driver .internal .value .NullValue ;
29
30
import org .neo4j .driver .v1 .exceptions .ClientException ;
30
31
import org .neo4j .driver .v1 .exceptions .value .LossyCoercion ;
31
32
import org .neo4j .driver .v1 .exceptions .value .Uncoercible ;
@@ -194,24 +195,54 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
194
195
*/
195
196
Object asObject ();
196
197
198
+ /**
199
+ * Apply the mapping function on the value if the value is not a {@link NullValue}, or the default value if the value is a {@link NullValue}.
200
+ * @param mapper The mapping function defines how to map a {@link Value} to T.
201
+ * @param defaultValue the value to return if the value is a {@link NullValue}
202
+ * @param <T> The return type
203
+ * @return The value after applying the given mapping function or the default value if the value is {@link NullValue}.
204
+ */
205
+ <T >T computeOrDefault ( Function <Value , T > mapper , T defaultValue );
206
+
197
207
/**
198
208
* @return the value as a Java boolean, if possible.
199
209
* @throws Uncoercible if value types are incompatible.
200
210
*/
201
211
boolean asBoolean ();
202
212
213
+ /**
214
+ * @param defaultValue return this value if the value is a {@link NullValue}.
215
+ * @return the value as a Java boolean, if possible.
216
+ * @throws Uncoercible if value types are incompatible.
217
+ */
218
+ boolean asBoolean ( boolean defaultValue );
219
+
203
220
/**
204
221
* @return the value as a Java byte array, if possible.
205
222
* @throws Uncoercible if value types are incompatible.
206
223
*/
207
224
byte [] asByteArray ();
208
225
226
+ /**
227
+ * @param defaultValue default to this value if the original value is a {@link NullValue}
228
+ * @return the value as a Java byte array, if possible.
229
+ * @throws Uncoercible if value types are incompatible.
230
+ */
231
+ byte [] asByteArray ( byte [] defaultValue );
232
+
209
233
/**
210
234
* @return the value as a Java String, if possible.
211
235
* @throws Uncoercible if value types are incompatible.
212
236
*/
213
237
String asString ();
214
238
239
+ /**
240
+ * @param defaultValue return this value if the value is null.
241
+ * @return the value as a Java String, if possible
242
+ * @throws Uncoercible if value types are incompatible.
243
+ */
244
+ String asString ( String defaultValue );
245
+
215
246
/**
216
247
* @return the value as a Java Number, if possible.
217
248
* @throws Uncoercible if value types are incompatible.
@@ -227,6 +258,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
227
258
*/
228
259
long asLong ();
229
260
261
+ /**
262
+ * Returns a Java long if no precision is lost in the conversion.
263
+ * @param defaultValue return this default value if the value is a {@link NullValue}.
264
+ * @return the value as a Java long.
265
+ * @throws LossyCoercion if it is not possible to convert the value without loosing precision.
266
+ * @throws Uncoercible if value types are incompatible.
267
+ */
268
+ long asLong ( long defaultValue );
269
+
230
270
/**
231
271
* Returns a Java int if no precision is lost in the conversion.
232
272
*
@@ -236,6 +276,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
236
276
*/
237
277
int asInt ();
238
278
279
+ /**
280
+ * Returns a Java int if no precision is lost in the conversion.
281
+ * @param defaultValue return this default value if the value is a {@link NullValue}.
282
+ * @return the value as a Java int.
283
+ * @throws LossyCoercion if it is not possible to convert the value without loosing precision.
284
+ * @throws Uncoercible if value types are incompatible.
285
+ */
286
+ int asInt ( int defaultValue );
287
+
239
288
/**
240
289
* Returns a Java double if no precision is lost in the conversion.
241
290
*
@@ -245,6 +294,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
245
294
*/
246
295
double asDouble ();
247
296
297
+ /**
298
+ * Returns a Java double if no precision is lost in the conversion.
299
+ * @param defaultValue default to this value if the value is a {@link NullValue}.
300
+ * @return the value as a Java double.
301
+ * @throws LossyCoercion if it is not possible to convert the value without loosing precision.
302
+ * @throws Uncoercible if value types are incompatible.
303
+ */
304
+ double asDouble ( double defaultValue );
305
+
248
306
/**
249
307
* Returns a Java float if no precision is lost in the conversion.
250
308
*
@@ -254,6 +312,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
254
312
*/
255
313
float asFloat ();
256
314
315
+ /**
316
+ * Returns a Java float if no precision is lost in the conversion.
317
+ * @param defaultValue default to this value if the value is a {@link NullValue}
318
+ * @return the value as a Java float.
319
+ * @throws LossyCoercion if it is not possible to convert the value without loosing precision.
320
+ * @throws Uncoercible if value types are incompatible.
321
+ */
322
+ float asFloat ( float defaultValue );
323
+
257
324
/**
258
325
* If the underlying type can be viewed as a list, returns a java list of
259
326
* values, where each value has been converted using {@link #asObject()}.
@@ -263,14 +330,35 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
263
330
*/
264
331
List <Object > asList ();
265
332
333
+
334
+ /**
335
+ * If the underlying type can be viewed as a list, returns a java list of
336
+ * values, where each value has been converted using {@link #asObject()}.
337
+ *
338
+ * @see #asObject()
339
+ * @param defaultValue default to this value if the value is a {@link NullValue}
340
+ * @return the value as a Java list of values, if possible
341
+ */
342
+ List <Object > asList ( List <Object > defaultValue );
343
+
266
344
/**
267
345
* @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
268
346
* as {@link Values#ofBoolean()}, {@link Values#ofList(Function)}.
269
347
* @param <T> the type of target list elements
270
348
* @see Values for a long list of built-in conversion functions
271
349
* @return the value as a list of T obtained by mapping from the list elements, if possible
272
350
*/
273
- <T > List <T > asList ( Function <Value , T > mapFunction );
351
+ <T > List <T > asList ( Function <Value ,T > mapFunction );
352
+
353
+ /**
354
+ * @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
355
+ * as {@link Values#ofBoolean()}, {@link Values#ofList(Function)}.
356
+ * @param <T> the type of target list elements
357
+ * @param defaultValue default to this value if the value is a {@link NullValue}
358
+ * @see Values for a long list of built-in conversion functions
359
+ * @return the value as a list of T obtained by mapping from the list elements, if possible
360
+ */
361
+ <T > List <T > asList ( Function <Value ,T > mapFunction , List <T > defaultValue );
274
362
275
363
/**
276
364
* @return the value as a {@link Entity}, if possible.
@@ -338,6 +426,76 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
338
426
*/
339
427
Point asPoint ();
340
428
429
+ /**
430
+ * @param defaultValue default to this value if the value is a {@link NullValue}
431
+ * @return the value as a {@link LocalDate}, if possible.
432
+ * @throws Uncoercible if value types are incompatible.
433
+ */
434
+ LocalDate asLocalDate ( LocalDate defaultValue );
435
+
436
+ /**
437
+ * @param defaultValue default to this value if the value is a {@link NullValue}
438
+ * @return the value as a {@link OffsetTime}, if possible.
439
+ * @throws Uncoercible if value types are incompatible.
440
+ */
441
+ OffsetTime asOffsetTime ( OffsetTime defaultValue );
442
+
443
+ /**
444
+ * @param defaultValue default to this value if the value is a {@link NullValue}
445
+ * @return the value as a {@link LocalTime}, if possible.
446
+ * @throws Uncoercible if value types are incompatible.
447
+ */
448
+ LocalTime asLocalTime ( LocalTime defaultValue );
449
+
450
+ /**
451
+ * @param defaultValue default to this value if the value is a {@link NullValue}
452
+ * @return the value as a {@link LocalDateTime}, if possible.
453
+ * @throws Uncoercible if value types are incompatible.
454
+ */
455
+ LocalDateTime asLocalDateTime ( LocalDateTime defaultValue );
456
+
457
+ /**
458
+ * @param defaultValue default to this value if the value is a {@link NullValue}
459
+ * @return the value as a {@link ZonedDateTime}, if possible.
460
+ * @throws Uncoercible if value types are incompatible.
461
+ */
462
+ ZonedDateTime asZonedDateTime ( ZonedDateTime defaultValue );
463
+
464
+ /**
465
+ * @param defaultValue default to this value if the value is a {@link NullValue}
466
+ * @return the value as a {@link IsoDuration}, if possible.
467
+ * @throws Uncoercible if value types are incompatible.
468
+ */
469
+ IsoDuration asIsoDuration ( IsoDuration defaultValue );
470
+
471
+ /**
472
+ * @param defaultValue default to this value if the value is a {@link NullValue}
473
+ * @return the value as a {@link Point}, if possible.
474
+ * @throws Uncoercible if value types are incompatible.
475
+ */
476
+ Point asPoint ( Point defaultValue );
477
+
478
+ /**
479
+ * Return as a map of string keys and values converted using
480
+ * {@link Value#asObject()}.
481
+ *
482
+ * This is equivalent to calling {@link #asMap(Function, Map)} with {@link Values#ofObject()}.
483
+ *
484
+ * @param defaultValue default to this value if the value is a {@link NullValue}
485
+ * @return the value as a Java map
486
+ */
487
+ Map <String , Object > asMap ( Map <String ,Object > defaultValue );
488
+
489
+ /**
490
+ * @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
491
+ * as {@link Values#ofBoolean()}, {@link Values#ofList(Function)}.
492
+ * @param <T> the type of map values
493
+ * @param defaultValue default to this value if the value is a {@link NullValue}
494
+ * @see Values for a long list of built-in conversion functions
495
+ * @return the value as a map from string keys to values of type T obtained from mapping he original map values, if possible
496
+ */
497
+ <T > Map <String , T > asMap ( Function <Value , T > mapFunction , Map <String , T > defaultValue );
498
+
341
499
@ Override
342
500
boolean equals ( Object other );
343
501
0 commit comments