Search in sources :

Example 1 with ListValueBuilder

use of org.neo4j.values.virtual.ListValueBuilder in project neo4j by neo4j.

the class ValueUtils method asListValue.

public static ListValue asListValue(List<?> collection) {
    int size = collection.size();
    if (size == 0) {
        return VirtualValues.EMPTY_LIST;
    }
    ListValueBuilder values = ListValueBuilder.newListBuilder(size);
    for (Object o : collection) {
        values.add(ValueUtils.of(o));
    }
    return values.build();
}
Also used : Point(org.neo4j.graphdb.spatial.Point) ListValueBuilder(org.neo4j.values.virtual.ListValueBuilder)

Example 2 with ListValueBuilder

use of org.neo4j.values.virtual.ListValueBuilder in project neo4j by neo4j.

the class ValueUtils method of.

/**
 * Creates an AnyValue by doing type inspection. Do not use in production code where performance is important.
 *
 * @param object the object to turned into a AnyValue
 * @return the AnyValue corresponding to object.
 */
@SuppressWarnings("unchecked")
public static AnyValue of(Object object) {
    if (object instanceof AnyValue) {
        return (AnyValue) object;
    }
    Value value = Values.unsafeOf(object, true);
    if (value != null) {
        return value;
    } else {
        if (object instanceof Entity) {
            if (object instanceof Node) {
                return fromNodeEntity((Node) object);
            } else if (object instanceof Relationship) {
                return fromRelationshipEntity((Relationship) object);
            } else {
                throw new IllegalArgumentException("Unknown entity + " + object.getClass().getName());
            }
        } else if (object instanceof Iterable<?>) {
            if (object instanceof Path) {
                return fromPath((Path) object);
            } else if (object instanceof List<?>) {
                return asListValue((List<Object>) object);
            } else {
                return asListValue((Iterable<Object>) object);
            }
        } else if (object instanceof Map<?, ?>) {
            return asMapValue((Map<String, Object>) object);
        } else if (object instanceof Iterator<?>) {
            ListValueBuilder builder = ListValueBuilder.newListBuilder();
            Iterator<?> iterator = (Iterator<?>) object;
            while (iterator.hasNext()) {
                builder.add(ValueUtils.of(iterator.next()));
            }
            return builder.build();
        } else if (object instanceof Object[]) {
            Object[] array = (Object[]) object;
            if (array.length == 0) {
                return VirtualValues.EMPTY_LIST;
            }
            ListValueBuilder builder = ListValueBuilder.newListBuilder(array.length);
            for (Object o : array) {
                builder.add(ValueUtils.of(o));
            }
            return builder.build();
        } else if (object instanceof Stream<?>) {
            return asListValue(((Stream<Object>) object).collect(Collectors.toList()));
        } else if (object instanceof Geometry) {
            return asGeometryValue((Geometry) object);
        } else {
            ClassLoader classLoader = object.getClass().getClassLoader();
            throw new IllegalArgumentException(String.format("Cannot convert %s of type %s to AnyValue, classloader=%s, classloader-name=%s", object, object.getClass().getName(), classLoader != null ? classLoader.toString() : "null", classLoader != null ? classLoader.getName() : "null"));
        }
    }
}
Also used : Path(org.neo4j.graphdb.Path) Entity(org.neo4j.graphdb.Entity) Node(org.neo4j.graphdb.Node) Geometry(org.neo4j.graphdb.spatial.Geometry) Relationship(org.neo4j.graphdb.Relationship) AnyValue(org.neo4j.values.AnyValue) AnyValue(org.neo4j.values.AnyValue) DoubleValue(org.neo4j.values.storable.DoubleValue) NodeValue(org.neo4j.values.virtual.NodeValue) Value(org.neo4j.values.storable.Value) MapValue(org.neo4j.values.virtual.MapValue) LongValue(org.neo4j.values.storable.LongValue) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) BooleanValue(org.neo4j.values.storable.BooleanValue) IntValue(org.neo4j.values.storable.IntValue) ListValue(org.neo4j.values.virtual.ListValue) TextValue(org.neo4j.values.storable.TextValue) PointValue(org.neo4j.values.storable.PointValue) PathValue(org.neo4j.values.virtual.PathValue) Iterator(java.util.Iterator) List(java.util.List) Stream(java.util.stream.Stream) Map(java.util.Map) ListValueBuilder(org.neo4j.values.virtual.ListValueBuilder)

