Search in sources :

Example 1 with DoubleValue

use of org.neo4j.values.storable.DoubleValue in project neo4j by neo4j.

the class ProcedureCompilation method toAnyValue.

/**
 * Takes an expression evaluating to one of the supported java values and turns
 * it into the corresponding AnyValue
 *
 * @param expression the expression to evaluate
 * @param userType the type of the expression to map
 * @return an expression properly mapped to AnyValue
 */
private static Expression toAnyValue(Expression expression, Class<?> userType, Expression context) {
    if (AnyValue.class.isAssignableFrom(userType)) {
        return nullCheck(expression, cast(userType, expression));
    }
    String type = userType.getCanonicalName();
    if (type.equals(LONG)) {
        return invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), expression);
    } else if (type.equals(BOXED_LONG)) {
        return nullCheck(expression, invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), unbox(expression)));
    } else if (type.equals(DOUBLE)) {
        return invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), expression);
    } else if (type.equals(BOXED_DOUBLE)) {
        return nullCheck(expression, invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), unbox(expression)));
    } else if (type.equals(NUMBER)) {
        return nullCheck(expression, invoke(methodReference(Values.class, NumberValue.class, "numberValue", Number.class), expression));
    } else if (type.equals(BOOLEAN)) {
        return invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), expression);
    } else if (type.equals(BOXED_BOOLEAN)) {
        return nullCheck(expression, invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), unbox(expression)));
    } else if (type.equals(STRING)) {
        return invoke(methodReference(Values.class, Value.class, "stringOrNoValue", String.class), expression);
    } else if (type.equals(BYTE_ARRAY)) {
        return nullCheck(expression, invoke(methodReference(Values.class, ByteArray.class, "byteArray", byte[].class), expression));
    } else if (type.equals(LIST)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, ListValue.class, "asListValue", Iterable.class), expression));
    } else if (type.equals(MAP)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, MapValue.class, "asMapValue", Map.class), expression));
    } else if (type.equals(ZONED_DATE_TIME)) {
        return nullCheck(expression, invoke(methodReference(DateTimeValue.class, DateTimeValue.class, "datetime", ZonedDateTime.class), expression));
    } else if (type.equals(OFFSET_TIME)) {
        return nullCheck(expression, invoke(methodReference(TimeValue.class, TimeValue.class, "time", OffsetTime.class), expression));
    } else if (type.equals(LOCAL_DATE)) {
        return nullCheck(expression, invoke(methodReference(DateValue.class, DateValue.class, "date", LocalDate.class), expression));
    } else if (type.equals(LOCAL_TIME)) {
        return nullCheck(expression, invoke(methodReference(LocalTimeValue.class, LocalTimeValue.class, "localTime", LocalTime.class), expression));
    } else if (type.equals(LOCAL_DATE_TIME)) {
        return nullCheck(expression, invoke(methodReference(LocalDateTimeValue.class, LocalDateTimeValue.class, "localDateTime", LocalDateTime.class), expression));
    } else if (type.equals(TEMPORAL_AMOUNT)) {
        return nullCheck(expression, invoke(methodReference(Values.class, DurationValue.class, "durationValue", TemporalAmount.class), expression));
    } else if (type.equals(NODE)) {
        Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
        Expression getNode = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, NodeValue.class, "fromNodeEntity", Node.class), getNode));
    } else if (type.equals(RELATIONSHIP)) {
        Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
        Expression getRelationship = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, RelationshipValue.class, "fromRelationshipEntity", Relationship.class), getRelationship));
    } else if (type.equals(PATH)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, PathValue.class, "fromPath", Path.class), expression));
    } else if (type.equals(POINT)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, PointValue.class, "asPointValue", Point.class), expression));
    } else {
        return invoke(methodReference(ValueUtils.class, AnyValue.class, "of", Object.class), expression);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Context(org.neo4j.kernel.api.procedure.Context) Path(org.neo4j.graphdb.Path) NodeValue(org.neo4j.values.virtual.NodeValue) Entity(org.neo4j.graphdb.Entity) LocalDateTimeValue(org.neo4j.values.storable.LocalDateTimeValue) DateTimeValue(org.neo4j.values.storable.DateTimeValue) PathValue(org.neo4j.values.virtual.PathValue) ListValue(org.neo4j.values.virtual.ListValue) Node(org.neo4j.graphdb.Node) Values(org.neo4j.values.storable.Values) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) LocalDate(java.time.LocalDate) LocalDateTimeValue(org.neo4j.values.storable.LocalDateTimeValue) ValueUtils(org.neo4j.kernel.impl.util.ValueUtils) DoubleValue(org.neo4j.values.storable.DoubleValue) ZonedDateTime(java.time.ZonedDateTime) Expression(org.neo4j.codegen.Expression) DateValue(org.neo4j.values.storable.DateValue) LongValue(org.neo4j.values.storable.LongValue) AnyValue(org.neo4j.values.AnyValue)

Example 2 with DoubleValue

use of org.neo4j.values.storable.DoubleValue in project neo4j by neo4j.

the class AbstractCypherAdapterStreamTest method assertMapEqualsWithDelta.

private static void assertMapEqualsWithDelta(MapValue a, MapValue b, double delta) {
    assertThat(a.size()).as("Map should have same size").isEqualTo(b.size());
    a.foreach((key, value) -> {
        // assertThat( "Missing key", b.get( key ) != Values.NO_VALUE );
        AnyValue aValue = value;
        AnyValue bValue = b.get(key);
        if (aValue instanceof MapValue) {
            assertThat(bValue instanceof MapValue).as("Value mismatch").isTrue();
            assertMapEqualsWithDelta((MapValue) aValue, (MapValue) bValue, delta);
        } else if (aValue instanceof DoubleValue) {
            assertThat(((DoubleValue) aValue).doubleValue()).as("Value mismatch").isCloseTo(((DoubleValue) bValue).doubleValue(), offset(delta));
        } else {
            assertThat(aValue).as("Value mismatch").isEqualTo(bValue);
        }
    });
}
Also used : DoubleValue(org.neo4j.values.storable.DoubleValue) AnyValue(org.neo4j.values.AnyValue) MapValue(org.neo4j.values.virtual.MapValue)

Aggregations

AnyValue (org.neo4j.values.AnyValue)2 DoubleValue (org.neo4j.values.storable.DoubleValue)2 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 ZonedDateTime (java.time.ZonedDateTime)1 Expression (org.neo4j.codegen.Expression)1 Entity (org.neo4j.graphdb.Entity)1 Node (org.neo4j.graphdb.Node)1 Path (org.neo4j.graphdb.Path)1 Context (org.neo4j.kernel.api.procedure.Context)1 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)1 ValueUtils (org.neo4j.kernel.impl.util.ValueUtils)1 DateTimeValue (org.neo4j.values.storable.DateTimeValue)1 DateValue (org.neo4j.values.storable.DateValue)1 LocalDateTimeValue (org.neo4j.values.storable.LocalDateTimeValue)1 LongValue (org.neo4j.values.storable.LongValue)1 Values (org.neo4j.values.storable.Values)1 ListValue (org.neo4j.values.virtual.ListValue)1 MapValue (org.neo4j.values.virtual.MapValue)1 NodeValue (org.neo4j.values.virtual.NodeValue)1