diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/BlockSignatureBuffer.java b/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/BlockSignatureBuffer.java index 579e4334..860c5c65 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/BlockSignatureBuffer.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/BlockSignatureBuffer.java @@ -84,9 +84,7 @@ public static byte[] generateBlockSignaturePayloadV0( } var toVerify = ByteBuffer.allocate(capacity).order(ByteOrder.LITTLE_ENDIAN); toVerify.put(payload); - if (externalSignature.isPresent()) { - toVerify.put(externalSignature.get().getSignature()); - } + externalSignature.ifPresent(signature -> toVerify.put(signature.getSignature())); toVerify.putInt(nextKey.getAlgorithm().getNumber()); toVerify.put(nextKeyBytes); toVerify.flip(); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java index 9094c1da..438a718f 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java @@ -55,8 +55,8 @@ public Schema.CheckV2 serialize() { default: } - for (int i = 0; i < this.queries.size(); i++) { - b.addQueries(this.queries.get(i).serialize()); + for (Rule query : this.queries) { + b.addQueries(query.serialize()); } return b.build(); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Combinator.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Combinator.java index 87ccd00a..3cc65915 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Combinator.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Combinator.java @@ -39,10 +39,10 @@ public boolean hasNext() { @Override public Pair> next() { - if (this.nextElement == null || !this.nextElement.isPresent()) { + if (this.nextElement == null || this.nextElement.isEmpty()) { this.nextElement = getNext(); } - if (this.nextElement == null || !this.nextElement.isPresent()) { + if (this.nextElement == null || this.nextElement.isEmpty()) { throw new NoSuchElementException(); } else { Pair> t = this.nextElement.get(); @@ -136,10 +136,6 @@ public Optional>> getNext() { } } - if (this.currentIt == null) { - return Optional.empty(); - } - Optional>> opt = this.currentIt.getNext(); if (opt.isPresent()) { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java index 19a6b34c..a28801be 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java @@ -63,7 +63,7 @@ public void merge(FactSet other) { } } - public Stream stream(TrustedOrigins blockIds) { + public Stream> stream(TrustedOrigins blockIds) { return facts.entrySet().stream() .filter( entry -> { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MatchedVariables.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MatchedVariables.java index f29b18db..7cbc0187 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MatchedVariables.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MatchedVariables.java @@ -36,7 +36,7 @@ public Optional get(final long key) { } public boolean isComplete() { - return this.variables.values().stream().allMatch((v) -> v.isPresent()); + return this.variables.values().stream().allMatch(Optional::isPresent); } public Optional> complete() { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java index c3a1a6f3..a7f0ee98 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java @@ -47,8 +47,7 @@ public boolean match(final Predicate rulePredicate) { } public Predicate clone() { - final List terms = new ArrayList<>(); - terms.addAll(this.terms); + final List terms = new ArrayList<>(this.terms); return new Predicate(this.name, terms); } @@ -78,19 +77,17 @@ public int hashCode() { public String toString() { return this.name + "(" - + String.join( - ", ", - this.terms.stream() - .map((i) -> (i == null) ? "(null)" : i.toString()) - .collect(Collectors.toList())) + + this.terms.stream() + .map((i) -> (i == null) ? "(null)" : i.toString()) + .collect(Collectors.joining(", ")) + ")"; } public Schema.PredicateV2 serialize() { Schema.PredicateV2.Builder builder = Schema.PredicateV2.newBuilder().setName(this.name); - for (int i = 0; i < this.terms.size(); i++) { - builder.addTerms(this.terms.get(i).serializeTerm()); + for (Term term : this.terms) { + builder.addTerms(term.serializeTerm()); } return builder.build(); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java index e7457720..93e96aba 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java @@ -86,14 +86,14 @@ public Stream, Error>> apply( if (!generatedVariables.containsKey(var.value())) { // throw new Error("variables that appear in the head should appear in the body // as well"); - return Stream.of(Result., Error>err(new Error.InternalError())); + return Stream.of(Result.err(new Error.InternalError())); } p.terms().set(index, generatedVariables.get(var.value())); } } origin.add(ruleOrigin); - return Stream.of(Result., Error>ok(new Pair<>(origin, new Fact(p)))); + return Stream.of(Result.ok(new Pair<>(origin, new Fact(p)))); }); } @@ -194,12 +194,12 @@ public Rule( public Schema.RuleV2 serialize() { Schema.RuleV2.Builder b = Schema.RuleV2.newBuilder().setHead(this.head.serialize()); - for (int i = 0; i < this.body.size(); i++) { - b.addBody(this.body.get(i).serialize()); + for (Predicate predicate : this.body) { + b.addBody(predicate.serialize()); } - for (int i = 0; i < this.expressions.size(); i++) { - b.addExpressions(this.expressions.get(i).serialize()); + for (Expression expression : this.expressions) { + b.addExpressions(expression.serialize()); } for (Scope scope : this.scopes) { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/SchemaVersion.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/SchemaVersion.java index 28f3f5c5..6542a243 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/SchemaVersion.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/SchemaVersion.java @@ -6,6 +6,7 @@ package org.eclipse.biscuit.datalog; import java.util.List; +import java.util.Objects; import java.util.Optional; import org.eclipse.biscuit.crypto.PublicKey; import org.eclipse.biscuit.datalog.expressions.Expression; @@ -130,10 +131,8 @@ private static boolean containsV33Op(List expressions) { for (Op op : e.getOps()) { if (op instanceof Op.Unary) { Op.Unary b = (Op.Unary) op; - switch (b.getOp()) { - case TypeOf: - return true; - default: + if (Objects.requireNonNull(b.getOp()) == Op.UnaryOp.TypeOf) { + return true; } } else if (op instanceof Op.Binary) { Op.Binary b = (Op.Binary) op; @@ -174,9 +173,6 @@ private static boolean containsV33Term(Term term) { if (term instanceof Term.Array) { return true; } - if (term instanceof Term.Map) { - return true; - } - return false; + return term instanceof Term.Map; } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Scope.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Scope.java index 1659dcd5..0d573352 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Scope.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Scope.java @@ -16,8 +16,8 @@ public enum Kind { PublicKey } - private Kind kind; - private long publicKey; + private final Kind kind; + private final long publicKey; private Scope(Kind kind, long publicKey) { this.kind = kind; @@ -106,7 +106,7 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = kind != null ? kind.hashCode() : 0; - result = 31 * result + (int) (publicKey ^ (publicKey >>> 32)); + result = 31 * result + Long.hashCode(publicKey); return result; } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/SymbolTable.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/SymbolTable.java index d95b49b3..43d0c76c 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/SymbolTable.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/SymbolTable.java @@ -72,7 +72,7 @@ public String fromEpochIsoDate(long epochSec) { private final List publicKeys; public long insert(final String symbol) { - int index = this.DEFAULT_SYMBOLS.indexOf(symbol); + int index = DEFAULT_SYMBOLS.indexOf(symbol); if (index == -1) { index = this.symbols.indexOf(symbol); if (index == -1) { @@ -114,7 +114,7 @@ public Term add(final String symbol) { public Optional get(final String symbol) { // looking for symbol in default symbols - long index = this.DEFAULT_SYMBOLS.indexOf(symbol); + long index = DEFAULT_SYMBOLS.indexOf(symbol); if (index == -1) { // looking for symbol in usages defined symbols index = this.symbols.indexOf(symbol); @@ -129,8 +129,8 @@ public Optional get(final String symbol) { } public Optional getSymbol(int i) { - if (i >= 0 && i < this.DEFAULT_SYMBOLS.size() && i < DEFAULT_SYMBOLS_OFFSET) { - return Optional.of(this.DEFAULT_SYMBOLS.get(i)); + if (i >= 0 && i < DEFAULT_SYMBOLS.size() && i < DEFAULT_SYMBOLS_OFFSET) { + return Optional.of(DEFAULT_SYMBOLS.get(i)); } else if (i >= DEFAULT_SYMBOLS_OFFSET && i < this.symbols.size() + DEFAULT_SYMBOLS_OFFSET) { return Optional.of(this.symbols.get(i - DEFAULT_SYMBOLS_OFFSET)); } else { @@ -155,9 +155,9 @@ public String formatRule(final Rule r) { public String formatRuleBody(final Rule r) { final List preds = - r.body().stream().map((p) -> this.formatPredicate(p)).collect(Collectors.toList()); + r.body().stream().map(this::formatPredicate).collect(Collectors.toList()); final List expressions = - r.expressions().stream().map((c) -> this.formatExpression(c)).collect(Collectors.toList()); + r.expressions().stream().map(this::formatExpression).collect(Collectors.toList()); String res = String.join(", ", preds); if (!expressions.isEmpty()) { @@ -170,7 +170,7 @@ public String formatRuleBody(final Rule r) { if (!r.scopes().isEmpty()) { res += " trusting "; final List scopes = - r.scopes().stream().map((s) -> this.formatScope(s)).collect(Collectors.toList()); + r.scopes().stream().map(this::formatScope).collect(Collectors.toList()); res += String.join(", ", scopes); } return res; @@ -199,13 +199,7 @@ public String formatScope(final Scope scope) { } public String formatPredicate(final Predicate p) { - List ids = - p.terms().stream() - .map( - (t) -> { - return this.formatTerm(t); - }) - .collect(Collectors.toList()); + List ids = p.terms().stream().map(this::formatTerm).collect(Collectors.toList()); return Optional.ofNullable(this.formatSymbol((int) p.name())).orElse("") + "(" + String.join(", ", ids) @@ -220,15 +214,14 @@ public String formatTerm(final Term i) { } else if (i instanceof Term.Date) { return fromEpochIsoDate(((Term.Date) i).value()); } else if (i instanceof Term.Integer) { - return "" + ((Term.Integer) i).value(); + return String.valueOf(((Term.Integer) i).value()); } else if (i instanceof Term.Str) { return "\"" + this.formatSymbol((int) ((Term.Str) i).value()) + "\""; } else if (i instanceof Term.Bytes) { return "hex:" + Utils.byteArrayToHexString(((Term.Bytes) i).value()).toLowerCase(); } else if (i instanceof Term.Set) { final List values = - ((Term.Set) i) - .value().stream().map((v) -> this.formatTerm(v)).collect(Collectors.toList()); + ((Term.Set) i).value().stream().map(this::formatTerm).collect(Collectors.toList()); if (values.isEmpty()) { return "{,}"; } else { @@ -239,11 +232,10 @@ public String formatTerm(final Term i) { return "null"; } else if (i instanceof Term.Array) { final List values = - ((Term.Array) i) - .value().stream().map((v) -> this.formatTerm(v)).collect(Collectors.toList()); + ((Term.Array) i).value().stream().map(this::formatTerm).collect(Collectors.toList()); return "[" + String.join(", ", values) + "]"; } else if (i instanceof Term.Map) { - ArrayList entries = new ArrayList(); + ArrayList entries = new ArrayList<>(); for (Map.Entry entry : ((Term.Map) i).value().entrySet()) { entries.add(formatTerm(entry.getKey()) + ": " + formatTerm(entry.getValue())); } @@ -275,15 +267,15 @@ public String formatCheck(final Check c) { break; } final List queries = - c.queries().stream().map((q) -> this.formatRuleBody(q)).collect(Collectors.toList()); + c.queries().stream().map(this::formatRuleBody).collect(Collectors.toList()); return prefix + String.join(" or ", queries); } public String formatWorld(final World w) { final List facts = - w.getFacts().stream().map((f) -> this.formatFact(f)).collect(Collectors.toList()); + w.getFacts().stream().map(this::formatFact).collect(Collectors.toList()); final List rules = - w.getRules().stream().map((r) -> this.formatRule(r)).collect(Collectors.toList()); + w.getRules().stream().map(this::formatRule).collect(Collectors.toList()); StringBuilder b = new StringBuilder(); b.append("World {\n\tfacts: [\n\t\t"); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/TemporarySymbolTable.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/TemporarySymbolTable.java index d2b91b90..9dca6279 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/TemporarySymbolTable.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/TemporarySymbolTable.java @@ -12,9 +12,9 @@ import java.util.Optional; public final class TemporarySymbolTable { - private SymbolTable base; - private int offset; - private List symbols; + private final SymbolTable base; + private final int offset; + private final List symbols; public TemporarySymbolTable(SymbolTable base) { this.offset = DEFAULT_SYMBOLS_OFFSET + base.currentOffset(); @@ -42,7 +42,7 @@ public long insert(final String symbol) { int index = this.symbols.indexOf(symbol); if (index != -1) { - return (long) (this.offset + index); + return this.offset + index; } this.symbols.add(symbol); return this.symbols.size() - 1 + this.offset; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java index 2d7dfe80..9ca22a69 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java @@ -103,11 +103,7 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { - return false; - } - - return true; + return o != null && getClass() == o.getClass(); } @Override @@ -177,7 +173,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return (int) (value ^ (value >>> 32)); + return Long.hashCode(value); } @Override @@ -244,12 +240,12 @@ public boolean equals(Object o) { @Override public int hashCode() { - return (int) (value ^ (value >>> 32)); + return Long.hashCode(value); } @Override public String toString() { - return "" + this.value; + return String.valueOf(this.value); } public Schema.TermV2 serializeTerm() { @@ -395,7 +391,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return (int) (value ^ (value >>> 32)); + return Long.hashCode(value); } public Schema.TermV2 serializeTerm() { @@ -470,7 +466,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return (int) (value ^ (value >>> 32)); + return Long.hashCode(value); } @Override @@ -543,7 +539,7 @@ public int hashCode() { @Override public String toString() { - return "" + this.value; + return String.valueOf(this.value); } public Schema.TermV2 serializeTerm() { @@ -610,7 +606,7 @@ public int hashCode() { @Override public String toString() { - return "" + value; + return String.valueOf(value); } public Schema.TermV2 serializeTerm() { @@ -712,7 +708,7 @@ public int hashCode() { @Override public String toString() { - return "" + value; + return String.valueOf(value); } public Schema.TermV2 serializeTerm() { @@ -737,7 +733,7 @@ public static Result deserializeV2(Schema.TermV2 term) return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected map")); } - java.util.HashMap values = new HashMap(); + java.util.HashMap values = new HashMap<>(); Schema.Map s = term.getMap(); for (Schema.MapEntry l : s.getEntriesList()) { @@ -819,16 +815,16 @@ public int hashCode() { @Override public String toString() { - if (value.size() == 0) { + if (value.isEmpty()) { return "{,}"; } - String s = "{"; + StringBuilder s = new StringBuilder("{"); int count = 0; for (Term elem : value) { - s += elem; + s.append(elem); count += 1; if (count < value.size()) { - s += ", "; + s.append(", "); } } return s + "}"; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/World.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/World.java index e7ff9d71..5c42083d 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/World.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/World.java @@ -147,7 +147,7 @@ public String print(SymbolTable symbolTable) { s.append("World {\n\t\tfacts: ["); for (Map.Entry> entry : this.facts.facts().entrySet()) { - s.append("\n\t\t\t" + entry.getKey() + ":"); + s.append("\n\t\t\t").append(entry.getKey()).append(":"); for (Fact f : entry.getValue()) { s.append("\n\t\t\t\t"); s.append(symbolTable.formatFact(f)); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Expression.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Expression.java index e6917f0e..b568fecc 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Expression.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Expression.java @@ -32,7 +32,7 @@ public ArrayList getOps() { // FIXME: should return a Result public Term evaluate(Map variables, TemporarySymbolTable temporarySymbolTable) throws Error.Execution { - Deque stack = new ArrayDeque(16); // Default value + Deque stack = new ArrayDeque<>(16); // Default value for (Op op : ops) { op.evaluate(stack, variables, temporarySymbolTable); } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java index e1aae371..bc26c1e5 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java @@ -6,7 +6,7 @@ package org.eclipse.biscuit.datalog.expressions; import biscuit.format.schema.Schema; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; @@ -14,6 +14,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import org.eclipse.biscuit.datalog.MapKey; @@ -90,11 +91,7 @@ public void evaluate( if (s.isEmpty()) { throw new Error.Execution("string not found in symbols for id" + value); } else { - try { - stack.push(new Term.Integer(s.get().getBytes("UTF-8").length)); - } catch (UnsupportedEncodingException e) { - throw new Error.Execution("cannot calculate string length: " + e.toString()); - } + stack.push(new Term.Integer(s.get().getBytes(StandardCharsets.UTF_8).length)); } } else if (value instanceof Term.Bytes) { stack.push(new Term.Integer(((Term.Bytes) value).value().length)); @@ -536,7 +533,7 @@ public void evaluate( Closure closure = (Closure) right; Term result = closure.call(variables, temporarySymbolTable); if (result instanceof Term.Bool) { - stack.push((Term.Bool) result); + stack.push(result); } } else { stack.push(new Term.Bool(false)); @@ -551,14 +548,14 @@ public void evaluate( Closure closure = (Closure) right; Term result = closure.call(variables, temporarySymbolTable); if (result instanceof Term.Bool) { - stack.push((Term.Bool) result); + stack.push(result); } } } break; case Intersection: if (right instanceof Term.Set && left instanceof Term.Set) { - HashSet intersec = new HashSet(); + HashSet intersec = new HashSet<>(); HashSet setRight = ((Term.Set) right).value(); HashSet setLeft = ((Term.Set) left).value(); for (Term locId : setRight) { @@ -571,7 +568,7 @@ public void evaluate( break; case Union: if (right instanceof Term.Set && left instanceof Term.Set) { - HashSet union = new HashSet(); + HashSet union = new HashSet<>(); HashSet setRight = ((Term.Set) right).value(); HashSet setLeft = ((Term.Set) left).value(); union.addAll(setRight); @@ -608,22 +605,14 @@ public void evaluate( stack.push(new Term.Null()); } else { Term element = array.get(index); - if (element != null) { - stack.push(element); - } else { - stack.push(new Term.Null()); - } + stack.push(Objects.requireNonNullElseGet(element, Term.Null::new)); } } if (right instanceof MapKey && left instanceof Term.Map) { MapKey key = (MapKey) right; HashMap map = ((Term.Map) left).value(); Term value = map.get(key); - if (value != null) { - stack.push(value); - } else { - stack.push(new Term.Null()); - } + stack.push(Objects.requireNonNullElseGet(value, Term.Null::new)); } break; case Any: @@ -1088,7 +1077,7 @@ Term call(Map variables, TemporarySymbolTable temporarySymbolTable) if (arity() != 0) { throw new Error.Execution("called closure with arity " + arity() + "with no arguments"); } - Deque stack = new ArrayDeque(16); // Default value + Deque stack = new ArrayDeque<>(16); // Default value for (Op op : ops) { op.evaluate(stack, variables, temporarySymbolTable); } @@ -1107,7 +1096,7 @@ Term call(Term arg, Map variables, TemporarySymbolTable temporarySym throw new Error.Execution( Error.Execution.Kind.ShadowedVariable, "closure parameter shadows variable"); } - Deque stack = new ArrayDeque(16); // Default value + Deque stack = new ArrayDeque<>(16); // Default value for (Op op : ops) { op.evaluate(stack, variables, temporarySymbolTable); } @@ -1165,7 +1154,7 @@ public static Result deserializeV1(Schema.OpClosure closu ArrayList ops = new ArrayList<>(); for (long param : closure.getParamsList()) { - params.add(new Long(param)); + params.add(param); } for (Schema.Op op : closure.getOpsList()) { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Authorizer.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Authorizer.java index 024afcd9..8394c996 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Authorizer.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Authorizer.java @@ -187,7 +187,7 @@ public void updateOnToken() throws Error.FailedLogic { TrustedOrigins ruleTrustedOrigins = TrustedOrigins.fromScopes( convertedRule.scopes(), blockTrustedOrigins, i + 1, this.publicKeyToBlockId); - world.addRule((long) i + 1, ruleTrustedOrigins, convertedRule); + world.addRule(i + 1, ruleTrustedOrigins, convertedRule); } } } @@ -415,8 +415,7 @@ public Long authorize(RunLimits limits) throws Error { org.eclipse.biscuit.datalog.Check c = this.checks.get(i).convert(symbolTable); boolean successful = false; - for (int j = 0; j < c.queries().size(); j++) { - org.eclipse.biscuit.datalog.Rule query = c.queries().get(j); + for (org.eclipse.biscuit.datalog.Rule query : c.queries()) { TrustedOrigins ruleTrustedOrigins = TrustedOrigins.fromScopes( query.scopes(), authorizerTrustedOrigins, Long.MAX_VALUE, this.publicKeyToBlockId); @@ -501,8 +500,8 @@ public Long authorize(RunLimits limits) throws Error { for (int i = 0; i < this.policies.size(); i++) { Policy policy = this.policies.get(i); - for (int j = 0; j < policy.queries().size(); j++) { - org.eclipse.biscuit.datalog.Rule query = policy.queries().get(j).convert(symbolTable); + for (Rule policyQuery : policy.queries()) { + org.eclipse.biscuit.datalog.Rule query = policyQuery.convert(symbolTable); TrustedOrigins policyTrustedOrigins = TrustedOrigins.fromScopes( query.scopes(), authorizerTrustedOrigins, Long.MAX_VALUE, this.publicKeyToBlockId); @@ -513,7 +512,7 @@ public Long authorize(RunLimits limits) throws Error { } if (res) { - if (this.policies.get(i).kind() == Policy.Kind.ALLOW) { + if (policy.kind() == Policy.Kind.ALLOW) { policyResult = Optional.of(Result.ok(i)); } else { policyResult = Optional.of(Result.err(i)); @@ -597,7 +596,7 @@ public String formatWorld() { StringBuilder facts = new StringBuilder(); for (Map.Entry> entry : this.world.getFacts().facts().entrySet()) { - facts.append("\n\t\t" + entry.getKey() + ":"); + facts.append("\n\t\t").append(entry.getKey()).append(":"); for (org.eclipse.biscuit.datalog.Fact f : entry.getValue()) { facts.append("\n\t\t\t"); facts.append(this.symbolTable.formatFact(f)); @@ -605,13 +604,13 @@ public String formatWorld() { } final List rules = this.world.getRules().stream() - .map((r) -> this.symbolTable.formatRule(r)) + .map(this.symbolTable::formatRule) .collect(Collectors.toList()); List checks = new ArrayList<>(); for (int j = 0; j < this.checks.size(); j++) { - checks.add("Authorizer[" + j + "]: " + this.checks.get(j).toString()); + checks.add("Authorizer[" + j + "]: " + this.checks.get(j)); } if (this.token != null) { @@ -649,7 +648,7 @@ public String formatWorld() { } return "World {\n\tfacts: [" - + facts.toString() + + facts // String.join(",\n\t\t", facts) + + "\n\t],\n\trules: [\n\t\t" + String.join(",\n\t\t", rules) diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Biscuit.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Biscuit.java index 65f49bf6..34ab15b8 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Biscuit.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Biscuit.java @@ -120,7 +120,6 @@ private static Biscuit make( } else { SerializedBiscuit s = container.getOk(); - Optional c = Optional.of(s); return new Biscuit(authority, blocks, authority.getSymbolTable(), s); } } @@ -339,10 +338,7 @@ public Biscuit attenuate(final SecureRandom rng, final KeyPair keypair, Block bl symbolTable.insert(pk); } - ArrayList blocks = new ArrayList<>(); - for (Block b : copiedBiscuit.blocks) { - blocks.add(b); - } + ArrayList blocks = new ArrayList<>(copiedBiscuit.blocks); blocks.add(block); SerializedBiscuit container = containerRes.getOk(); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Block.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Block.java index 45f9882c..ab037885 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Block.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Block.java @@ -118,7 +118,7 @@ public String print(SymbolTable symbolTable) { s.append(this.context); if (this.externalKey.isPresent()) { s.append("\n\t\texternal key: "); - s.append(this.externalKey.get().toString()); + s.append(this.externalKey.get()); } s.append("\n\t\tscopes: ["); for (Scope scope : this.scopes) { @@ -171,16 +171,16 @@ public String printCode(SymbolTable symbolTable) { s.append(this.externalKey.get().toString()); }*/ for (Scope scope : this.scopes) { - s.append("trusting " + localSymbols.formatScope(scope) + "\n"); + s.append("trusting ").append(localSymbols.formatScope(scope)).append("\n"); } for (Fact f : this.facts) { - s.append(localSymbols.formatFact(f) + ";\n"); + s.append(localSymbols.formatFact(f)).append(";\n"); } for (Rule r : this.rules) { - s.append(localSymbols.formatRule(r) + ";\n"); + s.append(localSymbols.formatRule(r)).append(";\n"); } for (Check c : this.checks) { - s.append(localSymbols.formatCheck(c) + ";\n"); + s.append(localSymbols.formatCheck(c)).append(";\n"); } return s.toString(); @@ -202,16 +202,16 @@ public Schema.Block serialize() { b.setContext(this.context); } - for (int i = 0; i < this.facts.size(); i++) { - b.addFactsV2(this.facts.get(i).serialize()); + for (Fact fact : this.facts) { + b.addFactsV2(fact.serialize()); } - for (int i = 0; i < this.rules.size(); i++) { - b.addRulesV2(this.rules.get(i).serialize()); + for (Rule rule : this.rules) { + b.addRulesV2(rule.serialize()); } - for (int i = 0; i < this.checks.size(); i++) { - b.addChecksV2(this.checks.get(i).serialize()); + for (Check check : this.checks) { + b.addChecksV2(check.serialize()); } for (Scope scope : this.scopes) { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Policy.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Policy.java index 0eaff09c..960aea40 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Policy.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Policy.java @@ -25,7 +25,7 @@ public enum Kind { } private final List queries; - private Kind kind; + private final Kind kind; public Policy(List queries, Kind kind) { this.queries = queries; @@ -42,8 +42,7 @@ public Policy(Rule query, Kind kind) { @Override public String toString() { - final List qs = - queries.stream().map((q) -> q.bodyToString()).collect(Collectors.toList()); + final List qs = queries.stream().map(Rule::bodyToString).collect(Collectors.toList()); switch (this.kind) { case ALLOW: diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/RevocationIdentifier.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/RevocationIdentifier.java index 3324e277..5eb35b78 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/RevocationIdentifier.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/RevocationIdentifier.java @@ -9,7 +9,7 @@ import org.eclipse.biscuit.token.builder.Utils; public final class RevocationIdentifier { - private byte[] bytes; + private final byte[] bytes; public RevocationIdentifier(byte[] bytes) { this.bytes = bytes; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/ThirdPartyBlockContents.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/ThirdPartyBlockContents.java index cc76e3c4..db84c2d7 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/ThirdPartyBlockContents.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/ThirdPartyBlockContents.java @@ -16,9 +16,9 @@ import org.eclipse.biscuit.error.Error; public final class ThirdPartyBlockContents { - private byte[] payload; - private byte[] signature; - private PublicKey publicKey; + private final byte[] payload; + private final byte[] signature; + private final PublicKey publicKey; ThirdPartyBlockContents(byte[] payload, byte[] signature, PublicKey publicKey) { this.payload = payload; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/UnverifiedBiscuit.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/UnverifiedBiscuit.java index 48168bd2..e7e5cbea 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/UnverifiedBiscuit.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/UnverifiedBiscuit.java @@ -171,10 +171,7 @@ private UnverifiedBiscuit attenuate(final SecureRandom rng, final KeyPair keypai symbols.add(s); } - ArrayList blocks = new ArrayList<>(); - for (Block b : copiedBiscuit.blocks) { - blocks.add(b); - } + ArrayList blocks = new ArrayList<>(copiedBiscuit.blocks); blocks.add(block); SerializedBiscuit container = containerRes.getOk(); @@ -318,10 +315,7 @@ public UnverifiedBiscuit appendThirdPartyBlock( SymbolTable symbols = new SymbolTable(copiedBiscuit.symbolTable); - ArrayList blocks = new ArrayList<>(); - for (Block b : copiedBiscuit.blocks) { - blocks.add(b); - } + ArrayList blocks = new ArrayList<>(copiedBiscuit.blocks); blocks.add(block); return new UnverifiedBiscuit(copiedBiscuit.authority, blocks, symbols, container); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Biscuit.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Biscuit.java index 99eb173a..759c9f00 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Biscuit.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Biscuit.java @@ -21,13 +21,13 @@ import org.eclipse.biscuit.token.builder.parser.Parser; public final class Biscuit { - private SecureRandom rng; - private Signer root; + private final SecureRandom rng; + private final Signer root; private String context; - private List facts; - private List rules; - private List checks; - private List scopes; + private final List facts; + private final List rules; + private final List checks; + private final List scopes; private Optional rootKeyId; public Biscuit(final SecureRandom rng, final Signer root) { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Block.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Block.java index c070e0c3..ef666402 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Block.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Block.java @@ -32,10 +32,10 @@ public final class Block { private String context; - private List facts; - private List rules; - private List checks; - private List scopes; + private final List facts; + private final List rules; + private final List checks; + private final List scopes; public Block() { this.context = ""; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Check.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Check.java index a08b7b3d..d4d8c923 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Check.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Check.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import org.eclipse.biscuit.datalog.Check.Kind; import org.eclipse.biscuit.datalog.SymbolTable; @@ -59,8 +60,7 @@ public Kind kind() { @Override public String toString() { - final List qs = - queries.stream().map((q) -> q.bodyToString()).collect(Collectors.toList()); + final List qs = queries.stream().map(Rule::bodyToString).collect(Collectors.toList()); switch (kind) { case ONE: @@ -85,7 +85,7 @@ public boolean equals(Object o) { Check check = (Check) o; - return queries != null ? queries.equals(check.queries) : check.queries == null; + return Objects.equals(queries, check.queries); } @Override diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Expression.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Expression.java index 7ac58d27..c5a59390 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Expression.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Expression.java @@ -25,7 +25,7 @@ public final org.eclipse.biscuit.datalog.expressions.Expression convertExpr( } public static Expression convertFrom(ArrayList ops, SymbolTable symbolTable) { - Deque stack = new ArrayDeque(16); + Deque stack = new ArrayDeque<>(16); for (Op op : ops) { if (op instanceof org.eclipse.biscuit.datalog.Term) { org.eclipse.biscuit.datalog.Term v = (org.eclipse.biscuit.datalog.Term) op; @@ -479,8 +479,8 @@ public String toString() { } public static final class Closure extends Expression { - private ArrayList params; - private Expression body; + private final ArrayList params; + private final Expression body; public Closure(Expression body) { this.params = new ArrayList<>(); @@ -493,10 +493,10 @@ public Closure(ArrayList params, Expression body) { } public void toOpcodes(SymbolTable symbolTable, List ops) { - ArrayList paramIndexes = new ArrayList(); + ArrayList paramIndexes = new ArrayList<>(); for (String param : params) { long index = symbolTable.insert(param); - paramIndexes.add(new Long(index)); + paramIndexes.add(index); } ArrayList bodyOps = new ArrayList<>(); body.toOpcodes(symbolTable, bodyOps); @@ -541,19 +541,19 @@ public boolean equals(Object o) { @Override public String toString() { - if (params.size() == 0) { + if (params.isEmpty()) { return body.toString(); } - String s = null; + StringBuilder s = null; for (String param : params) { if (s == null) { - s = "$" + param; + s = new StringBuilder("$" + param); } else { - s = s + ", $" + param; + s.append(", $").append(param); } } - return s + " -> " + body.toString(); + return s + " -> " + body; } } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Fact.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Fact.java index c7148294..90e97ca8 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Fact.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Fact.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -20,7 +21,7 @@ public final class Fact implements Cloneable { Optional>> variables; public Fact(String name, List terms) { - Map> variables = new HashMap>(); + Map> variables = new HashMap<>(); for (Term term : terms) { if (term instanceof Term.Variable) { variables.put(((Term.Variable) term).value, Optional.empty()); @@ -41,7 +42,7 @@ private Fact(Predicate predicate, Optional>> variable } public void validate() throws Error.Language { - if (!this.variables.isEmpty()) { + if (this.variables.isPresent()) { List invalidVariables = variables.get().entrySet().stream() .flatMap( @@ -75,21 +76,21 @@ public Fact set(String name, Term term) throws Error.Language { public Fact applyVariables() { this.variables.ifPresent( - laVariables -> { - this.predicate.terms = - this.predicate.terms.stream() - .flatMap( - t -> { - if (t instanceof Term.Variable) { - Optional term = - laVariables.getOrDefault(((Term.Variable) t).value, Optional.empty()); - return term.map(t2 -> Stream.of(t2)).orElse(Stream.empty()); - } else { - return Stream.of(t); - } - }) - .collect(Collectors.toList()); - }); + laVariables -> + this.predicate.terms = + this.predicate.terms.stream() + .flatMap( + t -> { + if (t instanceof Term.Variable) { + Optional term = + laVariables.getOrDefault( + ((Term.Variable) t).value, Optional.empty()); + return term.stream(); + } else { + return Stream.of(t); + } + }) + .collect(Collectors.toList())); return this; } @@ -129,7 +130,7 @@ public boolean equals(Object o) { Fact fact = (Fact) o; - return predicate != null ? predicate.equals(fact.predicate) : fact.predicate == null; + return Objects.equals(predicate, fact.predicate); } @Override @@ -140,13 +141,7 @@ public int hashCode() { @Override public Fact clone() { Predicate p = this.predicate.clone(); - Optional>> variables = - this.variables.map( - v -> { - Map> m = new HashMap<>(); - m.putAll(v); - return m; - }); + Optional>> variables = this.variables.map(HashMap::new); return new Fact(p, variables); } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Predicate.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Predicate.java index c6843d46..10e20dbf 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Predicate.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Predicate.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import org.eclipse.biscuit.datalog.SymbolTable; @@ -51,9 +52,8 @@ public static Predicate convertFrom( @Override public String toString() { - final List i = - terms.stream().map((term) -> term.toString()).collect(Collectors.toList()); - return "" + name + "(" + String.join(", ", i) + ")"; + final List i = terms.stream().map(Object::toString).collect(Collectors.toList()); + return name + "(" + String.join(", ", i) + ")"; } @Override @@ -67,10 +67,10 @@ public boolean equals(Object o) { Predicate predicate = (Predicate) o; - if (name != null ? !name.equals(predicate.name) : predicate.name != null) { + if (!Objects.equals(name, predicate.name)) { return false; } - return terms != null ? terms.equals(predicate.terms) : predicate.terms == null; + return Objects.equals(terms, predicate.terms); } @Override @@ -83,7 +83,7 @@ public int hashCode() { @Override public Predicate clone() { String name = this.name; - List terms = new ArrayList(this.terms.size()); + List terms = new ArrayList<>(this.terms.size()); terms.addAll(this.terms); return new Predicate(name, terms); } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Rule.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Rule.java index 73ce4501..19dab756 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Rule.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Rule.java @@ -9,6 +9,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -57,12 +58,9 @@ public Rule( @Override public Rule clone() { - List body = new ArrayList<>(); - body.addAll(this.body); - List expressions = new ArrayList<>(); - expressions.addAll(this.expressions); - List scopes = new ArrayList<>(); - scopes.addAll(this.scopes); + List body = new ArrayList<>(this.body); + List expressions = new ArrayList<>(this.expressions); + List scopes = new ArrayList<>(this.scopes); Predicate head = this.head.clone(); return new Rule(head, body, expressions, scopes); } @@ -90,7 +88,7 @@ public void applyVariables() { if (t instanceof Term.Variable) { Optional term = laVariables.getOrDefault(((Term.Variable) t).value, Optional.empty()); - return term.map(t2 -> Stream.of(t2)).orElse(Stream.of(t)); + return term.map(Stream::of).orElse(Stream.of(t)); } else { return Stream.of(t); } @@ -105,7 +103,7 @@ public void applyVariables() { Optional term = laVariables.getOrDefault( ((Term.Variable) t).value, Optional.empty()); - return term.map(t2 -> Stream.of(t2)).orElse(Stream.of(t)); + return term.map(Stream::of).orElse(Stream.of(t)); } else { return Stream.of(t); } @@ -171,7 +169,7 @@ public Result validateVariables() { return Result.err( "rule head or expressions contains variables that are not " + "used in predicates of the rule's body: " - + freeVariables.toString()); + + freeVariables); } public org.eclipse.biscuit.datalog.Rule convert(SymbolTable symbolTable) { @@ -228,16 +226,16 @@ public boolean equals(Object o) { Rule rule = (Rule) o; - if (head != null ? !head.equals(rule.head) : rule.head != null) { + if (!Objects.equals(head, rule.head)) { return false; } - if (body != null ? !body.equals(rule.body) : rule.body != null) { + if (!Objects.equals(body, rule.body)) { return false; } - if (scopes != null ? !scopes.equals(rule.scopes) : rule.scopes != null) { + if (!Objects.equals(scopes, rule.scopes)) { return false; } - return expressions != null ? expressions.equals(rule.expressions) : rule.expressions == null; + return Objects.equals(expressions, rule.expressions); } @Override @@ -255,8 +253,7 @@ public String bodyToString() { String res = ""; if (!r.body.isEmpty()) { - final List b = - r.body.stream().map((pred) -> pred.toString()).collect(Collectors.toList()); + final List b = r.body.stream().map(Predicate::toString).collect(Collectors.toList()); res += String.join(", ", b); } @@ -265,9 +262,7 @@ public String bodyToString() { res += ", "; } final List e = - r.expressions.stream() - .map((expression) -> expression.toString()) - .collect(Collectors.toList()); + r.expressions.stream().map(Object::toString).collect(Collectors.toList()); res += String.join(", ", e); } @@ -275,8 +270,7 @@ public String bodyToString() { if (!r.body.isEmpty() || !r.expressions.isEmpty()) { res += " "; } - final List e = - r.scopes.stream().map((scope) -> scope.toString()).collect(Collectors.toList()); + final List e = r.scopes.stream().map(Scope::toString).collect(Collectors.toList()); res += "trusting " + String.join(", ", e); } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Scope.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Scope.java index de97578c..1c9726eb 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Scope.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Scope.java @@ -17,9 +17,9 @@ enum Kind { Parameter, } - private Kind kind; - private PublicKey publicKey; - private String parameter; + private final Kind kind; + private final PublicKey publicKey; + private final String parameter; private Scope(Kind kind) { this.kind = kind; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Term.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Term.java index c5778e6b..ea8a23f7 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Term.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Term.java @@ -221,11 +221,7 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { - return false; - } - - return true; + return o != null && getClass() == o.getClass(); } @Override @@ -440,16 +436,16 @@ public java.util.Set getValue() { @Override public String toString() { - if (value.size() == 0) { + if (value.isEmpty()) { return "{,}"; } - String s = "{"; + StringBuilder s = new StringBuilder("{"); int count = 0; for (Term elem : value) { - s += elem; + s.append(elem); count += 1; if (count < value.size()) { - s += ", "; + s.append(", "); } } return s + "}"; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Error.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Error.java index 8bd3f98d..0e58d787 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Error.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Error.java @@ -5,6 +5,8 @@ package org.eclipse.biscuit.token.builder.parser; +import java.util.Objects; + public final class Error extends Exception { String input; String message; @@ -31,10 +33,10 @@ public boolean equals(Object o) { Error error = (Error) o; - if (input != null ? !input.equals(error.input) : error.input != null) { + if (!Objects.equals(input, error.input)) { return false; } - return message != null ? message.equals(error.message) : error.message == null; + return Objects.equals(message, error.message); } @Override diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Parser.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Parser.java index f1b8992f..e835f872 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Parser.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Parser.java @@ -219,7 +219,6 @@ public static Result, Error> rule(String s) { return Result.err(new Error(s, "rule arrow not found")); } - List predicates = new ArrayList(); s = s.substring(2); var bodyRes = ruleBody(s); @@ -288,7 +287,6 @@ public static Result, Error> policy(String s) { return Result.err(new Error(s, "missing policy prefix")); } - List queries = new ArrayList<>(); var bodyRes = checkBody(s); if (bodyRes.isErr()) { return Result.err(bodyRes.getErr()); @@ -326,7 +324,6 @@ public static Result>, Error> checkBody(String s) { } queries.add(valid.getOk()); - int i = 0; while (true) { if (s.isEmpty()) { break; @@ -359,7 +356,7 @@ public static Result>, Error> checkBody(String s) { } public static Result ruleBody(String s) { - List predicates = new ArrayList(); + List predicates = new ArrayList<>(); List expressions = new ArrayList<>(); while (true) { @@ -406,17 +403,17 @@ public static Result, Error> predicate(String s) { String name = tn._1; s = tn._2; - if (name.length() == 0) { + if (name.isEmpty()) { return Result.err(new Error(s, "no predicate name")); } s = space(s); - if (s.length() == 0 || s.charAt(0) != '(') { + if (s.isEmpty() || s.charAt(0) != '(') { return Result.err(new Error(s, "opening parens not found for predicate " + name)); } s = s.substring(1); - List terms = new ArrayList(); + List terms = new ArrayList<>(); while (true) { s = space(s); @@ -445,7 +442,7 @@ public static Result, Error> predicate(String s) { } String remaining = s.substring(1); - return Result.ok(new Pair(remaining, new Predicate(name, terms))); + return Result.ok(new Pair<>(remaining, new Predicate(name, terms))); } public static Result>, Error> scopes(String s) { @@ -455,7 +452,7 @@ public static Result>, Error> scopes(String s) { s = s.substring("trusting".length()); s = space(s); - List scopes = new ArrayList(); + List scopes = new ArrayList<>(); while (true) { s = space(s); @@ -499,7 +496,7 @@ public static Result, Error> scope(String s) { return Result.err(new Error(s, "unrecognized parameter")); } Pair t = res.getOk(); - if (!s.isEmpty() && s.charAt(0) == '}') { + if (s.charAt(0) == '}') { return Result.ok(new Pair<>(t._1, Scope.parameter(t._2))); } else { return Result.err(new Error(s, "unrecognized parameter end")); @@ -544,12 +541,12 @@ public static Result, Error> factPredicate(String s) { s = tn._2; s = space(s); - if (s.length() == 0 || s.charAt(0) != '(') { + if (s.isEmpty() || s.charAt(0) != '(') { return Result.err(new Error(s, "opening parens not found for fact " + name)); } s = s.substring(1); - List terms = new ArrayList(); + List terms = new ArrayList<>(); while (true) { s = space(s); @@ -578,7 +575,7 @@ public static Result, Error> factPredicate(String s) { } String remaining = s.substring(1); - return Result.ok(new Pair(remaining, new Predicate(name, terms))); + return Result.ok(new Pair<>(remaining, new Predicate(name, terms))); } public static Result, Error> name(String s) { @@ -586,7 +583,7 @@ public static Result, Error> name(String s) { String name = t._1; String remaining = t._2; - return Result.ok(new Pair(remaining, name)); + return Result.ok(new Pair<>(remaining, name)); } public static Result, Error> term(String s) { @@ -762,7 +759,7 @@ public static Result, Error> string(String s) { String string = s.substring(1, index + 1); String remaining = s.substring(index + 2); - return Result.ok(new Pair(remaining, (Term.Str) Utils.string(string))); + return Result.ok(new Pair<>(remaining, (Term.Str) Utils.string(string))); } public static Result, Error> integer(String s) { @@ -788,7 +785,7 @@ public static Result, Error> integer(String s) { long i = Long.parseLong(s.substring(0, index2)); String remaining = s.substring(index2); - return Result.ok(new Pair(remaining, (Term.Integer) Utils.integer(i))); + return Result.ok(new Pair<>(remaining, (Term.Integer) Utils.integer(i))); } public static Result, Error> date(String s) { @@ -798,7 +795,7 @@ public static Result, Error> date(String s) { try { OffsetDateTime d = OffsetDateTime.parse(t._1); String remaining = t._2; - return Result.ok(new Pair(remaining, new Term.Date(d.toEpochSecond()))); + return Result.ok(new Pair<>(remaining, new Term.Date(d.toEpochSecond()))); } catch (DateTimeParseException e) { return Result.err(new Error(s, "not a date")); } @@ -813,7 +810,7 @@ public static Result, Error> variable(String s) { takewhile( s.substring(1), (c) -> Character.isAlphabetic(c) || Character.isDigit(c) || c == '_'); - return Result.ok(new Pair(t._2, (Term.Variable) Utils.var(t._1))); + return Result.ok(new Pair<>(t._2, (Term.Variable) Utils.var(t._1))); } public static Result, Error> nullTerm(String s) { @@ -846,7 +843,7 @@ public static Result, Error> array(String s) { } s = s.substring(1); - ArrayList terms = new ArrayList(); + ArrayList terms = new ArrayList<>(); while (true) { s = space(s); @@ -890,7 +887,7 @@ public static Result, Error> map(String s) { } s = s.substring(1); - HashMap v = new HashMap(); + HashMap v = new HashMap<>(); while (true) { s = space(s); @@ -953,13 +950,13 @@ public static Result, Error> set(String s) { s = space(s.substring(1)); if (s.charAt(0) == '}') { s = s.substring(1); - return Result.ok(new Pair<>(s, new Term.Set(new HashSet()))); + return Result.ok(new Pair<>(s, new Term.Set(new HashSet<>()))); } else { return Result.err(new Error(s, "closing brace not found")); } } - HashSet terms = new HashSet(); + HashSet terms = new HashSet<>(); while (true) { s = space(s); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java index 80eddd4f..62a2f8a0 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java @@ -242,9 +242,7 @@ public byte[] serialize() throws Error.FormatError.SerializationError { } biscuitBuilder.setProof(proofBuilder.build()); - if (!this.rootKeyId.isEmpty()) { - biscuitBuilder.setRootKeyId(this.rootKeyId.get()); - } + this.rootKeyId.ifPresent(biscuitBuilder::setRootKeyId); Schema.Biscuit biscuit = biscuitBuilder.build(); @@ -342,10 +340,7 @@ public Result append( SignedBlock signedBlock = new SignedBlock(block, nextKey, signature, externalSignature, blockSignatureVersion); - ArrayList blocks = new ArrayList<>(); - for (SignedBlock bl : this.blocks) { - blocks.add(bl); - } + ArrayList blocks = new ArrayList<>(this.blocks); blocks.add(signedBlock); Proof proof = new Proof.NextSecret(next); @@ -420,11 +415,9 @@ static Result verifyAuthorityBlockS } var verificationResult = publicKey.verify(payload.getOk(), signedBlock.getSignature()); - if (verificationResult.isPresent()) { - return Result.err(verificationResult.get()); - } - - return Result.ok(signedBlock.getKey()); + return verificationResult + .>map(Result::err) + .orElseGet(() -> Result.ok(signedBlock.getKey())); } static Result verifyBlockSignature( @@ -465,7 +458,6 @@ static Result verifyBlockSignature( } public Pair> extractBlocks(SymbolTable symbolTable) throws Error { - ArrayList> blockExternalKeys = new ArrayList<>(); var authRes = Block.fromBytes(this.authority.getBlock(), Optional.empty()); if (authRes.isErr()) { throw authRes.getErr(); @@ -474,7 +466,6 @@ public Pair> extractBlocks(SymbolTable symbolTable) throws Er for (org.eclipse.biscuit.crypto.PublicKey pk : authority.getPublicKeys()) { symbolTable.insert(pk); } - blockExternalKeys.add(Optional.empty()); for (String s : authority.getSymbolTable().symbols()) { symbolTable.add(s); @@ -495,9 +486,7 @@ public Pair> extractBlocks(SymbolTable symbolTable) throws Er // blocks with external signatures keep their own symbol table if (bdata.getExternalSignature().isPresent()) { // symbolTable.insert(bdata.externalSignature.get().key); - blockExternalKeys.add(Optional.of(bdata.getExternalSignature().get().getKey())); } else { - blockExternalKeys.add(Optional.empty()); for (String s : block.getSymbolTable().symbols()) { symbolTable.add(s); } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SignedBlock.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SignedBlock.java index 21610dde..0629f0c4 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SignedBlock.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SignedBlock.java @@ -9,11 +9,11 @@ import org.eclipse.biscuit.crypto.PublicKey; public class SignedBlock { - private byte[] block; - private PublicKey key; - private byte[] signature; - private Optional externalSignature; - private int version; + private final byte[] block; + private final PublicKey key; + private final byte[] signature; + private final Optional externalSignature; + private final int version; public SignedBlock( byte[] block, diff --git a/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java b/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java index 93b607f5..5fd67380 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java @@ -27,7 +27,7 @@ public void testNegate() throws Error.Execution { Expression e = new Expression( - new ArrayList( + new ArrayList<>( Arrays.asList( new Term.Integer(1), new Term.Variable(SymbolTable.DEFAULT_SYMBOLS_OFFSET + 2), @@ -51,7 +51,7 @@ public void testAddsStr() throws Error.Execution { Expression e = new Expression( - new ArrayList( + new ArrayList<>( Arrays.asList( new Term.Str(SymbolTable.DEFAULT_SYMBOLS_OFFSET), new Term.Str(SymbolTable.DEFAULT_SYMBOLS_OFFSET + 1), @@ -72,7 +72,7 @@ public void testContainsStr() throws Error.Execution { Expression e = new Expression( - new ArrayList( + new ArrayList<>( Arrays.asList( new Term.Str(SymbolTable.DEFAULT_SYMBOLS_OFFSET), new Term.Str(SymbolTable.DEFAULT_SYMBOLS_OFFSET + 1), @@ -92,7 +92,7 @@ public void testNegativeContainsStr() throws Error.Execution { Expression e = new Expression( - new ArrayList( + new ArrayList<>( Arrays.asList( new Term.Str(SymbolTable.DEFAULT_SYMBOLS_OFFSET), new Term.Str(SymbolTable.DEFAULT_SYMBOLS_OFFSET + 1), @@ -111,7 +111,7 @@ public void testIntersectionAndContains() throws Error.Execution { Expression e = new Expression( - new ArrayList( + new ArrayList<>( Arrays.asList( new Term.Set( new HashSet<>( diff --git a/biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java b/biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java index 844a0bef..a944cf0d 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java @@ -10,9 +10,9 @@ import java.time.Instant; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.stream.Collectors; import org.eclipse.biscuit.datalog.expressions.Expression; import org.eclipse.biscuit.datalog.expressions.Op; @@ -34,26 +34,26 @@ public void testFamily() throws Error { final long grandparent = syms.insert("grandparent"); final long sibling = syms.insert("siblings"); - w.addFact(new Origin(0), new Fact(new Predicate(parent, Arrays.asList(a, b)))); - w.addFact(new Origin(0), new Fact(new Predicate(parent, Arrays.asList(b, c)))); - w.addFact(new Origin(0), new Fact(new Predicate(parent, Arrays.asList(c, d)))); + w.addFact(new Origin(0), new Fact(new Predicate(parent, List.of(a, b)))); + w.addFact(new Origin(0), new Fact(new Predicate(parent, List.of(b, c)))); + w.addFact(new Origin(0), new Fact(new Predicate(parent, List.of(c, d)))); final Rule r1 = new Rule( new Predicate( grandparent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("grandparent")), new Term.Variable(syms.insert("grandchild")))), - Arrays.asList( + List.of( new Predicate( parent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("grandparent")), new Term.Variable(syms.insert("parent")))), new Predicate( parent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("parent")), new Term.Variable(syms.insert("grandchild"))))), new ArrayList<>()); @@ -62,35 +62,29 @@ public void testFamily() throws Error { FactSet queryRuleResult = w.queryRule(r1, (long) 0, new TrustedOrigins(0), syms); System.out.println( "grandparents query_rules: [" - + String.join( - ", ", - queryRuleResult.stream() - .map((f) -> syms.formatFact(f)) - .collect(Collectors.toList())) + + queryRuleResult.stream().map(syms::formatFact).collect(Collectors.joining(", ")) + "]"); System.out.println( "current facts: [" - + String.join( - ", ", - w.getFacts().stream().map((f) -> syms.formatFact(f)).collect(Collectors.toList())) + + w.getFacts().stream().map(syms::formatFact).collect(Collectors.joining(", ")) + "]"); final Rule r2 = new Rule( new Predicate( grandparent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("grandparent")), new Term.Variable(syms.insert("grandchild")))), - Arrays.asList( + List.of( new Predicate( parent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("grandparent")), new Term.Variable(syms.insert("parent")))), new Predicate( parent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("parent")), new Term.Variable(syms.insert("grandchild"))))), new ArrayList<>()); @@ -104,13 +98,13 @@ public void testFamily() throws Error { new Rule( new Predicate( parent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("parent")), new Term.Variable(syms.insert("child")))), - Arrays.asList( + List.of( new Predicate( parent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("parent")), new Term.Variable(syms.insert("child"))))), new ArrayList<>()); @@ -123,29 +117,28 @@ public void testFamily() throws Error { } final Rule query2 = new Rule( - new Predicate(parent, Arrays.asList(new Term.Variable(syms.insert("parent")), b)), - Arrays.asList( - new Predicate(parent, Arrays.asList(new Term.Variable(syms.insert("parent")), b))), + new Predicate(parent, List.of(new Term.Variable(syms.insert("parent")), b)), + List.of(new Predicate(parent, List.of(new Term.Variable(syms.insert("parent")), b))), new ArrayList<>()); System.out.println( "parents of B: [" + String.join( ", ", w.queryRule(query2, (long) 0, new TrustedOrigins(0), syms).stream() - .map((f) -> syms.formatFact(f)) + .map(syms::formatFact) .collect(Collectors.toSet())) + "]"); final Rule query3 = new Rule( new Predicate( grandparent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("grandparent")), new Term.Variable(syms.insert("grandchild")))), - Arrays.asList( + List.of( new Predicate( grandparent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("grandparent")), new Term.Variable(syms.insert("grandchild"))))), new ArrayList<>()); @@ -154,42 +147,41 @@ public void testFamily() throws Error { + String.join( ", ", w.queryRule(query3, (long) 0, new TrustedOrigins(0), syms).stream() - .map((f) -> syms.formatFact(f)) + .map(syms::formatFact) .collect(Collectors.toSet())) + "]"); - w.addFact(new Origin(0), new Fact(new Predicate(parent, Arrays.asList(c, e)))); + w.addFact(new Origin(0), new Fact(new Predicate(parent, List.of(c, e)))); w.run(syms); final Rule query4 = new Rule( new Predicate( grandparent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("grandparent")), new Term.Variable(syms.insert("grandchild")))), - Arrays.asList( + List.of( new Predicate( grandparent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("grandparent")), new Term.Variable(syms.insert("grandchild"))))), new ArrayList<>()); final FactSet res = w.queryRule(query4, (long) 0, new TrustedOrigins(0), syms); System.out.println( "grandparents after inserting parent(C, E): [" - + String.join( - ", ", res.stream().map((f) -> syms.formatFact(f)).collect(Collectors.toSet())) + + String.join(", ", res.stream().map(syms::formatFact).collect(Collectors.toSet())) + "]"); final FactSet expected = new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( - new Fact(new Predicate(grandparent, Arrays.asList(a, c))), - new Fact(new Predicate(grandparent, Arrays.asList(b, d))), - new Fact(new Predicate(grandparent, Arrays.asList(b, e)))))); + List.of( + new Fact(new Predicate(grandparent, List.of(a, c))), + new Fact(new Predicate(grandparent, List.of(b, d))), + new Fact(new Predicate(grandparent, List.of(b, e)))))); assertEquals(expected, res); w.addRule( @@ -198,18 +190,18 @@ public void testFamily() throws Error { new Rule( new Predicate( sibling, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sibling1")), new Term.Variable(syms.insert("sibling2")))), - Arrays.asList( + List.of( new Predicate( parent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("parent")), new Term.Variable(syms.insert("sibling1")))), new Predicate( parent, - Arrays.asList( + List.of( new Term.Variable(syms.insert("parent")), new Term.Variable(syms.insert("sibling2"))))), new ArrayList<>())); @@ -219,13 +211,13 @@ public void testFamily() throws Error { new Rule( new Predicate( sibling, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sibling1")), new Term.Variable(syms.insert("sibling2")))), - Arrays.asList( + List.of( new Predicate( sibling, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sibling1")), new Term.Variable(syms.insert("sibling2"))))), new ArrayList<>()); @@ -234,7 +226,7 @@ public void testFamily() throws Error { + String.join( ", ", w.queryRule(query5, (long) 0, new TrustedOrigins(0), syms).stream() - .map((f) -> syms.formatFact(f)) + .map(syms::formatFact) .collect(Collectors.toSet())) + "]"); } @@ -256,39 +248,39 @@ public void testNumbers() throws Error { final long t2 = syms.insert("t2"); final long join = syms.insert("join"); - w.addFact(new Origin(0), new Fact(new Predicate(t1, Arrays.asList(new Term.Integer(0), abc)))); - w.addFact(new Origin(0), new Fact(new Predicate(t1, Arrays.asList(new Term.Integer(1), def)))); - w.addFact(new Origin(0), new Fact(new Predicate(t1, Arrays.asList(new Term.Integer(2), ghi)))); - w.addFact(new Origin(0), new Fact(new Predicate(t1, Arrays.asList(new Term.Integer(3), jkl)))); - w.addFact(new Origin(0), new Fact(new Predicate(t1, Arrays.asList(new Term.Integer(4), mno)))); + w.addFact(new Origin(0), new Fact(new Predicate(t1, List.of(new Term.Integer(0), abc)))); + w.addFact(new Origin(0), new Fact(new Predicate(t1, List.of(new Term.Integer(1), def)))); + w.addFact(new Origin(0), new Fact(new Predicate(t1, List.of(new Term.Integer(2), ghi)))); + w.addFact(new Origin(0), new Fact(new Predicate(t1, List.of(new Term.Integer(3), jkl)))); + w.addFact(new Origin(0), new Fact(new Predicate(t1, List.of(new Term.Integer(4), mno)))); w.addFact( new Origin(0), - new Fact(new Predicate(t2, Arrays.asList(new Term.Integer(0), aaa, new Term.Integer(0))))); + new Fact(new Predicate(t2, List.of(new Term.Integer(0), aaa, new Term.Integer(0))))); w.addFact( new Origin(0), - new Fact(new Predicate(t2, Arrays.asList(new Term.Integer(1), bbb, new Term.Integer(0))))); + new Fact(new Predicate(t2, List.of(new Term.Integer(1), bbb, new Term.Integer(0))))); w.addFact( new Origin(0), - new Fact(new Predicate(t2, Arrays.asList(new Term.Integer(2), ccc, new Term.Integer(1))))); + new Fact(new Predicate(t2, List.of(new Term.Integer(2), ccc, new Term.Integer(1))))); FactSet res = w.queryRule( new Rule( new Predicate( join, - Arrays.asList( + List.of( new Term.Variable(syms.insert("left")), new Term.Variable(syms.insert("right")))), - Arrays.asList( + List.of( new Predicate( t1, - Arrays.asList( + List.of( new Term.Variable(syms.insert("id")), new Term.Variable(syms.insert("left")))), new Predicate( t2, - Arrays.asList( + List.of( new Term.Variable(syms.insert("t2_id")), new Term.Variable(syms.insert("right")), new Term.Variable(syms.insert("id"))))), @@ -304,10 +296,10 @@ public void testNumbers() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( - new Fact(new Predicate(join, Arrays.asList(abc, aaa))), - new Fact(new Predicate(join, Arrays.asList(abc, bbb))), - new Fact(new Predicate(join, Arrays.asList(def, ccc)))))); + List.of( + new Fact(new Predicate(join, List.of(abc, aaa))), + new Fact(new Predicate(join, List.of(abc, bbb))), + new Fact(new Predicate(join, List.of(def, ccc)))))); assertEquals(expected, res); res = @@ -315,25 +307,25 @@ public void testNumbers() throws Error { new Rule( new Predicate( join, - Arrays.asList( + List.of( new Term.Variable(syms.insert("left")), new Term.Variable(syms.insert("right")))), - Arrays.asList( + List.of( new Predicate( t1, - Arrays.asList( + List.of( new Term.Variable(syms.insert("id")), new Term.Variable(syms.insert("left")))), new Predicate( t2, - Arrays.asList( + List.of( new Term.Variable(syms.insert("t2_id")), new Term.Variable(syms.insert("right")), new Term.Variable(syms.insert("id"))))), - Arrays.asList( + List.of( new Expression( - new ArrayList( - Arrays.asList( + new ArrayList<>( + List.of( new Term.Variable(syms.insert("id")), new Term.Integer(1), new Op.Binary(Op.BinaryOp.LessThan)))))), @@ -348,33 +340,33 @@ public void testNumbers() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( - new Fact(new Predicate(join, Arrays.asList(abc, aaa))), - new Fact(new Predicate(join, Arrays.asList(abc, bbb)))))); + List.of( + new Fact(new Predicate(join, List.of(abc, aaa))), + new Fact(new Predicate(join, List.of(abc, bbb)))))); assertEquals(expected, res); } - private final FactSet testSuffix( + private FactSet testSuffix( final World w, SymbolTable syms, final long suff, final long route, final String suffix) throws Error { return w.queryRule( new Rule( new Predicate( suff, - Arrays.asList( + List.of( new Term.Variable(syms.insert("app_id")), new Term.Variable(syms.insert("domain")))), - Arrays.asList( + List.of( new Predicate( route, - Arrays.asList( + List.of( new Term.Variable(syms.insert("route_id")), new Term.Variable(syms.insert("app_id")), new Term.Variable(syms.insert("domain"))))), - Arrays.asList( + List.of( new Expression( - new ArrayList( - Arrays.asList( + new ArrayList<>( + List.of( new Term.Variable(syms.insert("domain")), syms.add(suffix), new Op.Binary(Op.BinaryOp.Suffix)))))), @@ -397,26 +389,22 @@ public void testStr() throws Error { w.addFact( new Origin(0), new Fact( - new Predicate( - route, Arrays.asList(new Term.Integer(0), app_0, syms.add("example.com"))))); + new Predicate(route, List.of(new Term.Integer(0), app_0, syms.add("example.com"))))); w.addFact( new Origin(0), - new Fact( - new Predicate(route, Arrays.asList(new Term.Integer(1), app_1, syms.add("test.com"))))); + new Fact(new Predicate(route, List.of(new Term.Integer(1), app_1, syms.add("test.com"))))); w.addFact( new Origin(0), - new Fact( - new Predicate(route, Arrays.asList(new Term.Integer(2), app_2, syms.add("test.fr"))))); + new Fact(new Predicate(route, List.of(new Term.Integer(2), app_2, syms.add("test.fr"))))); w.addFact( new Origin(0), new Fact( new Predicate( - route, Arrays.asList(new Term.Integer(3), app_0, syms.add("www.example.com"))))); + route, List.of(new Term.Integer(3), app_0, syms.add("www.example.com"))))); w.addFact( new Origin(0), new Fact( - new Predicate( - route, Arrays.asList(new Term.Integer(4), app_1, syms.add("mx.example.com"))))); + new Predicate(route, List.of(new Term.Integer(4), app_1, syms.add("mx.example.com"))))); FactSet res = testSuffix(w, syms, suff, route, ".fr"); for (Iterator it = res.stream().iterator(); it.hasNext(); ) { @@ -427,8 +415,7 @@ public void testStr() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( - new Fact(new Predicate(suff, Arrays.asList(app_2, syms.add("test.fr"))))))); + List.of(new Fact(new Predicate(suff, List.of(app_2, syms.add("test.fr"))))))); assertEquals(expected, res); res = testSuffix(w, syms, suff, route, "example.com"); @@ -440,12 +427,10 @@ public void testStr() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( - new Fact(new Predicate(suff, Arrays.asList(app_0, syms.add("example.com")))), - new Fact( - new Predicate(suff, Arrays.asList(app_0, syms.add("www.example.com")))), - new Fact( - new Predicate(suff, Arrays.asList(app_1, syms.add("mx.example.com"))))))); + List.of( + new Fact(new Predicate(suff, List.of(app_0, syms.add("example.com")))), + new Fact(new Predicate(suff, List.of(app_0, syms.add("www.example.com")))), + new Fact(new Predicate(suff, List.of(app_1, syms.add("mx.example.com"))))))); assertEquals(expected, res); } @@ -471,33 +456,33 @@ public void testDate() throws Error { w.addFact( new Origin(0), - new Fact(new Predicate(x, Arrays.asList(new Term.Date(t1.getEpochSecond()), abc)))); + new Fact(new Predicate(x, List.of(new Term.Date(t1.getEpochSecond()), abc)))); w.addFact( new Origin(0), - new Fact(new Predicate(x, Arrays.asList(new Term.Date(t3.getEpochSecond()), def)))); + new Fact(new Predicate(x, List.of(new Term.Date(t3.getEpochSecond()), def)))); final Rule r1 = new Rule( new Predicate( before, - Arrays.asList( + List.of( new Term.Variable(syms.insert("date")), new Term.Variable(syms.insert("val")))), - Arrays.asList( + List.of( new Predicate( x, - Arrays.asList( + List.of( new Term.Variable(syms.insert("date")), new Term.Variable(syms.insert("val"))))), - Arrays.asList( + List.of( new Expression( - new ArrayList( - Arrays.asList( + new ArrayList<>( + List.of( new Term.Variable(syms.insert("date")), new Term.Date(t2_timestamp), new Op.Binary(Op.BinaryOp.LessOrEqual)))), new Expression( - new ArrayList( - Arrays.asList( + new ArrayList<>( + List.of( new Term.Variable(syms.insert("date")), new Term.Date(0), new Op.Binary(Op.BinaryOp.GreaterOrEqual)))))); @@ -512,34 +497,33 @@ public void testDate() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( + List.of( new Fact( - new Predicate( - before, Arrays.asList(new Term.Date(t1.getEpochSecond()), abc)))))); + new Predicate(before, List.of(new Term.Date(t1.getEpochSecond()), abc)))))); assertEquals(expected, res); final Rule r2 = new Rule( new Predicate( after, - Arrays.asList( + List.of( new Term.Variable(syms.insert("date")), new Term.Variable(syms.insert("val")))), - Arrays.asList( + List.of( new Predicate( x, - Arrays.asList( + List.of( new Term.Variable(syms.insert("date")), new Term.Variable(syms.insert("val"))))), - Arrays.asList( + List.of( new Expression( - new ArrayList( - Arrays.asList( + new ArrayList<>( + List.of( new Term.Variable(syms.insert("date")), new Term.Date(t2_timestamp), new Op.Binary(Op.BinaryOp.GreaterOrEqual)))), new Expression( - new ArrayList( - Arrays.asList( + new ArrayList<>( + List.of( new Term.Variable(syms.insert("date")), new Term.Date(0), new Op.Binary(Op.BinaryOp.GreaterOrEqual)))))); @@ -554,10 +538,9 @@ public void testDate() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( + List.of( new Fact( - new Predicate( - after, Arrays.asList(new Term.Date(t3.getEpochSecond()), def)))))); + new Predicate(after, List.of(new Term.Date(t3.getEpochSecond()), def)))))); assertEquals(expected, res); } @@ -575,31 +558,30 @@ public void testSet() throws Error { w.addFact( new Origin(0), - new Fact(new Predicate(x, Arrays.asList(abc, new Term.Integer(0), syms.add("test"))))); + new Fact(new Predicate(x, List.of(abc, new Term.Integer(0), syms.add("test"))))); w.addFact( new Origin(0), - new Fact(new Predicate(x, Arrays.asList(def, new Term.Integer(2), syms.add("hello"))))); + new Fact(new Predicate(x, List.of(def, new Term.Integer(2), syms.add("hello"))))); final Rule r1 = new Rule( new Predicate( int_set, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sym")), new Term.Variable(syms.insert("str")))), - Arrays.asList( + List.of( new Predicate( x, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sym")), new Term.Variable(syms.insert("int")), new Term.Variable(syms.insert("str"))))), - Arrays.asList( + List.of( new Expression( - new ArrayList( - Arrays.asList( + new ArrayList<>( + List.of( new Term.Set( - new HashSet<>( - Arrays.asList(new Term.Integer(0L), new Term.Integer(1L)))), + new HashSet<>(List.of(new Term.Integer(0L), new Term.Integer(1L)))), new Term.Variable(syms.insert("int")), new Op.Binary(Op.BinaryOp.Contains)))))); System.out.println("testing r1: " + syms.formatRule(r1)); @@ -612,8 +594,7 @@ public void testSet() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( - new Fact(new Predicate(int_set, Arrays.asList(abc, syms.add("test"))))))); + List.of(new Fact(new Predicate(int_set, List.of(abc, syms.add("test"))))))); assertEquals(expected, res); final long abcSymId = syms.insert("abc"); @@ -623,24 +604,24 @@ public void testSet() throws Error { new Rule( new Predicate( symbol_set, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sym")), new Term.Variable(syms.insert("int")), new Term.Variable(syms.insert("str")))), - Arrays.asList( + List.of( new Predicate( x, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sym")), new Term.Variable(syms.insert("int")), new Term.Variable(syms.insert("str"))))), - Arrays.asList( + List.of( new Expression( - new ArrayList( - Arrays.asList( + new ArrayList<>( + List.of( new Term.Set( new HashSet<>( - Arrays.asList(new Term.Str(abcSymId), new Term.Str(ghiSymId)))), + List.of(new Term.Str(abcSymId), new Term.Str(ghiSymId)))), new Term.Variable(syms.insert("sym")), new Op.Binary(Op.BinaryOp.Contains), new Op.Unary(Op.UnaryOp.Negate)))))); @@ -655,34 +636,32 @@ public void testSet() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( + List.of( new Fact( new Predicate( - symbol_set, - Arrays.asList(def, new Term.Integer(2), syms.add("hello"))))))); + symbol_set, List.of(def, new Term.Integer(2), syms.add("hello"))))))); assertEquals(expected, res); final Rule r3 = new Rule( new Predicate( string_set, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sym")), new Term.Variable(syms.insert("int")), new Term.Variable(syms.insert("str")))), - Arrays.asList( + List.of( new Predicate( x, - Arrays.asList( + List.of( new Term.Variable(syms.insert("sym")), new Term.Variable(syms.insert("int")), new Term.Variable(syms.insert("str"))))), - Arrays.asList( + List.of( new Expression( - new ArrayList( - Arrays.asList( - new Term.Set( - new HashSet<>(Arrays.asList(syms.add("test"), syms.add("aaa")))), + new ArrayList<>( + List.of( + new Term.Set(new HashSet<>(List.of(syms.add("test"), syms.add("aaa")))), new Term.Variable(syms.insert("str")), new Op.Binary(Op.BinaryOp.Contains)))))); System.out.println("testing r3: " + syms.formatRule(r3)); @@ -695,11 +674,10 @@ public void testSet() throws Error { new FactSet( new Origin(0), new HashSet<>( - Arrays.asList( + List.of( new Fact( new Predicate( - string_set, - Arrays.asList(abc, new Term.Integer(0), syms.add("test"))))))); + string_set, List.of(abc, new Term.Integer(0), syms.add("test"))))))); assertEquals(expected, res); } @@ -718,16 +696,16 @@ public void testResource() throws Error { final Term read = syms.add("read"); final Term write = syms.add("write"); - w.addFact(new Origin(0), new Fact(new Predicate(right, Arrays.asList(file1, read)))); - w.addFact(new Origin(0), new Fact(new Predicate(right, Arrays.asList(file2, read)))); - w.addFact(new Origin(0), new Fact(new Predicate(right, Arrays.asList(file1, write)))); + w.addFact(new Origin(0), new Fact(new Predicate(right, List.of(file1, read)))); + w.addFact(new Origin(0), new Fact(new Predicate(right, List.of(file2, read)))); + w.addFact(new Origin(0), new Fact(new Predicate(right, List.of(file1, write)))); final long caveat1 = syms.insert("caveat1"); // r1: caveat2(#file1) <- resource(#ambient, #file1) final Rule r1 = new Rule( - new Predicate(caveat1, Arrays.asList(file1)), - Arrays.asList(new Predicate(resource, Arrays.asList(file1))), + new Predicate(caveat1, List.of(file1)), + List.of(new Predicate(resource, List.of(file1))), new ArrayList<>()); System.out.println("testing caveat 1(should return nothing): " + syms.formatRule(r1)); @@ -746,11 +724,11 @@ public void testResource() throws Error { // 0?, #read) final Rule r2 = new Rule( - new Predicate(caveat2, Arrays.asList(var0)), - Arrays.asList( - new Predicate(resource, Arrays.asList(var0)), - new Predicate(operation, Arrays.asList(read)), - new Predicate(right, Arrays.asList(var0, read))), + new Predicate(caveat2, List.of(var0)), + List.of( + new Predicate(resource, List.of(var0)), + new Predicate(operation, List.of(read)), + new Predicate(right, List.of(var0, read))), new ArrayList<>()); System.out.println("testing caveat 2: " + syms.formatRule(r2));