Skip to content

Commit 7c5570d

Browse files
authored
Merge pull request #481 from zhenlineo/1.6-or-default
Add `Value#asXXX(defalutValue)` for primary types
2 parents c40695e + 3637193 commit 7c5570d

File tree

3 files changed

+375
-1
lines changed

3 files changed

+375
-1
lines changed

driver/src/main/java/org/neo4j/driver/internal/value/ValueAdapter.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ public String asString()
9090
throw new Uncoercible( type().name(), "Java String" );
9191
}
9292

93+
@Override
94+
public boolean asBoolean( boolean defaultValue )
95+
{
96+
return computeOrDefault( Value:: asBoolean, defaultValue );
97+
}
98+
99+
@Override
100+
public String asString( String defaultValue )
101+
{
102+
return computeOrDefault( (Value::asString), defaultValue );
103+
}
104+
105+
@Override
106+
public long asLong( long defaultValue )
107+
{
108+
return computeOrDefault( Value::asLong, defaultValue );
109+
}
110+
111+
@Override
112+
public int asInt( int defaultValue )
113+
{
114+
return computeOrDefault( Value::asInt, defaultValue );
115+
}
116+
117+
@Override
118+
public double asDouble( double defaultValue )
119+
{
120+
return computeOrDefault( Value::asDouble, defaultValue );
121+
}
122+
123+
@Override
124+
public float asFloat( float defaultValue )
125+
{
126+
return computeOrDefault( Value::asFloat, defaultValue );
127+
}
128+
93129
@Override
94130
public long asLong()
95131
{
@@ -150,6 +186,88 @@ public Object asObject()
150186
throw new Uncoercible( type().name(), "Java Object" );
151187
}
152188

189+
@Override
190+
public <T> T computeOrDefault( Function<Value,T> mapper, T defaultValue )
191+
{
192+
if ( isNull() )
193+
{
194+
return defaultValue;
195+
}
196+
return mapper.apply( this );
197+
}
198+
199+
@Override
200+
public Map<String,Object> asMap( Map<String,Object> defaultValue )
201+
{
202+
return computeOrDefault( Value::asMap, defaultValue );
203+
}
204+
205+
@Override
206+
public <T> Map<String,T> asMap( Function<Value,T> mapFunction, Map<String,T> defaultValue )
207+
{
208+
return computeOrDefault( value -> value.asMap( mapFunction ), defaultValue );
209+
}
210+
211+
@Override
212+
public byte[] asByteArray( byte[] defaultValue )
213+
{
214+
return computeOrDefault( Value::asByteArray, defaultValue );
215+
}
216+
217+
@Override
218+
public List<Object> asList( List<Object> defaultValue )
219+
{
220+
return computeOrDefault( Value::asList, defaultValue );
221+
}
222+
223+
@Override
224+
public <T> List<T> asList( Function<Value,T> mapFunction, List<T> defaultValue )
225+
{
226+
return computeOrDefault( value -> value.asList( mapFunction ), defaultValue );
227+
}
228+
229+
@Override
230+
public LocalDate asLocalDate( LocalDate defaultValue )
231+
{
232+
return computeOrDefault( Value::asLocalDate, defaultValue );
233+
}
234+
235+
@Override
236+
public OffsetTime asOffsetTime( OffsetTime defaultValue )
237+
{
238+
return computeOrDefault( Value::asOffsetTime, defaultValue );
239+
}
240+
241+
@Override
242+
public LocalTime asLocalTime( LocalTime defaultValue )
243+
{
244+
return computeOrDefault( Value::asLocalTime, defaultValue );
245+
}
246+
247+
@Override
248+
public LocalDateTime asLocalDateTime( LocalDateTime defaultValue )
249+
{
250+
return computeOrDefault( Value::asLocalDateTime, defaultValue );
251+
}
252+
253+
@Override
254+
public ZonedDateTime asZonedDateTime( ZonedDateTime defaultValue )
255+
{
256+
return computeOrDefault( Value::asZonedDateTime, defaultValue );
257+
}
258+
259+
@Override
260+
public IsoDuration asIsoDuration( IsoDuration defaultValue )
261+
{
262+
return computeOrDefault( Value::asIsoDuration, defaultValue );
263+
}
264+
265+
@Override
266+
public Point asPoint( Point defaultValue )
267+
{
268+
return computeOrDefault( Value::asPoint, defaultValue );
269+
}
270+
153271
@Override
154272
public byte[] asByteArray()
155273
{

driver/src/main/java/org/neo4j/driver/v1/Value.java

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Map;
2828

29+
import org.neo4j.driver.internal.value.NullValue;
2930
import org.neo4j.driver.v1.exceptions.ClientException;
3031
import org.neo4j.driver.v1.exceptions.value.LossyCoercion;
3132
import 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

Comments
 (0)