2626import java .util .List ;
2727import java .util .Map ;
2828
29+ import org .neo4j .driver .internal .value .NullValue ;
2930import org .neo4j .driver .v1 .exceptions .ClientException ;
3031import org .neo4j .driver .v1 .exceptions .value .LossyCoercion ;
3132import org .neo4j .driver .v1 .exceptions .value .Uncoercible ;
@@ -194,24 +195,54 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
194195 */
195196 Object asObject ();
196197
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+
197207 /**
198208 * @return the value as a Java boolean, if possible.
199209 * @throws Uncoercible if value types are incompatible.
200210 */
201211 boolean asBoolean ();
202212
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+
203220 /**
204221 * @return the value as a Java byte array, if possible.
205222 * @throws Uncoercible if value types are incompatible.
206223 */
207224 byte [] asByteArray ();
208225
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+
209233 /**
210234 * @return the value as a Java String, if possible.
211235 * @throws Uncoercible if value types are incompatible.
212236 */
213237 String asString ();
214238
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+
215246 /**
216247 * @return the value as a Java Number, if possible.
217248 * @throws Uncoercible if value types are incompatible.
@@ -227,6 +258,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
227258 */
228259 long asLong ();
229260
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+
230270 /**
231271 * Returns a Java int if no precision is lost in the conversion.
232272 *
@@ -236,6 +276,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
236276 */
237277 int asInt ();
238278
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+
239288 /**
240289 * Returns a Java double if no precision is lost in the conversion.
241290 *
@@ -245,6 +294,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
245294 */
246295 double asDouble ();
247296
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+
248306 /**
249307 * Returns a Java float if no precision is lost in the conversion.
250308 *
@@ -254,6 +312,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
254312 */
255313 float asFloat ();
256314
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+
257324 /**
258325 * If the underlying type can be viewed as a list, returns a java list of
259326 * values, where each value has been converted using {@link #asObject()}.
@@ -263,14 +330,35 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
263330 */
264331 List <Object > asList ();
265332
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+
266344 /**
267345 * @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
268346 * as {@link Values#ofBoolean()}, {@link Values#ofList(Function)}.
269347 * @param <T> the type of target list elements
270348 * @see Values for a long list of built-in conversion functions
271349 * @return the value as a list of T obtained by mapping from the list elements, if possible
272350 */
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 );
274362
275363 /**
276364 * @return the value as a {@link Entity}, if possible.
@@ -338,6 +426,76 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
338426 */
339427 Point asPoint ();
340428
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+
341499 @ Override
342500 boolean equals ( Object other );
343501
0 commit comments