Skip to content

Commit

Permalink
Fix smaller review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lojjs committed Oct 4, 2022
1 parent 290c827 commit 2523252
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 82 deletions.
5 changes: 3 additions & 2 deletions common/src/main/java/apoc/convert/ConvertUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()) {
Expand Down
5 changes: 0 additions & 5 deletions common/src/main/java/apoc/util/collection/Iterables.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,10 +53,6 @@ public static <T, C extends Collection<T>> C addAll(C collection, Iterable<? ext
return collection;
}

public static <FROM, TO> Iterable<TO> map(Function<? super FROM, ? extends TO> function, Iterable<FROM> from) {
return new MapIterable<>(from, function);
}

@SafeVarargs
public static <T, C extends T> Iterable<T> iterable(C... items) {
return Arrays.asList(items);
Expand Down
19 changes: 1 addition & 18 deletions common/src/main/java/apoc/util/collection/Iterators.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static <T> T assertNotNull(Iterator<T> iterator, T result) {
* @return the {@code collection} which was passed in, now filled
* with the items from {@code iterator}.
*/
public static <C extends Collection<T>, T> C addToCollection(Iterator<T> iterator, C collection) {
private static <C extends Collection<T>, T> C addToCollection(Iterator<T> iterator, C collection) {
try {
while (iterator.hasNext()) {
collection.add(iterator.next());
Expand All @@ -123,23 +123,6 @@ public static <C extends Collection<T>, T> C addToCollection(Iterator<T> 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 <T> 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 <T> Iterable<T> loop(final Iterator<T> iterator) {
return () -> iterator;
}

/**
* Counts the number of items in the {@code iterator} by looping
* through it.
Expand Down
48 changes: 0 additions & 48 deletions common/src/main/java/apoc/util/collection/MapIterable.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 )
Expand All @@ -52,14 +50,14 @@ public static SubGraph from(Transaction tx, Result result, boolean addBetween)
{
final CypherResultSubGraph graph = new CypherResultSubGraph();
final List<String> columns = result.columns();
for ( Map<String, Object> 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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ private List<String> exportIndexes() {
Iterable<String> props = index.getPropertyKeys();
List<String> 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);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/apoc/nodes/Nodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> relationshipTypes(@Name("node") Node node, @Name(value = "types",defaultValue = "") String types) {
if (node==null) return null;
List<String> relTypes = Iterables.asList(Iterables.map(RelationshipType::name, node.getRelationshipTypes()));
List<String> relTypes = Iterables.stream(node.getRelationshipTypes()).map(RelationshipType::name).collect(Collectors.toList());
if (types == null || types.isEmpty()) return relTypes;
List<String> result = new ArrayList<>(relTypes.size());
for (Pair<RelationshipType, Direction> p : parse(types)) {
Expand Down Expand Up @@ -608,7 +608,7 @@ public List<Map<String, Object>> 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<String,Boolean> relationshipExists(@Name("node") Node node, @Name(value = "types", defaultValue = "") String types) {
if (node == null || types == null || types.isEmpty()) return null;
List<String> relTypes = Iterables.asList(Iterables.map(RelationshipType::name, node.getRelationshipTypes()));
List<String> relTypes = Iterables.stream(node.getRelationshipTypes()).map(RelationshipType::name).toList();
Map<String,Boolean> result = new HashMap<>();
for (Pair<RelationshipType, Direction> p : parse(types)) {
String name = p.first().name();
Expand Down

0 comments on commit 2523252

Please sign in to comment.