Skip to content

Add Value#asXXX(defalutValue) for primary types #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions driver/src/main/java/org/neo4j/driver/internal/value/ValueAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ public String asString()
throw new Uncoercible( type().name(), "Java String" );
}

@Override
public boolean asBoolean( boolean defaultValue )
{
return computeOrDefault( Value:: asBoolean, defaultValue );
}

@Override
public String asString( String defaultValue )
{
return computeOrDefault( (Value::asString), defaultValue );
}

@Override
public long asLong( long defaultValue )
{
return computeOrDefault( Value::asLong, defaultValue );
}

@Override
public int asInt( int defaultValue )
{
return computeOrDefault( Value::asInt, defaultValue );
}

@Override
public double asDouble( double defaultValue )
{
return computeOrDefault( Value::asDouble, defaultValue );
}

@Override
public float asFloat( float defaultValue )
{
return computeOrDefault( Value::asFloat, defaultValue );
}

@Override
public long asLong()
{
Expand Down Expand Up @@ -150,6 +186,88 @@ public Object asObject()
throw new Uncoercible( type().name(), "Java Object" );
}

@Override
public <T> T computeOrDefault( Function<Value,T> mapper, T defaultValue )
{
if ( isNull() )
{
return defaultValue;
}
return mapper.apply( this );
}

@Override
public Map<String,Object> asMap( Map<String,Object> defaultValue )
{
return computeOrDefault( Value::asMap, defaultValue );
}

@Override
public <T> Map<String,T> asMap( Function<Value,T> mapFunction, Map<String,T> defaultValue )
{
return computeOrDefault( value -> value.asMap( mapFunction ), defaultValue );
}

@Override
public byte[] asByteArray( byte[] defaultValue )
{
return computeOrDefault( Value::asByteArray, defaultValue );
}

@Override
public List<Object> asList( List<Object> defaultValue )
{
return computeOrDefault( Value::asList, defaultValue );
}

@Override
public <T> List<T> asList( Function<Value,T> mapFunction, List<T> defaultValue )
{
return computeOrDefault( value -> value.asList( mapFunction ), defaultValue );
}

@Override
public LocalDate asLocalDate( LocalDate defaultValue )
{
return computeOrDefault( Value::asLocalDate, defaultValue );
}

@Override
public OffsetTime asOffsetTime( OffsetTime defaultValue )
{
return computeOrDefault( Value::asOffsetTime, defaultValue );
}

@Override
public LocalTime asLocalTime( LocalTime defaultValue )
{
return computeOrDefault( Value::asLocalTime, defaultValue );
}

@Override
public LocalDateTime asLocalDateTime( LocalDateTime defaultValue )
{
return computeOrDefault( Value::asLocalDateTime, defaultValue );
}

@Override
public ZonedDateTime asZonedDateTime( ZonedDateTime defaultValue )
{
return computeOrDefault( Value::asZonedDateTime, defaultValue );
}

@Override
public IsoDuration asIsoDuration( IsoDuration defaultValue )
{
return computeOrDefault( Value::asIsoDuration, defaultValue );
}

@Override
public Point asPoint( Point defaultValue )
{
return computeOrDefault( Value::asPoint, defaultValue );
}

