From 252325294a53531a88baf1b2f5fbafb4f9254766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louise=20S=C3=B6derstr=C3=B6m?= Date: Wed, 28 Sep 2022 14:59:53 +0200 Subject: [PATCH] Fix smaller review comments --- .../main/java/apoc/convert/ConvertUtils.java | 5 +- .../java/apoc/util/collection/Iterables.java | 5 -- .../java/apoc/util/collection/Iterators.java | 19 +------- .../apoc/util/collection/MapIterable.java | 48 ------------------- .../cypher/export/CypherResultSubGraph.java | 8 ++-- .../MultiStatementCypherSubGraphExporter.java | 4 +- core/src/main/java/apoc/nodes/Nodes.java | 4 +- 7 files changed, 11 insertions(+), 82 deletions(-) delete mode 100644 common/src/main/java/apoc/util/collection/MapIterable.java diff --git a/common/src/main/java/apoc/convert/ConvertUtils.java b/common/src/main/java/apoc/convert/ConvertUtils.java index bbf91e2f2..53f08f914 100644 --- a/common/src/main/java/apoc/convert/ConvertUtils.java +++ b/common/src/main/java/apoc/convert/ConvertUtils.java @@ -1,5 +1,6 @@ package apoc.convert; +import apoc.util.collection.Iterables; import apoc.util.collection.Iterators; import java.lang.reflect.Array; import java.util.ArrayList; @@ -18,8 +19,8 @@ public static List convertToList(Object list) { if (list == null) return null; else if (list instanceof List) return (List) list; else if (list instanceof Collection) return new ArrayList((Collection)list); - else if (list instanceof Iterable) return Iterators.addToCollection(((Iterable)list).iterator(),(List)new ArrayList<>(100)); - else if (list instanceof Iterator) return Iterators.addToCollection((Iterator)list,(List)new ArrayList<>(100)); + else if (list instanceof Iterable) return Iterables.asList((Iterable)list); + else if (list instanceof Iterator) return Iterators.asList((Iterator)list); else if (list.getClass().isArray()) { final Object[] objectArray; if (list.getClass().getComponentType().isPrimitive()) { diff --git a/common/src/main/java/apoc/util/collection/Iterables.java b/common/src/main/java/apoc/util/collection/Iterables.java index 2b5f1b729..aef6738f4 100644 --- a/common/src/main/java/apoc/util/collection/Iterables.java +++ b/common/src/main/java/apoc/util/collection/Iterables.java @@ -10,7 +10,6 @@ import java.util.NoSuchElementException; import java.util.Objects; import java.util.Set; -import java.util.function.Function; import java.util.stream.Stream; import org.neo4j.graphdb.Resource; import org.neo4j.graphdb.ResourceIterable; @@ -54,10 +53,6 @@ public static > C addAll(C collection, Iterable Iterable map(Function function, Iterable from) { - return new MapIterable<>(from, function); - } - @SafeVarargs public static Iterable iterable(C... items) { return Arrays.asList(items); diff --git a/common/src/main/java/apoc/util/collection/Iterators.java b/common/src/main/java/apoc/util/collection/Iterators.java index af5848619..8b8260fcc 100644 --- a/common/src/main/java/apoc/util/collection/Iterators.java +++ b/common/src/main/java/apoc/util/collection/Iterators.java @@ -112,7 +112,7 @@ private static T assertNotNull(Iterator iterator, T result) { * @return the {@code collection} which was passed in, now filled * with the items from {@code iterator}. */ - public static , T> C addToCollection(Iterator iterator, C collection) { + private static , T> C addToCollection(Iterator iterator, C collection) { try { while (iterator.hasNext()) { collection.add(iterator.next()); @@ -123,23 +123,6 @@ public static , T> C addToCollection(Iterator iterato } } - /** - * Convenience method for looping over an {@link Iterator}. Converts the - * {@link Iterator} to an {@link Iterable} by wrapping it in an - * {@link Iterable} that returns the {@link Iterator}. It breaks the - * contract of {@link Iterable} in that it returns the supplied iterator - * instance for each call to {@code iterator()} on the returned - * {@link Iterable} instance. This method exists to make it easy to use an - * {@link Iterator} in a for-loop. - * - * @param the type of items in the iterator. - * @param iterator the iterator to expose as an {@link Iterable}. - * @return the supplied iterator posing as an {@link Iterable}. - */ - public static Iterable loop(final Iterator iterator) { - return () -> iterator; - } - /** * Counts the number of items in the {@code iterator} by looping * through it. diff --git a/common/src/main/java/apoc/util/collection/MapIterable.java b/common/src/main/java/apoc/util/collection/MapIterable.java deleted file mode 100644 index 36ac6ff7e..000000000 --- a/common/src/main/java/apoc/util/collection/MapIterable.java +++ /dev/null @@ -1,48 +0,0 @@ -package apoc.util.collection; - - -import java.util.Iterator; -import java.util.function.Function; - -class MapIterable implements Iterable { - private final Iterable from; - private final Function function; - - MapIterable(Iterable from, Function function) { - this.from = from; - this.function = function; - } - - @Override - public Iterator iterator() { - return new MapIterator<>(from.iterator(), function); - } - - static class MapIterator implements Iterator { - private final Iterator fromIterator; - private final Function function; - - MapIterator(Iterator fromIterator, Function function) { - this.fromIterator = fromIterator; - this.function = function; - } - - @Override - public boolean hasNext() { - return fromIterator.hasNext(); - } - - @Override - public TO next() { - FROM from = fromIterator.next(); - - return function.apply(from); - } - - @Override - public void remove() { - fromIterator.remove(); - } - } -} - diff --git a/common/src/main/java/org/neo4j/cypher/export/CypherResultSubGraph.java b/common/src/main/java/org/neo4j/cypher/export/CypherResultSubGraph.java index bd2d03a8d..06a355984 100644 --- a/common/src/main/java/org/neo4j/cypher/export/CypherResultSubGraph.java +++ b/common/src/main/java/org/neo4j/cypher/export/CypherResultSubGraph.java @@ -1,7 +1,5 @@ package org.neo4j.cypher.export; -import static apoc.util.collection.Iterators.loop; - import apoc.util.collection.Iterables; import org.neo4j.graphdb.*; import org.neo4j.graphdb.schema.ConstraintDefinition; @@ -34,7 +32,7 @@ public void add( Node node ) void addNode( long id, Node data ) { nodes.put( id, data ); - labels.addAll( Iterables.addAll( new ArrayList<>(), data.getLabels() ) ); + labels.addAll( Iterables.asList( data.getLabels() ) ); } public void add( Relationship rel ) @@ -52,14 +50,14 @@ public static SubGraph from(Transaction tx, Result result, boolean addBetween) { final CypherResultSubGraph graph = new CypherResultSubGraph(); final List columns = result.columns(); - for ( Map row : loop( result ) ) + result.forEachRemaining( row -> { for ( String column : columns ) { final Object value = row.get( column ); graph.addToGraph( value ); } - } + } ); for ( IndexDefinition def : tx.schema().getIndexes() ) { if ( def.isNodeIndex() && def.getIndexType() != IndexType.LOOKUP ) diff --git a/core/src/main/java/apoc/export/cypher/MultiStatementCypherSubGraphExporter.java b/core/src/main/java/apoc/export/cypher/MultiStatementCypherSubGraphExporter.java index c557043e8..94f18c0e2 100644 --- a/core/src/main/java/apoc/export/cypher/MultiStatementCypherSubGraphExporter.java +++ b/core/src/main/java/apoc/export/cypher/MultiStatementCypherSubGraphExporter.java @@ -243,9 +243,9 @@ private List exportIndexes() { Iterable props = index.getPropertyKeys(); List tokenNames; if (isNodeIndex) { - tokenNames = Iterables.asList(Iterables.map(Label::name, index.getLabels())); + tokenNames = Iterables.stream(index.getLabels()).map(Label::name).collect(Collectors.toList()); } else { - tokenNames = Iterables.asList(Iterables.map(RelationshipType::name, index.getRelationshipTypes())); + tokenNames = Iterables.stream(index.getRelationshipTypes()).map(RelationshipType::name).collect(Collectors.toList()); } boolean inGraph = tokensInGraph(tokenNames); diff --git a/core/src/main/java/apoc/nodes/Nodes.java b/core/src/main/java/apoc/nodes/Nodes.java index f716a6eaa..df7d0d0b7 100644 --- a/core/src/main/java/apoc/nodes/Nodes.java +++ b/core/src/main/java/apoc/nodes/Nodes.java @@ -573,7 +573,7 @@ public long degreeOut(@Name("node") Node node, @Name(value = "types",defaultValu @Description("apoc.node.relationship.types(node, rel-direction-pattern) - returns a list of distinct relationship types") public List relationshipTypes(@Name("node") Node node, @Name(value = "types",defaultValue = "") String types) { if (node==null) return null; - List relTypes = Iterables.asList(Iterables.map(RelationshipType::name, node.getRelationshipTypes())); + List relTypes = Iterables.stream(node.getRelationshipTypes()).map(RelationshipType::name).collect(Collectors.toList()); if (types == null || types.isEmpty()) return relTypes; List result = new ArrayList<>(relTypes.size()); for (Pair p : parse(types)) { @@ -608,7 +608,7 @@ public List> nodesRelationshipTypes(@Name("ids") Object ids, @Description("apoc.node.relationships.exist(node, rel-direction-pattern) - returns a map with rel-pattern, boolean for the given relationship patterns") public Map relationshipExists(@Name("node") Node node, @Name(value = "types", defaultValue = "") String types) { if (node == null || types == null || types.isEmpty()) return null; - List relTypes = Iterables.asList(Iterables.map(RelationshipType::name, node.getRelationshipTypes())); + List relTypes = Iterables.stream(node.getRelationshipTypes()).map(RelationshipType::name).toList(); Map result = new HashMap<>(); for (Pair p : parse(types)) { String name = p.first().name();