From a264694fb03cca1ec46d8e8baebb3275cf8de8cd Mon Sep 17 00:00:00 2001 From: Markus Sabadello Date: Sun, 22 Oct 2023 16:25:06 +0200 Subject: [PATCH] chore: Various code cleanup --- src/main/java/foundation/identity/did/DID.java | 5 +++-- .../java/foundation/identity/did/DIDDocument.java | 2 +- src/main/java/foundation/identity/did/DIDURL.java | 8 ++++---- src/main/java/foundation/identity/did/Service.java | 2 +- .../foundation/identity/did/VerificationMethod.java | 2 +- .../foundation/identity/did/jsonld/DIDContexts.java | 11 ++++++----- .../foundation/identity/did/parser/DIDGrammar.java | 1 + .../RepresentationSpecificEntries.java | 11 +++++++---- .../consumption/RepresentationConsumer.java | 6 ++---- .../consumption/RepresentationConsumerCBOR.java | 2 +- .../production/RepresentationProducer.java | 5 +---- .../production/RepresentationProducerCBOR.java | 2 +- .../identity/did/validation/Validation.java | 8 ++++---- .../java/foundation/identity/did/DIDDocumentTest.java | 9 ++++++--- src/test/java/foundation/identity/did/DIDTest.java | 6 ++---- src/test/java/foundation/identity/did/DIDURLTest.java | 4 +--- .../identity/did/VerificationMethodTest.java | 4 ++-- 17 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/main/java/foundation/identity/did/DID.java b/src/main/java/foundation/identity/did/DID.java index 7e07093..912280a 100644 --- a/src/main/java/foundation/identity/did/DID.java +++ b/src/main/java/foundation/identity/did/DID.java @@ -12,6 +12,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -71,7 +72,7 @@ public void postBranch(int offset, int length) { if (keepParseTree) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ast.display(new PrintStream(byteArrayOutputStream)); - parseTree = new String(byteArrayOutputStream.toByteArray()); + parseTree = byteArrayOutputStream.toString(StandardCharsets.UTF_8); } String methodName = parsedStrings[0] == null ? null : parsedStrings[0]; @@ -198,7 +199,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (obj == null || ! (obj instanceof DID)) return false; + if (! (obj instanceof DID)) return false; if (obj == this) return true; return this.didString.equals(((DID) obj).didString); diff --git a/src/main/java/foundation/identity/did/DIDDocument.java b/src/main/java/foundation/identity/did/DIDDocument.java index 3943647..8fd0ec6 100644 --- a/src/main/java/foundation/identity/did/DIDDocument.java +++ b/src/main/java/foundation/identity/did/DIDDocument.java @@ -155,7 +155,7 @@ public B service(Service service) { } public static Builder> builder() { - return new Builder(new DIDDocument()); + return new Builder<>(new DIDDocument()); } public static DIDDocument fromJsonObject(Map jsonObject) { diff --git a/src/main/java/foundation/identity/did/DIDURL.java b/src/main/java/foundation/identity/did/DIDURL.java index 7b10d9d..c431ef1 100644 --- a/src/main/java/foundation/identity/did/DIDURL.java +++ b/src/main/java/foundation/identity/did/DIDURL.java @@ -28,7 +28,7 @@ public class DIDURL { private String path; private String query; private String fragment; - private Map parameters = new HashMap (); + private Map parameters; private String parseTree; DIDURL(String didUrlString, DID did, String path, String query, Map parameters, String fragment, String parseTree) { @@ -105,7 +105,7 @@ public void postBranch(int offset, int length) { if (keepParseTree) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ast.display(new PrintStream(byteArrayOutputStream)); - parseTree = new String(byteArrayOutputStream.toByteArray()); + parseTree = byteArrayOutputStream.toString(StandardCharsets.UTF_8); } String methodName = parsedStrings[0] == null ? null : parsedStrings[0]; @@ -171,7 +171,7 @@ public JsonObject toJsonObject(boolean addParseTree) { jsonObjectBuilder = jsonObjectBuilder .add("didUrlString", this.getDidUrlString() == null ? JsonValue.NULL : Json.createValue(this.getDidUrlString())) .add("did", this.getDid() == null ? JsonValue.NULL : this.getDid().toJsonObject(addParseTree)) - .add("parameters", this.getParameters() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap(this.getParameters())).build()) + .add("parameters", this.getParameters() == null ? JsonValue.NULL : Json.createObjectBuilder(new HashMap<>(this.getParameters())).build()) .add("path", this.getPath() == null ? JsonValue.NULL : Json.createValue(this.getPath())) .add("query", this.getQuery() == null ? JsonValue.NULL : Json.createValue(this.getQuery())) .add("fragment", this.getFragment() == null ? JsonValue.NULL : Json.createValue(this.getFragment())); @@ -283,7 +283,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (obj == null || ! (obj instanceof DIDURL)) return false; + if (! (obj instanceof DIDURL)) return false; if (obj == this) return true; return this.didUrlString.equals(((DIDURL) obj).didUrlString); diff --git a/src/main/java/foundation/identity/did/Service.java b/src/main/java/foundation/identity/did/Service.java index 25429b3..386ba99 100644 --- a/src/main/java/foundation/identity/did/Service.java +++ b/src/main/java/foundation/identity/did/Service.java @@ -57,7 +57,7 @@ public B serviceEndpoint(Object serviceEndpoint) { } public static Builder> builder() { - return new Builder(new Service()); + return new Builder<>(new Service()); } public static Service fromJsonObject(Map jsonObject) { diff --git a/src/main/java/foundation/identity/did/VerificationMethod.java b/src/main/java/foundation/identity/did/VerificationMethod.java index cf877f3..5ae14d3 100644 --- a/src/main/java/foundation/identity/did/VerificationMethod.java +++ b/src/main/java/foundation/identity/did/VerificationMethod.java @@ -99,7 +99,7 @@ public B publicKeyJwk(Map publicKeyJwk) { } public static Builder> builder() { - return new Builder(new VerificationMethod()); + return new Builder<>(new VerificationMethod()); } public static VerificationMethod fromJsonObject(Map jsonObject) { diff --git a/src/main/java/foundation/identity/did/jsonld/DIDContexts.java b/src/main/java/foundation/identity/did/jsonld/DIDContexts.java index c312220..905aa8e 100644 --- a/src/main/java/foundation/identity/did/jsonld/DIDContexts.java +++ b/src/main/java/foundation/identity/did/jsonld/DIDContexts.java @@ -9,6 +9,7 @@ import java.net.URI; import java.util.HashMap; import java.util.Map; +import java.util.Objects; public class DIDContexts { @@ -28,15 +29,15 @@ public class DIDContexts { CONTEXTS = new HashMap<>(); CONTEXTS.put(JSONLD_CONTEXT_W3_NS_DID_V1, - JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3-ns-did-v1.jsonld"))); + JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3-ns-did-v1.jsonld")))); CONTEXTS.put(JSONLD_CONTEXT_W3_2019_DID_V1, - JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3-2019-did-v1.jsonld"))); + JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3-2019-did-v1.jsonld")))); CONTEXTS.put(JSONLD_CONTEXT_W3ID_DID_V1, - JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3id-did-v1.jsonld"))); + JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3id-did-v1.jsonld")))); CONTEXTS.put(JSONLD_CONTEXT_W3ID_DID_V011, - JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3id-did-v011.jsonld"))); + JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3id-did-v011.jsonld")))); CONTEXTS.put(JSONLD_CONTEXT_W3ID_VERESONE_V1, - JsonDocument.of(MediaType.JSON_LD, DIDContexts.class.getResourceAsStream("diddocument-context-w3id-veresone-v1.jsonld"))); + JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(DIDContexts.class.getResourceAsStream("diddocument-context-w3id-veresone-v1.jsonld")))); for (Map.Entry context : CONTEXTS.entrySet()) { context.getValue().setDocumentUrl(context.getKey()); diff --git a/src/main/java/foundation/identity/did/parser/DIDGrammar.java b/src/main/java/foundation/identity/did/parser/DIDGrammar.java index 51635fc..b3df711 100644 --- a/src/main/java/foundation/identity/did/parser/DIDGrammar.java +++ b/src/main/java/foundation/identity/did/parser/DIDGrammar.java @@ -6,6 +6,7 @@ package foundation.identity.did.parser; import apg.Grammar; + import java.io.PrintStream; public class DIDGrammar extends Grammar{ diff --git a/src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java b/src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java index a68167b..a6bb261 100644 --- a/src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java +++ b/src/main/java/foundation/identity/did/representations/RepresentationSpecificEntries.java @@ -1,16 +1,19 @@ package foundation.identity.did.representations; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class RepresentationSpecificEntries { - public static final List JSONLD_REPRESENTATION_SPECIFIC_ENTRY_NAMES = Arrays.asList( + public static final List JSONLD_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of( "@context" ); - public static final List JSON_REPRESENTATION_SPECIFIC_ENTRY_NAMES = Arrays.asList(); + public static final List JSON_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of(); - public static final List CBOR_REPRESENTATION_SPECIFIC_ENTRY_NAMES = Arrays.asList(); + public static final List CBOR_REPRESENTATION_SPECIFIC_ENTRY_NAMES = List.of(); public static final Map> REPRESENTATION_SPECIFIC_ENTRY_NAMES = new HashMap<>(); public static final List ALL_REPRESENTATION_SPECIFIC_ENTRY_NAMES = new ArrayList<>(); diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java index 88071e4..4b728ee 100644 --- a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java +++ b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java @@ -7,11 +7,9 @@ public interface RepresentationConsumer { - public static class Result { + public record Result(Map didDocument, + Map> representationSpecificEntries) { - public Result(Map didDocument, Map> representationSpecificEntries) { this.didDocument = didDocument; this.representationSpecificEntries = representationSpecificEntries; } - public Map didDocument; - public Map> representationSpecificEntries; } public static Result consume(byte[] representation, String mediaType) throws IOException { diff --git a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerCBOR.java b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerCBOR.java index c2a532f..3d49531 100644 --- a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerCBOR.java +++ b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumerCBOR.java @@ -24,7 +24,7 @@ private RepresentationConsumerCBOR() { @Override public RepresentationConsumer.Result consume(byte[] representation) throws IOException { CBORObject cborObject = CBORObject.DecodeFromBytes(representation); - Map map = (Map) cborObject.ToObject(LinkedHashMap.class); + Map map = cborObject.ToObject(LinkedHashMap.class); return this.detectRepresentationSpecificEntries(map); } } diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java index 5a6095d..99b5cf3 100644 --- a/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java +++ b/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java @@ -7,11 +7,8 @@ public interface RepresentationProducer { - public static class Result { + public record Result(String mediaType, byte[] representation) { - public Result(String mediaType, byte[] representation) { this.mediaType = mediaType; this.representation = representation; } - public String mediaType; - public byte[] representation; } public static RepresentationProducer.Result produce(Map didDocument, Map representationSpecificEntries, String mediaType) throws IOException { diff --git a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerCBOR.java b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerCBOR.java index 6005724..da86c43 100644 --- a/src/main/java/foundation/identity/did/representations/production/RepresentationProducerCBOR.java +++ b/src/main/java/foundation/identity/did/representations/production/RepresentationProducerCBOR.java @@ -26,7 +26,7 @@ private RepresentationProducerCBOR() { public RepresentationProducer.Result produce(Map didDocument, Map representationSpecificEntries) throws IOException { RepresentationProducer.Result jsonResult = RepresentationProducerJSON.getInstance().produce(didDocument, representationSpecificEntries); - CBORObject cborObject = CBORObject.FromJSONBytes(jsonResult.representation); + CBORObject cborObject = CBORObject.FromJSONBytes(jsonResult.representation()); byte[] representation = cborObject.EncodeToBytes(); return new RepresentationProducer.Result(MEDIA_TYPE, representation); } diff --git a/src/main/java/foundation/identity/did/validation/Validation.java b/src/main/java/foundation/identity/did/validation/Validation.java index 45d432c..945d018 100644 --- a/src/main/java/foundation/identity/did/validation/Validation.java +++ b/src/main/java/foundation/identity/did/validation/Validation.java @@ -37,10 +37,10 @@ private static void validateRun(Runnable runnable, String message) throws Illega public static void validate(DIDDocument didDocument) throws IllegalStateException { - validateRun(() -> { validateTrue(didDocument.getJsonObject() != null); }, "Bad or missing JSON object."); - validateRun(() -> { validateTrue(didDocument.getContexts().size() > 0); }, "Bad or missing '@context'."); - validateRun(() -> { validateUrl(didDocument.getContexts().get(0)); }, "@context must be a valid URI: " + didDocument.getContexts().get(0)); - validateRun(() -> { validateTrue(DIDDocument.DEFAULT_JSONLD_CONTEXTS[0].equals(didDocument.getContexts().get(0))); }, "First value of @context must be " + DIDDocument.DEFAULT_JSONLD_CONTEXTS[0] + ": " + didDocument.getContexts().get(0)); + validateRun(() -> validateTrue(didDocument.getJsonObject() != null), "Bad or missing JSON object."); + validateRun(() -> validateTrue(! didDocument.getContexts().isEmpty()), "Bad or missing '@context'."); + validateRun(() -> validateUrl(didDocument.getContexts().get(0)), "@context must be a valid URI: " + didDocument.getContexts().get(0)); + validateRun(() -> validateTrue(DIDDocument.DEFAULT_JSONLD_CONTEXTS[0].equals(didDocument.getContexts().get(0))), "First value of @context must be " + DIDDocument.DEFAULT_JSONLD_CONTEXTS[0] + ": " + didDocument.getContexts().get(0)); validateRun(() -> { if (didDocument.getId() != null) validateUrl(didDocument.getId()); }, "'id' must be a valid URI."); } } diff --git a/src/test/java/foundation/identity/did/DIDDocumentTest.java b/src/test/java/foundation/identity/did/DIDDocumentTest.java index e1f4f31..08da56c 100644 --- a/src/test/java/foundation/identity/did/DIDDocumentTest.java +++ b/src/test/java/foundation/identity/did/DIDDocumentTest.java @@ -4,7 +4,10 @@ import org.junit.jupiter.api.Test; import java.net.URI; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,7 +42,7 @@ private DIDDocument create() { VerificationMethod verificationMethod = VerificationMethod.builder() .id(URI.create("did:ex:123#key-1")) - .types(Arrays.asList("Ed25519VerificationKey2018")) + .types(List.of("Ed25519VerificationKey2018")) .publicKeyBase58("000000000") .build(); @@ -47,7 +50,7 @@ private DIDDocument create() { // DID DOCUMENT services - List services = new ArrayList(); + List services = new ArrayList<>(); Service service = Service.builder() .type("DIDCommService") diff --git a/src/test/java/foundation/identity/did/DIDTest.java b/src/test/java/foundation/identity/did/DIDTest.java index 1b32ca9..d595190 100644 --- a/src/test/java/foundation/identity/did/DIDTest.java +++ b/src/test/java/foundation/identity/did/DIDTest.java @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class DIDTest { @@ -21,8 +21,6 @@ public void testDID() throws Exception { @Test public void testInvalidDID() throws Exception { - Assertions.assertThrows(ParserException.class, () -> { - DID.fromString("did:ex"); - }); + Assertions.assertThrows(ParserException.class, () -> DID.fromString("did:ex")); } } diff --git a/src/test/java/foundation/identity/did/DIDURLTest.java b/src/test/java/foundation/identity/did/DIDURLTest.java index fb7b33d..e8e5705 100644 --- a/src/test/java/foundation/identity/did/DIDURLTest.java +++ b/src/test/java/foundation/identity/did/DIDURLTest.java @@ -49,9 +49,7 @@ public void testBareDIDURL() throws Exception { @Test public void testInvalidDIDURL() throws Exception { - Assertions.assertThrows(ParserException.class, () -> { - DIDURL.fromString("did:ex/my/path?ab=1&cd=2#frag"); - }); + Assertions.assertThrows(ParserException.class, () -> DIDURL.fromString("did:ex/my/path?ab=1&cd=2#frag")); } @Test diff --git a/src/test/java/foundation/identity/did/VerificationMethodTest.java b/src/test/java/foundation/identity/did/VerificationMethodTest.java index 7d80062..83b13d5 100644 --- a/src/test/java/foundation/identity/did/VerificationMethodTest.java +++ b/src/test/java/foundation/identity/did/VerificationMethodTest.java @@ -3,7 +3,7 @@ import org.junit.jupiter.api.Test; import java.net.URI; -import java.util.Arrays; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -34,7 +34,7 @@ private VerificationMethod create() { return VerificationMethod.builder() .id(URI.create("did:ex:123#key-1")) - .types(Arrays.asList("Ed25519VerificationKey2018")) + .types(List.of("Ed25519VerificationKey2018")) .publicKeyBase58("000000000") .build(); }