Skip to content

Commit 64f7ddb

Browse files
committed
Convert vavr Tuple2 to use custom implementation.
1 parent 170a069 commit 64f7ddb

File tree

14 files changed

+306
-272
lines changed

14 files changed

+306
-272
lines changed

src/main/java/org/eclipse/biscuit/datalog/Combinator.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package org.eclipse.biscuit.datalog;
77

8-
import io.vavr.Tuple2;
98
import java.io.Serializable;
109
import java.util.HashSet;
1110
import java.util.Iterator;
@@ -17,17 +16,17 @@
1716
import java.util.function.Supplier;
1817
import java.util.stream.Stream;
1918

20-
public final class Combinator implements Serializable, Iterator<Tuple2<Origin, Map<Long, Term>>> {
19+
public final class Combinator implements Serializable, Iterator<Pair<Origin, Map<Long, Term>>> {
2120
private MatchedVariables variables;
22-
private final Supplier<Stream<Tuple2<Origin, Fact>>> allFacts;
21+
private final Supplier<Stream<Pair<Origin, Fact>>> allFacts;
2322
private final List<Predicate> predicates;
24-
private final Iterator<Tuple2<Origin, Fact>> currentFacts;
23+
private final Iterator<Pair<Origin, Fact>> currentFacts;
2524
private Combinator currentIt;
2625
private final SymbolTable symbolTable;
2726

2827
private Origin currentOrigin;
2928

30-
private Optional<Tuple2<Origin, Map<Long, Term>>> nextElement;
29+
private Optional<Pair<Origin, Map<Long, Term>>> nextElement;
3130

3231
@Override
3332
public boolean hasNext() {
@@ -39,20 +38,20 @@ public boolean hasNext() {
3938
}
4039

4140
@Override
42-
public Tuple2<Origin, Map<Long, Term>> next() {
41+
public Pair<Origin, Map<Long, Term>> next() {
4342
if (this.nextElement == null || !this.nextElement.isPresent()) {
4443
this.nextElement = getNext();
4544
}
4645
if (this.nextElement == null || !this.nextElement.isPresent()) {
4746
throw new NoSuchElementException();
4847
} else {
49-
Tuple2<Origin, Map<Long, Term>> t = this.nextElement.get();
48+
Pair<Origin, Map<Long, Term>> t = this.nextElement.get();
5049
this.nextElement = Optional.empty();
5150
return t;
5251
}
5352
}
5453

55-
public Optional<Tuple2<Origin, Map<Long, Term>>> getNext() {
54+
public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
5655
if (this.predicates.isEmpty()) {
5756
final Optional<Map<Long, Term>> vOpt = this.variables.complete();
5857
if (vOpt.isEmpty()) {
@@ -67,7 +66,7 @@ public Optional<Tuple2<Origin, Map<Long, Term>>> getNext() {
6766
set.add((long) 0);
6867

6968
this.variables = new MatchedVariables(set);
70-
return Optional.of(new Tuple2<>(new Origin(), variables));
69+
return Optional.of(new Pair<>(new Origin(), variables));
7170
}
7271
}
7372

@@ -78,7 +77,7 @@ public Optional<Tuple2<Origin, Map<Long, Term>>> getNext() {
7877
while (true) {
7978
// we iterate over the facts that match the current predicate
8079
if (this.currentFacts.hasNext()) {
81-
final Tuple2<Origin, Fact> t = this.currentFacts.next();
80+
final Pair<Origin, Fact> t = this.currentFacts.next();
8281
Origin currentOrigin = t._1.clone();
8382
Fact fact = t._2;
8483

@@ -115,7 +114,7 @@ public Optional<Tuple2<Origin, Map<Long, Term>>> getNext() {
115114
if (vOpt.isEmpty()) {
116115
continue;
117116
} else {
118-
return Optional.of(new Tuple2<>(currentOrigin, vOpt.get()));
117+
return Optional.of(new Pair<>(currentOrigin, vOpt.get()));
119118
}
120119
} else {
121120
this.currentOrigin = currentOrigin;
@@ -141,11 +140,11 @@ public Optional<Tuple2<Origin, Map<Long, Term>>> getNext() {
141140
return Optional.empty();
142141
}
143142

144-
Optional<Tuple2<Origin, Map<Long, Term>>> opt = this.currentIt.getNext();
143+
Optional<Pair<Origin, Map<Long, Term>>> opt = this.currentIt.getNext();
145144

146145
if (opt.isPresent()) {
147-
Tuple2<Origin, Map<Long, Term>> t = opt.get();
148-
return Optional.of(new Tuple2<>(t._1.union(currentOrigin), t._2));
146+
Pair<Origin, Map<Long, Term>> t = opt.get();
147+
return Optional.of(new Pair<>(t._1.union(currentOrigin), t._2));
149148
} else {
150149
currentOrigin = null;
151150
currentIt = null;
@@ -156,7 +155,7 @@ public Optional<Tuple2<Origin, Map<Long, Term>>> getNext() {
156155
public Combinator(
157156
final MatchedVariables variables,
158157
final List<Predicate> predicates,
159-
Supplier<Stream<Tuple2<Origin, Fact>>> allFacts,
158+
Supplier<Stream<Pair<Origin, Fact>>> allFacts,
160159
final SymbolTable symbolTable) {
161160
this.variables = variables;
162161
this.allFacts = allFacts;

src/main/java/org/eclipse/biscuit/datalog/FactSet.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package org.eclipse.biscuit.datalog;
77

8-
import io.vavr.Tuple2;
98
import java.util.HashMap;
109
import java.util.HashSet;
1110
import java.util.Map;
@@ -71,8 +70,7 @@ public Stream stream(TrustedOrigins blockIds) {
7170
Origin o = entry.getKey();
7271
return blockIds.contains(o);
7372
})
74-
.flatMap(
75-
entry -> entry.getValue().stream().map(fact -> new Tuple2<>(entry.getKey(), fact)));
73+
.flatMap(entry -> entry.getValue().stream().map(fact -> new Pair<>(entry.getKey(), fact)));
7674
}
7775

7876
public Stream<Fact> stream() {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2019 Geoffroy Couprie <[email protected]> and Contributors to the Eclipse Foundation.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.eclipse.biscuit.datalog;
7+
8+
import java.util.Objects;
9+
10+
public final class Pair<T1, T2> {
11+
public final T1 _1;
12+
public final T2 _2;
13+
14+
public Pair(T1 t1, T2 t2) {
15+
this._1 = t1;
16+
this._2 = t2;
17+
}
18+
19+
public boolean equals(Object o) {
20+
if (o == this) {
21+
return true;
22+
}
23+
if (!(o instanceof Pair)) {
24+
return false;
25+
}
26+
Pair<?, ?> that = (Pair<?, ?>) o;
27+
return Objects.equals(this._1, that._1) && Objects.equals(this._2, that._2);
28+
}
29+
30+
public int hashCode() {
31+
int result = 1;
32+
result = 31 * result + _1.hashCode();
33+
result = 31 * result + _2.hashCode();
34+
return result;
35+
}
36+
37+
public String toString() {
38+
return "(" + this._1 + ", " + this._2 + ")";
39+
}
40+
}

src/main/java/org/eclipse/biscuit/datalog/Rule.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package org.eclipse.biscuit.datalog;
77

88
import biscuit.format.schema.Schema;
9-
import io.vavr.Tuple2;
109
import java.io.Serializable;
1110
import java.util.ArrayList;
1211
import java.util.HashSet;
@@ -46,16 +45,16 @@ public List<Scope> scopes() {
4645
return scopes;
4746
}
4847

49-
public Stream<Result<Tuple2<Origin, Fact>, Error>> apply(
50-
final Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier,
48+
public Stream<Result<Pair<Origin, Fact>, Error>> apply(
49+
final Supplier<Stream<Pair<Origin, Fact>>> factsSupplier,
5150
Long ruleOrigin,
5251
SymbolTable symbolTable) {
5352
MatchedVariables variables = variablesSet();
5453

5554
Combinator combinator = new Combinator(variables, this.body, factsSupplier, symbolTable);
56-
Spliterator<Tuple2<Origin, Map<Long, Term>>> splitItr =
55+
Spliterator<Pair<Origin, Map<Long, Term>>> splitItr =
5756
Spliterators.spliteratorUnknownSize(combinator, Spliterator.ORDERED);
58-
Stream<Tuple2<Origin, Map<Long, Term>>> stream = StreamSupport.stream(splitItr, false);
57+
Stream<Pair<Origin, Map<Long, Term>>> stream = StreamSupport.stream(splitItr, false);
5958

6059
return stream
6160
.map(
@@ -88,14 +87,14 @@ public Stream<Result<Tuple2<Origin, Fact>, Error>> apply(
8887
if (!generatedVariables.containsKey(var.value())) {
8988
// throw new Error("variables that appear in the head should appear in the body
9089
// as well");
91-
return Result.<Tuple2<Origin, Fact>, Error>err(new Error.InternalError());
90+
return Result.<Pair<Origin, Fact>, Error>err(new Error.InternalError());
9291
}
9392
p.terms().set(index, generatedVariables.get(var.value()));
9493
}
9594
}
9695

9796
origin.add(ruleOrigin);
98-
return Result.<Tuple2<Origin, Fact>, Error>ok(new Tuple2<>(origin, new Fact(p)));
97+
return Result.<Pair<Origin, Fact>, Error>ok(new Pair<>(origin, new Fact(p)));
9998
})
10099
.filter(Objects::nonNull);
101100
}
@@ -123,7 +122,7 @@ public boolean findMatch(
123122
return variables.checkExpressions(this.expressions, symbolTable).isPresent();
124123
}
125124

126-
Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier = () -> facts.stream(scope);
125+
Supplier<Stream<Pair<Origin, Fact>>> factsSupplier = () -> facts.stream(scope);
127126
var stream = this.apply(factsSupplier, origin, symbolTable);
128127
var it = stream.iterator();
129128

@@ -148,12 +147,12 @@ public boolean checkMatchAll(final FactSet facts, TrustedOrigins scope, SymbolTa
148147
return variables.checkExpressions(this.expressions, symbolTable).isPresent();
149148
}
150149

151-
Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier = () -> facts.stream(scope);
150+
Supplier<Stream<Pair<Origin, Fact>>> factsSupplier = () -> facts.stream(scope);
152151
Combinator combinator = new Combinator(variables, this.body, factsSupplier, symbolTable);
153152
boolean found = false;
154153

155154
for (Combinator it = combinator; it.hasNext(); ) {
156-
Tuple2<Origin, Map<Long, Term>> t = it.next();
155+
Pair<Origin, Map<Long, Term>> t = it.next();
157156
Map<Long, Term> generatedVariables = t._2;
158157
found = true;
159158

src/main/java/org/eclipse/biscuit/datalog/RuleSet.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@
55

66
package org.eclipse.biscuit.datalog;
77

8-
import io.vavr.Tuple2;
98
import java.util.ArrayList;
109
import java.util.HashMap;
1110
import java.util.List;
1211
import java.util.Map;
1312
import java.util.stream.Stream;
1413

1514
public final class RuleSet {
16-
private final HashMap<TrustedOrigins, List<Tuple2<Long, Rule>>> rules;
15+
private final HashMap<TrustedOrigins, List<Pair<Long, Rule>>> rules;
1716

1817
public RuleSet() {
1918
rules = new HashMap<>();
2019
}
2120

2221
public void add(Long origin, TrustedOrigins scope, Rule rule) {
2322
if (!rules.containsKey(scope)) {
24-
rules.put(scope, List.of(new Tuple2<>(origin, rule)));
23+
rules.put(scope, List.of(new Pair<>(origin, rule)));
2524
} else {
26-
rules.get(scope).add(new Tuple2<>(origin, rule));
25+
rules.get(scope).add(new Pair<>(origin, rule));
2726
}
2827
}
2928

3029
public RuleSet clone() {
3130
RuleSet newRules = new RuleSet();
3231

33-
for (Map.Entry<TrustedOrigins, List<Tuple2<Long, Rule>>> entry : this.rules.entrySet()) {
34-
List<Tuple2<Long, Rule>> l = new ArrayList<>(entry.getValue());
32+
for (Map.Entry<TrustedOrigins, List<Pair<Long, Rule>>> entry : this.rules.entrySet()) {
33+
List<Pair<Long, Rule>> l = new ArrayList<>(entry.getValue());
3534
newRules.rules.put(entry.getKey(), l);
3635
}
3736

@@ -46,7 +45,7 @@ public void clear() {
4645
this.rules.clear();
4746
}
4847

49-
public HashMap<TrustedOrigins, List<Tuple2<Long, Rule>>> getRules() {
48+
public HashMap<TrustedOrigins, List<Pair<Long, Rule>>> getRules() {
5049
return this.rules;
5150
}
5251
}

src/main/java/org/eclipse/biscuit/datalog/World.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package org.eclipse.biscuit.datalog;
77

8-
import io.vavr.Tuple2;
98
import java.io.Serializable;
109
import java.time.Instant;
1110
import java.util.HashSet;
@@ -43,10 +42,10 @@ public void run(RunLimits limits, final SymbolTable symbolTable) throws Error {
4342
while (true) {
4443
final FactSet newFacts = new FactSet();
4544

46-
for (Map.Entry<TrustedOrigins, List<Tuple2<Long, Rule>>> entry :
45+
for (Map.Entry<TrustedOrigins, List<Pair<Long, Rule>>> entry :
4746
this.rules.getRules().entrySet()) {
48-
for (Tuple2<Long, Rule> t : entry.getValue()) {
49-
Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier =
47+
for (Pair<Long, Rule> t : entry.getValue()) {
48+
Supplier<Stream<Pair<Origin, Fact>>> factsSupplier =
5049
() -> this.facts.stream(entry.getKey());
5150

5251
var stream = t._2.apply(factsSupplier, t._1, symbolTable);
@@ -57,7 +56,7 @@ public void run(RunLimits limits, final SymbolTable symbolTable) throws Error {
5756
}
5857

5958
if (res.isOk()) {
60-
Tuple2<Origin, Fact> t2 = res.getOk();
59+
Pair<Origin, Fact> t2 = res.getOk();
6160
newFacts.add(t2._1, t2._2);
6261
} else {
6362
throw res.getErr();
@@ -96,14 +95,14 @@ public FactSet queryRule(
9695
final Rule rule, Long origin, TrustedOrigins scope, SymbolTable symbolTable) throws Error {
9796
final FactSet newFacts = new FactSet();
9897

99-
Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier = () -> this.facts.stream(scope);
98+
Supplier<Stream<Pair<Origin, Fact>>> factsSupplier = () -> this.facts.stream(scope);
10099

101100
var stream = rule.apply(factsSupplier, origin, symbolTable);
102101
for (var it = stream.iterator(); it.hasNext(); ) {
103102
var res = it.next();
104103

105104
if (res.isOk()) {
106-
Tuple2<Origin, Fact> t2 = res.getOk();
105+
Pair<Origin, Fact> t2 = res.getOk();
107106
newFacts.add(t2._1, t2._2);
108107
} else {
109108
throw res.getErr();

src/main/java/org/eclipse/biscuit/token/Authorizer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package org.eclipse.biscuit.token;
77

8-
import io.vavr.Tuple2;
98
import java.time.Instant;
109
import java.util.ArrayList;
1110
import java.util.Date;
@@ -21,6 +20,7 @@
2120
import org.eclipse.biscuit.crypto.PublicKey;
2221
import org.eclipse.biscuit.datalog.FactSet;
2322
import org.eclipse.biscuit.datalog.Origin;
23+
import org.eclipse.biscuit.datalog.Pair;
2424
import org.eclipse.biscuit.datalog.RuleSet;
2525
import org.eclipse.biscuit.datalog.RunLimits;
2626
import org.eclipse.biscuit.datalog.Scope;
@@ -670,18 +670,18 @@ public RuleSet getRules() {
670670
return this.world.getRules();
671671
}
672672

673-
public List<Tuple2<Long, List<Check>>> getChecks() {
674-
List<Tuple2<Long, List<Check>>> allChecks = new ArrayList<>();
673+
public List<Pair<Long, List<Check>>> getChecks() {
674+
List<Pair<Long, List<Check>>> allChecks = new ArrayList<>();
675675
if (!this.checks.isEmpty()) {
676-
allChecks.add(new Tuple2<>(Long.MAX_VALUE, this.checks));
676+
allChecks.add(new Pair<>(Long.MAX_VALUE, this.checks));
677677
}
678678

679679
List<Check> authorityChecks = new ArrayList<>();
680680
for (org.eclipse.biscuit.datalog.Check check : this.token.authority.getChecks()) {
681681
authorityChecks.add(Check.convertFrom(check, this.token.symbolTable));
682682
}
683683
if (!authorityChecks.isEmpty()) {
684-
allChecks.add(new Tuple2<>((long) 0, authorityChecks));
684+
allChecks.add(new Pair<>((long) 0, authorityChecks));
685685
}
686686

687687
long count = 1;
@@ -700,7 +700,7 @@ public List<Tuple2<Long, List<Check>>> getChecks() {
700700
}
701701
}
702702
if (!blockChecks.isEmpty()) {
703-
allChecks.add(new Tuple2<>(count, blockChecks));
703+
allChecks.add(new Pair<>(count, blockChecks));
704704
}
705705
count += 1;
706706
}

0 commit comments

Comments
 (0)