Skip to content

Commit

Permalink
[dITjfMpu] various changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed May 24, 2023
1 parent d1641eb commit 633dd90
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 176 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ apply from: "licenses-source-header.gradle"

ext {
publicDir = "${project.rootDir}"
neo4jVersionEffective = project.hasProperty("neo4jVersionOverride") ? project.getProperty("neo4jVersionOverride") : "5.8.0"
neo4jVersionEffective = project.hasProperty("neo4jVersionOverride") ? project.getProperty("neo4jVersionOverride") : "5.9.0"
testContainersVersion = '1.17.6'
}
38 changes: 12 additions & 26 deletions common/src/main/java/apoc/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import apoc.result.VirtualNode;
import apoc.result.VirtualRelationship;
import apoc.util.collection.Iterators;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -86,7 +87,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1113,34 +1113,20 @@ public static <T> boolean valueEquals(T one, T other) {
.equals(ValueUtils.of(other));
}

// todo - parameterized???
public static <T> List<T> removeAll(Collection<T> remove, List<T> second) {
return gettStream(remove, second)
.collect(Collectors.toList());
}

public static <T> Set<T> removeAll(Collection<T> remove, Set<T> second) {
return gettStream(remove, second)
.collect(Collectors.toSet());
}


public static boolean isaBoolean(Collection<Object> second, Object i) {
return second.stream().anyMatch(i2 -> Util.valueEquals(i, i2));
public static boolean containsValueEquals(Collection<Object> collection, Object value) {
return collection.stream()
.anyMatch(i -> Util.valueEquals(value, i));
}

private static <T> Stream<T> gettStream(Collection<T> remove, Collection<T> second) {
return remove.stream()
.filter(i -> isNoneMatch(second, i));
}

private static <T> boolean isNoneMatch(Collection<T> second, T i) {
return second.stream().noneMatch(i2 -> Util.valueEquals(i, i2));
}

public static <T> List<AnyValue> toAnyValues(List<T> second) {
return second.stream()
public static <T> List<AnyValue> toAnyValues(List<T> list) {
return list.stream()
.map(ValueUtils::of)
.collect(Collectors.toList());
}

public static int indexOf(List<Object> list, Object value) {
return ListUtils.indexOf(list,
(i) -> Util.valueEquals(i, value)
);
}
}
57 changes: 21 additions & 36 deletions core/src/main/java/apoc/coll/Coll.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import apoc.result.ListResult;
import apoc.util.Util;
import com.google.common.util.concurrent.AtomicDouble;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
Expand All @@ -31,7 +30,6 @@
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.impl.util.ValueUtils;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
Expand All @@ -41,15 +39,28 @@

import java.lang.reflect.Array;
import java.text.Collator;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.RandomAccess;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static apoc.util.Util.isaBoolean;
import static apoc.util.Util.containsValueEquals;
import static apoc.util.Util.toAnyValues;
import static java.util.Arrays.asList;

Expand Down Expand Up @@ -382,12 +393,12 @@ public Stream<ListResult> split(@Name("coll") List<Object> list, @Name("value")
if (list==null || list.isEmpty()) return Stream.empty();
List<Object> l = new ArrayList<>(list);
List<List<Object>> result = new ArrayList<>(10);
int idx = extracted(l, value);
int idx = Util.indexOf(l, value);
while (idx != -1) {
List<Object> subList = l.subList(0, idx);
if (!subList.isEmpty()) result.add(subList);
l = l.subList(idx+1,l.size());
idx = extracted(l, value);// l.indexOf(value);
idx = Util.indexOf(l, value);
}
if (!l.isEmpty()) result.add(l);
return result.stream().map(ListResult::new);
Expand Down Expand Up @@ -464,35 +475,17 @@ public List<Object> remove(@Name("coll") List<Object> coll, @Name("index") long
public long indexOf(@Name("coll") List<Object> coll, @Name("value") Object value) {
// return reduce(res=[0,-1], x in $list | CASE WHEN x=$value AND res[1]=-1 THEN [res[0], res[0]+1] ELSE [res[0]+1, res[1]] END)[1] as value
if (coll == null || coll.isEmpty()) return -1;
return new ArrayList<>(toAnyValues(coll)).indexOf(ValueUtils.of(value));
}
// @UserFunction("apoc.coll.indexOf")
// @Description("Returns the index for the first occurrence of the specified value in the list.")
// public long indexOf(@Name("coll") List<Object> coll, @Name("value") Object value) {
// // return reduce(res=[0,-1], x in $list | CASE WHEN x=$value AND res[1]=-1 THEN [res[0], res[0]+1] ELSE [res[0]+1, res[1]] END)[1] as value
// return extracted(coll, value);
// }

private int extracted(List<Object> coll, Object value) {
return ListUtils.indexOf(coll,
(i) -> Util.valueEquals(i, value)// ValueUtils.of(i).equals(ValueUtils.of(value))
);
return Util.indexOf(coll, value);
}

@UserFunction("apoc.coll.containsAll")
@Description("Returns whether or not all of the given values exist in the given collection (using a HashSet).")
public boolean containsAll(@Name("coll1") List<Object> coll, @Name("coll2") List<Object> values) {
if (coll == null || coll.isEmpty() || values == null) return false;
// todo - forse anche questo??
// CollectionUtils.containsAll(coll, values, null)
Set<Object> objects = new HashSet<>(coll);
// values.stream().allMatch(i -> objects.removeIf(i1 -> i1.equals()))

return values.stream()
.allMatch( i -> isaBoolean(objects, i));
// .allMatch( i -> isaBoolean(objects, i) objects.stream().anyMatch(i1 -> Util.valueEquals(i, i1)/* i1.equals(i)*/) );
// .containsAll(values);
// return new HashSet<>(coll).containsAll(values);
.allMatch( i -> containsValueEquals(objects, i));
}

@UserFunction("apoc.coll.containsSorted")
Expand Down Expand Up @@ -622,14 +615,6 @@ public List<Object> removeAll(@Name("list1") List<Object> first, @Name("list2")
if (second!=null) list.removeAll(toAnyValues(second));
return list;
}
// @UserFunction("apoc.coll.removeAll")
// @Description("Returns the first list with all elements of the second list removed.")
// public List<Object> removeAll(@Name("list1") List<Object> first, @Name("list2") List<Object> second) {
// if (first == null) return null;
// List<Object> list = new ArrayList<>(first);
// if (second!=null) list = Util.removeAll(list, second);
// return list;
// }
@UserFunction("apoc.coll.subtract")
@Description("Returns the first list as a set with all the elements of the second list removed.")
public List<Object> subtract(@Name("list1") List<Object> first, @Name("list2") List<Object> second) {
Expand All @@ -643,8 +628,8 @@ public List<Object> subtract(@Name("list1") List<Object> first, @Name("list2") L
@Description("Returns the distinct intersection of two lists.")
public List<Object> intersection(@Name("list1") List<Object> first, @Name("list2") List<Object> second) {
if (first == null || second == null) return Collections.emptyList();
Set<Object> set =first.stream().filter(i -> isaBoolean(second, i))
.collect(Collectors.toSet());
Set<Object> set = new HashSet<>(first);
set.retainAll(second);
return new SetBackedList(set);
}

Expand Down
Loading

0 comments on commit 633dd90

Please sign in to comment.