From ca164fc0a93cdfba8e952d3f273d42406faaa762 Mon Sep 17 00:00:00 2001 From: John Kastner Date: Mon, 19 Feb 2024 11:19:02 -0500 Subject: [PATCH] Re-remove dependency on Guava * Use `copyOf` and `unmoidifiableSet` functions to replace `ImmutableSet` * Roll our own memoizing supplier --- CedarJava/build.gradle | 1 - .../com/cedarpolicy/model/AuthorizationResponse.java | 10 ++++------ .../model/PartialAuthorizationRequest.java | 3 +-- .../model/PartialAuthorizationResponse.java | 9 +++++---- .../model/slice/TemplateInstantiation.java | 3 +-- .../java/com/cedarpolicy/value/EntityTypeName.java | 12 +++++++++--- .../main/java/com/cedarpolicy/value/EntityUID.java | 11 +++++++++-- 7 files changed, 29 insertions(+), 20 deletions(-) diff --git a/CedarJava/build.gradle b/CedarJava/build.gradle index b4e3afcd..65de46e7 100644 --- a/CedarJava/build.gradle +++ b/CedarJava/build.gradle @@ -70,7 +70,6 @@ dependencies { implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.1' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.16.1' implementation 'org.slf4j:slf4j-api:2.0.12' - implementation 'com.google.guava:guava:33.0.0-jre' compileOnly 'com.github.spotbugs:spotbugs-annotations:4.8.3' testImplementation 'org.slf4j:slf4j-simple:2.0.12' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2' diff --git a/CedarJava/src/main/java/com/cedarpolicy/model/AuthorizationResponse.java b/CedarJava/src/main/java/com/cedarpolicy/model/AuthorizationResponse.java index 925d2cf3..96971e19 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/model/AuthorizationResponse.java +++ b/CedarJava/src/main/java/com/cedarpolicy/model/AuthorizationResponse.java @@ -21,8 +21,6 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.util.List; import java.util.Set; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; /** * The result of processing an AuthorizationRequest. The answer to the request is contained in the @@ -54,10 +52,10 @@ public static class Diagnostics { * Set of policyID's that caused the decision. For example, when a policy evaluates to Deny, * all deny policies that evaluated to True will appear in Reasons. */ - private ImmutableSet reason; + private Set reason; /** Set of errors and warnings returned by Cedar. */ - private ImmutableList errors; + private List errors; /** * Read the reasons and errors from a JSON object. @@ -69,8 +67,8 @@ public static class Diagnostics { public Diagnostics( @JsonProperty("reason") Set reason, @JsonProperty("errors") List errors) { - this.errors = ImmutableList.copyOf(errors); - this.reason = ImmutableSet.copyOf(reason); + this.errors = List.copyOf(errors); + this.reason = Set.copyOf(reason); } /** diff --git a/CedarJava/src/main/java/com/cedarpolicy/model/PartialAuthorizationRequest.java b/CedarJava/src/main/java/com/cedarpolicy/model/PartialAuthorizationRequest.java index 736ea57c..d5c846a1 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/model/PartialAuthorizationRequest.java +++ b/CedarJava/src/main/java/com/cedarpolicy/model/PartialAuthorizationRequest.java @@ -7,7 +7,6 @@ import com.cedarpolicy.value.EntityUID; import com.cedarpolicy.value.Value; import com.fasterxml.jackson.annotation.JsonInclude; -import com.google.common.collect.ImmutableMap; import java.util.Map; import java.util.Optional; @@ -158,7 +157,7 @@ public Builder resource(Entity resource) { * @return The builder. */ public Builder context(Map context) { - this.context = ImmutableMap.copyOf(context); + this.context = Map.copyOf(context); return this; } diff --git a/CedarJava/src/main/java/com/cedarpolicy/model/PartialAuthorizationResponse.java b/CedarJava/src/main/java/com/cedarpolicy/model/PartialAuthorizationResponse.java index c56fc9f1..e805f26c 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/model/PartialAuthorizationResponse.java +++ b/CedarJava/src/main/java/com/cedarpolicy/model/PartialAuthorizationResponse.java @@ -8,8 +8,9 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.collect.ImmutableSet; +import java.util.stream.Collectors; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -109,17 +110,17 @@ public boolean reachedDecision() { } public static final class ResidualPartialAuthorizationResponse extends PartialAuthorizationResponse { - private final ImmutableSet residuals; + private final Set residuals; public ResidualPartialAuthorizationResponse(Map residuals, Diagnostics diagnostics) { super(diagnostics); this.residuals = residuals.entrySet().stream() .map(e -> new Policy(e.getValue().toString(), e.getKey())) - .collect(ImmutableSet.toImmutableSet()); + .collect(Collectors.toUnmodifiableSet()); } public Set getResiduals() { - return this.residuals; + return Collections.unmodifiableSet(this.residuals); } @Override diff --git a/CedarJava/src/main/java/com/cedarpolicy/model/slice/TemplateInstantiation.java b/CedarJava/src/main/java/com/cedarpolicy/model/slice/TemplateInstantiation.java index ed31afaa..88482b71 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/model/slice/TemplateInstantiation.java +++ b/CedarJava/src/main/java/com/cedarpolicy/model/slice/TemplateInstantiation.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; -import com.google.common.collect.ImmutableList; /** Template instantiation. */ public class TemplateInstantiation { @@ -46,7 +45,7 @@ public TemplateInstantiation( @JsonProperty("instantiations") List instantiations) { this.templateId = templateId; this.resultPolicyId = resultPolicyId; - this.instantiations = ImmutableList.copyOf(instantiations); + this.instantiations = List.copyOf(instantiations); } /** Get the template ID. */ diff --git a/CedarJava/src/main/java/com/cedarpolicy/value/EntityTypeName.java b/CedarJava/src/main/java/com/cedarpolicy/value/EntityTypeName.java index 26ba0537..cbe08f26 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/value/EntityTypeName.java +++ b/CedarJava/src/main/java/com/cedarpolicy/value/EntityTypeName.java @@ -1,8 +1,6 @@ package com.cedarpolicy.value; -import com.google.common.base.Suppliers; - import java.util.List; import java.util.Optional; import java.util.Objects; @@ -34,7 +32,15 @@ public final class EntityTypeName { protected EntityTypeName(List namespace, String basename) { this.namespace = namespace; this.basename= basename; - this.entityTypeNameRepr = Suppliers.memoize(() -> getEntityTypeNameRepr(this)); + this.entityTypeNameRepr = new Supplier() { + String entityTypeNameRepr = null; + public String get() { + if (entityTypeNameRepr == null) { + entityTypeNameRepr = getEntityTypeNameRepr(EntityTypeName.this); + } + return entityTypeNameRepr; + } + }; } /** diff --git a/CedarJava/src/main/java/com/cedarpolicy/value/EntityUID.java b/CedarJava/src/main/java/com/cedarpolicy/value/EntityUID.java index 41a62b25..e556edb7 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/value/EntityUID.java +++ b/CedarJava/src/main/java/com/cedarpolicy/value/EntityUID.java @@ -21,7 +21,6 @@ import java.util.function.Supplier; import com.cedarpolicy.serializer.JsonEUID; -import com.google.common.base.Suppliers; /** * Represents a Cedar Entity UID. An entity UID contains both the entity type and a unique @@ -44,7 +43,15 @@ public final class EntityUID extends Value { public EntityUID(EntityTypeName type, EntityIdentifier id) { this.type = type; this.id = id; - this.euidRepr = Suppliers.memoize(() -> getEUIDRepr(type, id)); + this.euidRepr = new Supplier() { + String uidRepr = null; + public String get() { + if (uidRepr == null) { + uidRepr = getEUIDRepr(type, id); + } + return uidRepr; + } + }; } /**