From e190aed271854f0bfda4e0edf8c057ab21861430 Mon Sep 17 00:00:00 2001 From: Moe Jangda Date: Thu, 2 Nov 2023 12:38:56 -0700 Subject: [PATCH] support java 11 --- pom.xml | 2 +- .../consumption/RepresentationConsumer.java | 45 +++++++++++++++++-- .../production/RepresentationProducer.java | 39 +++++++++++++++- .../RepresentationProducerCBOR.java | 2 +- 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index c552aca..4115c5c 100644 --- a/pom.xml +++ b/pom.xml @@ -43,7 +43,7 @@ UTF-8 UTF-8 - 17 + 11 github ${maven.build.timestamp} 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 4b728ee..f78263f 100644 --- a/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java +++ b/src/main/java/foundation/identity/did/representations/consumption/RepresentationConsumer.java @@ -3,13 +3,52 @@ import foundation.identity.did.representations.Representations; import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; public interface RepresentationConsumer { - public record Result(Map didDocument, - Map> representationSpecificEntries) { - + public class Result { + private final Map didDocument; + private final Map> representationSpecificEntries; + + public Result(Map didDocument, + Map> representationSpecificEntries) { + this.didDocument = Map.copyOf(didDocument); + Map> tempMap = new HashMap<>(); + representationSpecificEntries.forEach((key, value) -> + tempMap.put(key, Map.copyOf(value)) + ); + this.representationSpecificEntries = Collections.unmodifiableMap(tempMap); + } + + public Map getDidDocument() { + return didDocument; + } + + public Map> getRepresentationSpecificEntries() { + return representationSpecificEntries; + } + + // Implement equals and hashCode to mimic the behavior of a record + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Result result = (Result) o; + + if (!didDocument.equals(result.didDocument)) return false; + return representationSpecificEntries.equals(result.representationSpecificEntries); + } + + @Override + public int hashCode() { + int result = didDocument.hashCode(); + result = 31 * result + representationSpecificEntries.hashCode(); + return result; + } } public static Result consume(byte[] representation, String mediaType) throws IOException { 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 99b5cf3..3b7fe54 100644 --- a/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java +++ b/src/main/java/foundation/identity/did/representations/production/RepresentationProducer.java @@ -7,8 +7,43 @@ public interface RepresentationProducer { - public record Result(String mediaType, byte[] representation) { - + public class Result { + private final String mediaType; + private final byte[] representation; + + public Result(String mediaType, byte[] representation) { + this.mediaType = mediaType; + // It's important to clone the array to maintain immutability + this.representation = representation.clone(); + } + + public String getMediaType() { + return mediaType; + } + + public byte[] getRepresentation() { + // Also clone the array on getter to prevent external modification + return representation.clone(); + } + + // Implement equals and hashCode to mimic the behavior of a record + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Result result = (Result) o; + + if (!mediaType.equals(result.mediaType)) return false; + return java.util.Arrays.equals(representation, result.representation); + } + + @Override + public int hashCode() { + int result = mediaType.hashCode(); + result = 31 * result + java.util.Arrays.hashCode(representation); + return result; + } } 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 da86c43..9b6c316 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.getRepresentation()); byte[] representation = cborObject.EncodeToBytes(); return new RepresentationProducer.Result(MEDIA_TYPE, representation); }