-
Notifications
You must be signed in to change notification settings - Fork 155
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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}. | ||
* @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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add description and There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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. | ||
* | ||
|
@@ -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. | ||
* | ||
|
@@ -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. | ||
* | ||
|
@@ -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()}. | ||
|
@@ -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. | ||
|
@@ -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 ); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.