Skip to content

Commit

Permalink
changed Cypher to Java API
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Apr 16, 2024
1 parent 78033e6 commit 455261c
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions extended/src/main/java/apoc/temporal/TemporalExtended.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

import apoc.Extended;
import apoc.util.Util;
import org.apache.commons.collections4.ComparatorUtils;
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;
import org.neo4j.procedure.UserFunction;
import org.neo4j.values.AnyValue;
import org.neo4j.values.AnyValues;
import org.neo4j.values.Comparison;
import org.neo4j.values.storable.Values;

import java.util.Map;
import java.util.Set;

import static org.neo4j.values.AnyValues.COMPARATOR;
import static org.neo4j.values.AnyValues.TERNARY_COMPARATOR;

@Extended
public class TemporalExtended {
Expand All @@ -28,17 +38,26 @@ public Boolean overlap(@Name("start1") Object start1,

boolean acceptAdjacentSpans = Util.toBoolean(config.get(ACCEPT_ADJACENT_KEY));

String operator = acceptAdjacentSpans ? "<=" : "<";
String query = "RETURN ($start1 %1$s $end2) AND ($start2 %1$s $end1) AS value".formatted(operator);
Map<String, Object> params = Map.of("start1", start1,
"end1", end1,
"start2", start2,
"end2", end2);
AnyValue startValue1 = ValueUtils.of(start1);
AnyValue endValue2 = ValueUtils.of(end2);

AnyValue startValue2 = ValueUtils.of(start2);
AnyValue endValue1 = ValueUtils.of(end1);

Comparison comparison1 = TERNARY_COMPARATOR.ternaryCompare(startValue1, endValue2);
Comparison comparison2 = TERNARY_COMPARATOR.ternaryCompare(startValue2, endValue1);

try (Result result = tx.execute(query, params)) {
Object value = result.next().get("value");
return (Boolean) value;
// if object are incompatibles, the Comparison returns UNDEFINED
if (comparison1.equals(Comparison.UNDEFINED) || comparison2.equals(Comparison.UNDEFINED)) {
return null;
}

int range1 = comparison1.value();
int range2 = comparison2.value();
if (acceptAdjacentSpans) {
return range1 <= 0 && range2 <= 0;
}
return range1 < 0 && range2 < 0;
}

}

0 comments on commit 455261c

Please sign in to comment.