use of apoc.util.Util in project neo4j-apoc-procedures by neo4j-contrib.
the class Convert method convertToList.
@SuppressWarnings("unchecked")
private <T> List<T> convertToList(Object list, Class<T> type) {
List<Object> convertedList = convertToList(list);
if (convertedList == null) {
return null;
}
Stream<T> stream = null;
Types varType = Types.of(type);
switch(varType) {
case INTEGER:
stream = (Stream<T>) convertedList.stream().map(Util::toLong);
break;
case FLOAT:
stream = (Stream<T>) convertedList.stream().map(Util::toDouble);
break;
case STRING:
stream = (Stream<T>) convertedList.stream().map(this::toString);
break;
case BOOLEAN:
stream = (Stream<T>) convertedList.stream().map(this::toBoolean);
break;
case NODE:
stream = (Stream<T>) convertedList.stream().map(this::toNode);
break;
case RELATIONSHIP:
stream = (Stream<T>) convertedList.stream().map(this::toRelationship);
break;
default:
throw new RuntimeException("Supported types are: Integer, Float, String, Boolean, Node, Relationship");
}
return stream.collect(Collectors.toList());
}
use of apoc.util.Util in project neo4j-apoc-procedures by neo4j-contrib.
the class Strings method toCypher.
@UserFunction
@Description("apoc.text.toCypher(value, {skipKeys,keepKeys,skipValues,keepValues,skipNull,node,relationship,start,end}) | tries it's best to convert the value to a cypher-property-string")
public String toCypher(@Name("value") Object value, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) {
if (config.containsKey("keepValues") && !((Collection) config.get("keepValues")).stream().noneMatch((v) -> (v.getClass().isInstance(value) || isPrimitive(value) && isPrimitive(v)) && !value.equals(v)))
return null;
else if (config.containsKey("skipValues") && ((Collection) config.get("skipValues")).contains(value))
return null;
if (value == null)
return "null";
if (value instanceof Number || value instanceof Boolean)
return value.toString();
if (value instanceof String)
return '\'' + value.toString() + '\'';
if (value instanceof Iterable)
return '[' + StreamSupport.stream(((Iterable<?>) value).spliterator(), false).map(v -> toCypher(v, config)).filter(Objects::nonNull).collect(Collectors.joining(",")) + ']';
if (value.getClass().isArray())
return '[' + Arrays.stream((Object[]) value).map(v -> toCypher(v, config)).filter(Objects::nonNull).collect(Collectors.joining(",")) + ']';
if (value instanceof Node) {
Node node = (Node) value;
String labels = StreamSupport.stream(node.getLabels().spliterator(), false).map(l -> quote(l.name())).collect(Collectors.joining(":"));
if (!labels.isEmpty())
labels = ':' + labels;
String var = cypherName(config, "node", () -> "", Util::quote);
return '(' + var + labels + ' ' + toCypher(node.getAllProperties(), config) + ')';
}
if (value instanceof Relationship) {
Relationship rel = (Relationship) value;
String type = ':' + quote(rel.getType().name());
String start = cypherName(config, "start", () -> toCypher(rel.getStartNode(), config), (s) -> '(' + quote(s) + ')');
String relationship = cypherName(config, "relationship", () -> "", Util::quote);
String end = cypherName(config, "end", () -> toCypher(rel.getEndNode(), config), (s) -> '(' + quote(s) + ')');
return start + "-[" + relationship + type + ' ' + toCypher(rel.getAllProperties(), config) + "]->" + end;
}
if (value instanceof Map) {
Map<String, Object> values = (Map<String, Object>) value;
if (config.containsKey("keepKeys")) {
values.keySet().retainAll((List<String>) (config.get("keepKeys")));
}
if (config.containsKey("skipKeys")) {
values.keySet().removeAll((List<String>) (config.get("skipKeys")));
}
return '{' + values.entrySet().stream().map((e) -> Pair.of(e.getKey(), toCypher(e.getValue(), config))).filter((p) -> p.other() != null).sorted(Comparator.comparing(Pair::first)).map((p) -> quote(p.first()) + ":" + p.other()).collect(Collectors.joining(",")) + '}';
}
return null;
}
Aggregations