@Override
public byte[] asByteArray()
{
Expand Down
160 changes: 159 additions & 1 deletion driver/src/main/java/org/neo4j/driver/v1/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;

import org.neo4j.driver.internal.value.NullValue;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.value.LossyCoercion;
import org.neo4j.driver.v1.exceptions.value.Uncoercible;
Expand Down Expand Up @@ -194,24 +195,54 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
*/
Object asObject();

/**
* 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}.
* @param mapper The mapping function defines how to map a {@link Value} to T.
* @param defaultValue the value to return if the value is a {@link NullValue}
* @param <T> The return type
* @return The value after applying the given mapping function or the default value if the value is {@link NullValue}.
*/
<T>T computeOrDefault( Function<Value, T> mapper, T defaultValue );

/**
* @return the value as a Java boolean, if possible.
* @throws Uncoercible if value types are incompatible.
*/
boolean asBoolean();

/**
* @param defaultValue return this value if the value is a {@link NullValue}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add description and @throws Uncoercible if value types are incompatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was in the javadoc.

* @return the value as a Java boolean, if possible.
* @throws Uncoercible if value types are incompatible.
*/
boolean asBoolean( boolean defaultValue );

/**
* @return the value as a Java byte array, if possible.
* @throws Uncoercible if value types are incompatible.
*/
byte[] asByteArray();

/**
* @param defaultValue default to this value if the original value is a {@link NullValue}
* @return the value as a Java byte array, if possible.
* @throws Uncoercible if value types are incompatible.
*/
byte[] asByteArray( byte[] defaultValue );

/**
* @return the value as a Java String, if possible.
* @throws Uncoercible if value types are incompatible.
*/
String asString();

/**
* @param defaultValue return this value if the value is null.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add description and @throws Uncoercible if value types are incompatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* @return the value as a Java String, if possible
* @throws Uncoercible if value types are incompatible.
*/
String asString( String defaultValue );

/**
* @return the value as a Java Number, if possible.
* @throws Uncoercible if value types are incompatible.
Expand All @@ -227,6 +258,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
*/
long asLong();

/**
* Returns a Java long if no precision is lost in the conversion.
* @param defaultValue return this default value if the value is a {@link NullValue}.
* @return the value as a Java long.
* @throws LossyCoercion if it is not possible to convert the value without loosing precision.
* @throws Uncoercible if value types are incompatible.
*/
long asLong( long defaultValue );

/**
* Returns a Java int if no precision is lost in the conversion.
*
Expand All @@ -236,6 +276,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
*/
int asInt();

/**
* Returns a Java int if no precision is lost in the conversion.
* @param defaultValue return this default value if the value is a {@link NullValue}.
* @return the value as a Java int.
* @throws LossyCoercion if it is not possible to convert the value without loosing precision.
* @throws Uncoercible if value types are incompatible.
*/
int asInt( int defaultValue );

/**
* Returns a Java double if no precision is lost in the conversion.
*
Expand All @@ -245,6 +294,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
*/
double asDouble();

/**
* Returns a Java double if no precision is lost in the conversion.
* @param defaultValue default to this value if the value is a {@link NullValue}.
* @return the value as a Java double.
* @throws LossyCoercion if it is not possible to convert the value without loosing precision.
* @throws Uncoercible if value types are incompatible.
*/
double asDouble( double defaultValue );

/**
* Returns a Java float if no precision is lost in the conversion.
*
Expand All @@ -254,6 +312,15 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
*/
float asFloat();

/**
* Returns a Java float if no precision is lost in the conversion.
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a Java float.
* @throws LossyCoercion if it is not possible to convert the value without loosing precision.
* @throws Uncoercible if value types are incompatible.
*/
float asFloat( float defaultValue );

/**
* If the underlying type can be viewed as a list, returns a java list of
* values, where each value has been converted using {@link #asObject()}.
Expand All @@ -263,14 +330,35 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
*/
List<Object> asList();


/**
* If the underlying type can be viewed as a list, returns a java list of
* values, where each value has been converted using {@link #asObject()}.
*
* @see #asObject()
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a Java list of values, if possible
*/
List<Object> asList( List<Object> defaultValue );

/**
* @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
* as {@link Values#ofBoolean()}, {@link Values#ofList(Function)}.
* @param <T> the type of target list elements
* @see Values for a long list of built-in conversion functions
* @return the value as a list of T obtained by mapping from the list elements, if possible
*/
<T> List<T> asList( Function<Value, T> mapFunction );
<T> List<T> asList( Function<Value,T> mapFunction );

/**
* @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
* as {@link Values#ofBoolean()}, {@link Values#ofList(Function)}.
* @param <T> the type of target list elements
* @param defaultValue default to this value if the value is a {@link NullValue}
* @see Values for a long list of built-in conversion functions
* @return the value as a list of T obtained by mapping from the list elements, if possible
*/
<T> List<T> asList( Function<Value,T> mapFunction, List<T> defaultValue );

/**
* @return the value as a {@link Entity}, if possible.
Expand Down Expand Up @@ -338,6 +426,76 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
*/
Point asPoint();

/**
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a {@link LocalDate}, if possible.
* @throws Uncoercible if value types are incompatible.
*/
LocalDate asLocalDate( LocalDate defaultValue );

/**
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a {@link OffsetTime}, if possible.
* @throws Uncoercible if value types are incompatible.
*/
OffsetTime asOffsetTime( OffsetTime defaultValue );

/**
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a {@link LocalTime}, if possible.
* @throws Uncoercible if value types are incompatible.
*/
LocalTime asLocalTime( LocalTime defaultValue );

/**
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a {@link LocalDateTime}, if possible.
* @throws Uncoercible if value types are incompatible.
*/
LocalDateTime asLocalDateTime( LocalDateTime defaultValue );

/**
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a {@link ZonedDateTime}, if possible.
* @throws Uncoercible if value types are incompatible.
*/
ZonedDateTime asZonedDateTime( ZonedDateTime defaultValue );

/**
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a {@link IsoDuration}, if possible.
* @throws Uncoercible if value types are incompatible.
*/
IsoDuration asIsoDuration( IsoDuration defaultValue );

/**
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a {@link Point}, if possible.
* @throws Uncoercible if value types are incompatible.
*/
Point asPoint( Point defaultValue );

/**
* Return as a map of string keys and values converted using
* {@link Value#asObject()}.
*
* This is equivalent to calling {@link #asMap(Function, Map)} with {@link Values#ofObject()}.
*
* @param defaultValue default to this value if the value is a {@link NullValue}
* @return the value as a Java map
*/
Map<String, Object> asMap( Map<String,Object> defaultValue );

/**
* @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
* as {@link Values#ofBoolean()}, {@link Values#ofList(Function)}.
* @param <T> the type of map values
* @param defaultValue default to this value if the value is a {@link NullValue}
* @see Values for a long list of built-in conversion functions
* @return the value as a map from string keys to values of type T obtained from mapping he original map values, if possible
*/
<T> Map<String, T> asMap( Function<Value, T> mapFunction, Map<String, T> defaultValue );

@Override
boolean equals( Object other );

Expand Down
Loading