Example 3 with ListValueBuilder

use of org.neo4j.values.virtual.ListValueBuilder in project neo4j by neo4j.

the class AbstractCypherAdapterStream method convertNotifications.

private static AnyValue convertNotifications(Iterable<Notification> notifications) {
    ListValueBuilder listValueBuilder = ListValueBuilder.newListBuilder();
    for (Notification notification : notifications) {
        // position is optional
        InputPosition pos = notification.getPosition();
        boolean includePosition = !pos.equals(InputPosition.empty);
        int size = includePosition ? 5 : 4;
        MapValueBuilder builder = new MapValueBuilder(size);
        builder.add("code", utf8Value(notification.getCode()));
        builder.add("title", utf8Value(notification.getTitle()));
        builder.add("description", utf8Value(notification.getDescription()));
        builder.add("severity", utf8Value(notification.getSeverity().toString()));
        if (includePosition) {
            // only add the position if it is not empty
            builder.add("position", VirtualValues.map(new String[] { "offset", "line", "column" }, new AnyValue[] { intValue(pos.getOffset()), intValue(pos.getLine()), intValue(pos.getColumn()) }));
        }
        listValueBuilder.add(builder.build());
    }
    return listValueBuilder.build();
}
Also used : MapValueBuilder(org.neo4j.values.virtual.MapValueBuilder) InputPosition(org.neo4j.graphdb.InputPosition) AnyValue(org.neo4j.values.AnyValue) Notification(org.neo4j.graphdb.Notification) ListValueBuilder(org.neo4j.values.virtual.ListValueBuilder)

Example 4 with ListValueBuilder

use of org.neo4j.values.virtual.ListValueBuilder in project neo4j by neo4j.

the class StringValue method splitNonRegex.

/**
 * Splits a string based on a single delimiter string
 *
 * @param input String to be split
 * @param delim delimiter, must not be not empty
 * @return the split string as a List of TextValues
 */
private static ListValue splitNonRegex(String input, String delim) {
    ListValueBuilder substrings = ListValueBuilder.newListBuilder();
    int offset = 0;
    int index;
    do {
        index = input.indexOf(delim, offset);
        offset = updateSubstringsAndOffset(substrings, offset, input, index, delim);
    } while (index != -1);
    return substrings.build();
}
Also used : ListValueBuilder(org.neo4j.values.virtual.ListValueBuilder)

Example 5 with ListValueBuilder

use of org.neo4j.values.virtual.ListValueBuilder in project neo4j by neo4j.

the class StringValue method splitNonRegex.

/**
 * Splits a string with multiple separator strings
 *
 * @param input String to be split
 * @param delims delimiters, must not be not empty
 * @return the split string as a List of TextValues
 */
private static ListValue splitNonRegex(String input, List<String> delims) {
    ListValueBuilder substrings = ListValueBuilder.newListBuilder();
    int offset = 0;
    Pair<Integer, String> nextSubstring;
    do {
        nextSubstring = firstIndexOf(input, offset, delims);
        offset = updateSubstringsAndOffset(substrings, offset, input, nextSubstring.first(), nextSubstring.other());
    } while (nextSubstring.first() != -1);
    return substrings.build();
}
Also used : ListValueBuilder(org.neo4j.values.virtual.ListValueBuilder)

Aggregations

ListValueBuilder (org.neo4j.values.virtual.ListValueBuilder)6 AnyValue (org.neo4j.values.AnyValue)3 LongValue (org.neo4j.values.storable.LongValue)2 ListValue (org.neo4j.values.virtual.ListValue)2 MapValueBuilder (org.neo4j.values.virtual.MapValueBuilder)2 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Stream (java.util.stream.Stream)1 Entity (org.neo4j.graphdb.Entity)1 InputPosition (org.neo4j.graphdb.InputPosition)1 Node (org.neo4j.graphdb.Node)1 Notification (org.neo4j.graphdb.Notification)1 Path (org.neo4j.graphdb.Path)1 Relationship (org.neo4j.graphdb.Relationship)1 Geometry (org.neo4j.graphdb.spatial.Geometry)1 Point (org.neo4j.graphdb.spatial.Point)1 BooleanValue (org.neo4j.values.storable.BooleanValue)1 DoubleValue (org.neo4j.values.storable.DoubleValue)1 IntValue (org.neo4j.values.storable.IntValue)1