From 2299524d3e3bb270786ced78253122a3a89913d2 Mon Sep 17 00:00:00 2001 From: Filip Date: Tue, 20 Feb 2024 01:05:47 +0100 Subject: [PATCH 1/4] Add hashCode() and equals() methods --- .../java/com/apicatalog/rdf/RdfTriple.java | 5 ++--- .../com/apicatalog/rdf/impl/RdfNQuadImpl.java | 22 +++++++++++++++++++ .../apicatalog/rdf/impl/RdfTripleImpl.java | 20 +++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/apicatalog/rdf/RdfTriple.java b/src/main/java/com/apicatalog/rdf/RdfTriple.java index 5153556e..b5a9d1b0 100644 --- a/src/main/java/com/apicatalog/rdf/RdfTriple.java +++ b/src/main/java/com/apicatalog/rdf/RdfTriple.java @@ -28,18 +28,17 @@ public interface RdfTriple { RdfResource getSubject(); /** - * An absolute IRI or blank node identifier denoting the predicate of the triple. + * An absolute IRI or blank node identifier denoting the predicate of the + * triple. * * @return an absolute URI or blank node */ RdfResource getPredicate(); - /** * An absolute IRI or blank node identifier or {@link RdfLiteral}. * * @return {@link RdfValue} */ RdfValue getObject(); - } diff --git a/src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java b/src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java index f5c3ffe3..b99e9739 100644 --- a/src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java +++ b/src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java @@ -15,6 +15,7 @@ */ package com.apicatalog.rdf.impl; +import java.util.Objects; import java.util.Optional; import com.apicatalog.rdf.RdfNQuad; @@ -43,4 +44,25 @@ public String toString() { + ", graph=" + graphName + "]"; } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + Objects.hash(graphName); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + RdfNQuadImpl other = (RdfNQuadImpl) obj; + return Objects.equals(graphName, other.graphName); + } + } diff --git a/src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java b/src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java index febd275e..70f09378 100644 --- a/src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java +++ b/src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java @@ -15,6 +15,8 @@ */ package com.apicatalog.rdf.impl; +import java.util.Objects; + import com.apicatalog.rdf.RdfResource; import com.apicatalog.rdf.RdfTriple; import com.apicatalog.rdf.RdfValue; @@ -55,4 +57,22 @@ public String toString() { + ", object=" + object + "]"; } + + @Override + public int hashCode() { + return Objects.hash(object, predicate, subject); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + RdfTripleImpl other = (RdfTripleImpl) obj; + return Objects.equals(object, other.object) && Objects.equals(predicate, other.predicate) && Objects.equals(subject, other.subject); + } + } From 1ab65c1e4bc3b84d7799e2f8f70453d3093de5a6 Mon Sep 17 00:00:00 2001 From: Filip Date: Tue, 20 Feb 2024 02:21:17 +0100 Subject: [PATCH 2/4] toString() method returns n-quads serialization --- .../apicatalog/rdf/impl/RdfLiteralImpl.java | 7 +++++-- .../com/apicatalog/rdf/impl/RdfNQuadImpl.java | 20 +++++++++++++------ .../apicatalog/rdf/impl/RdfResourceImpl.java | 5 ++++- .../apicatalog/rdf/impl/RdfTripleImpl.java | 5 +---- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java b/src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java index 8914720a..71b42379 100644 --- a/src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java +++ b/src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java @@ -19,6 +19,7 @@ import java.util.Optional; import com.apicatalog.rdf.RdfLiteral; +import com.apicatalog.rdf.io.nquad.NQuadsWriter; import com.apicatalog.rdf.lang.RdfConstants; import com.apicatalog.rdf.lang.XsdConstants; @@ -85,13 +86,15 @@ public boolean equals(Object obj) { public String toString() { StringBuilder builder = new StringBuilder(); - builder.append(value); + builder.append('"'); + builder.append(NQuadsWriter.escape(value)); + builder.append('"'); if (langTag != null) { builder.append('@'); builder.append(langTag); - } else if (dataType != null) { + } else if (dataType != null && !XsdConstants.STRING.equals(dataType)) { builder.append("^^"); builder.append(dataType); } diff --git a/src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java b/src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java index b99e9739..46cdb553 100644 --- a/src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java +++ b/src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java @@ -38,11 +38,19 @@ public Optional getGraphName() { @Override public String toString() { - return "RdfNQuadImpl[subject=" + getSubject() - + ", predicate=" + getPredicate() - + ", object=" + getObject() - + ", graph=" + graphName - + "]"; + final StringBuilder builder = new StringBuilder() + .append(getSubject()) + .append(' ') + .append(getPredicate()) + .append(' ') + .append(getObject()) + .append(' '); + + if (graphName != null) { + builder.append(graphName).append(' '); + } + + return builder.append('.').toString(); } @Override @@ -64,5 +72,5 @@ public boolean equals(Object obj) { RdfNQuadImpl other = (RdfNQuadImpl) obj; return Objects.equals(graphName, other.graphName); } - + } diff --git a/src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java b/src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java index 77903d24..96b754ac 100644 --- a/src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java +++ b/src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java @@ -66,6 +66,9 @@ public boolean equals(Object obj) { @Override public String toString() { - return Objects.toString(value); + if (blankNode) { + return Objects.toString(value); + } + return '<' + Objects.toString(value) + ">"; } } diff --git a/src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java b/src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java index 70f09378..0f3c7cd5 100644 --- a/src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java +++ b/src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java @@ -52,10 +52,7 @@ public RdfValue getObject() { @Override public String toString() { - return "RdfTripleImpl[subject=" + subject - + ", predicate=" + predicate - + ", object=" + object - + "]"; + return subject + " " + predicate + " " + object + " ."; } @Override From e459cba57a0b543e77e335eac9f2689995578b2d Mon Sep 17 00:00:00 2001 From: Filip Date: Tue, 20 Feb 2024 02:34:01 +0100 Subject: [PATCH 3/4] Fix RDF processing s/toString/getValue/g --- .../jsonld/serialization/RdfToJsonld.java | 22 +++++++++---------- .../apicatalog/rdf/impl/RdfResourceImpl.java | 2 +- .../apicatalog/rdf/io/nquad/NQuadsWriter.java | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java b/src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java index 9f07d3c5..18327cff 100644 --- a/src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java +++ b/src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java @@ -333,8 +333,8 @@ private void step5(final String graphName, final RdfGraph graph) throws JsonLdEr // 5.7. for (final RdfTriple triple : graph.toList()) { - final String subject = triple.getSubject().toString(); - final String predicate = triple.getPredicate().toString(); + final String subject = triple.getSubject().getValue(); + final String predicate = triple.getPredicate().getValue(); // 5.7.1. if (!graphMap.contains(graphName, subject)) { @@ -350,9 +350,9 @@ private void step5(final String graphName, final RdfGraph graph) throws JsonLdEr // 5.7.4. if ((triple.getObject().isBlankNode() || triple.getObject().isIRI()) - && !graphMap.contains(graphName, triple.getObject().toString())) { + && !graphMap.contains(graphName, triple.getObject().getValue())) { - graphMap.set(graphName, triple.getObject().toString(), Keywords.ID, JsonProvider.instance().createValue(triple.getObject().toString())); + graphMap.set(graphName, triple.getObject().getValue(), Keywords.ID, JsonProvider.instance().createValue(triple.getObject().getValue())); } // 5.7.5. @@ -364,11 +364,11 @@ private void step5(final String graphName, final RdfGraph graph) throws JsonLdEr JsonArray types = type.get().asJsonArray(); - graphMap.set(graphName, subject, Keywords.TYPE, JsonProvider.instance().createArrayBuilder(types).add(triple.getObject().toString()).build()); + graphMap.set(graphName, subject, Keywords.TYPE, JsonProvider.instance().createArrayBuilder(types).add(triple.getObject().getValue()).build()); } else { - graphMap.set(graphName, subject, Keywords.TYPE, JsonProvider.instance().createArrayBuilder().add(triple.getObject().toString()).build()); + graphMap.set(graphName, subject, Keywords.TYPE, JsonProvider.instance().createArrayBuilder().add(triple.getObject().getValue()).build()); } continue; @@ -398,7 +398,7 @@ private void step5(final String graphName, final RdfGraph graph) throws JsonLdEr } // 5.7.9. - if (triple.getObject().isIRI() && RdfConstants.NIL.equals(triple.getObject().toString())) { + if (triple.getObject().isIRI() && RdfConstants.NIL.equals(triple.getObject().getValue())) { Reference reference = new Reference(); reference.graphName = graphName; @@ -406,12 +406,12 @@ private void step5(final String graphName, final RdfGraph graph) throws JsonLdEr reference.property = predicate; reference.value = value; - graphMap.addUsage(graphName, triple.getObject().toString(), reference); + graphMap.addUsage(graphName, triple.getObject().getValue(), reference); // 5.7.10. - } else if (referenceOnce.containsKey(triple.getObject().toString())) { + } else if (referenceOnce.containsKey(triple.getObject().getValue())) { - referenceOnce.put(triple.getObject().toString(), null); + referenceOnce.put(triple.getObject().getValue(), null); // 5.7.11. } else if (triple.getObject().isBlankNode()) { @@ -422,7 +422,7 @@ private void step5(final String graphName, final RdfGraph graph) throws JsonLdEr reference.property = predicate; reference.value = value; - referenceOnce.put(triple.getObject().toString(), reference); + referenceOnce.put(triple.getObject().getValue(), reference); } } } diff --git a/src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java b/src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java index 96b754ac..96d716d8 100644 --- a/src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java +++ b/src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java @@ -69,6 +69,6 @@ public String toString() { if (blankNode) { return Objects.toString(value); } - return '<' + Objects.toString(value) + ">"; + return '<' + Objects.toString(value) + '>'; } } diff --git a/src/main/java/com/apicatalog/rdf/io/nquad/NQuadsWriter.java b/src/main/java/com/apicatalog/rdf/io/nquad/NQuadsWriter.java index d724179b..80bd2969 100644 --- a/src/main/java/com/apicatalog/rdf/io/nquad/NQuadsWriter.java +++ b/src/main/java/com/apicatalog/rdf/io/nquad/NQuadsWriter.java @@ -77,7 +77,7 @@ public void writeValue(RdfValue object) throws IOException { } if (object.isIRI()) { - writeIri(object.toString()); + writeIri(object.getValue()); return; } @@ -87,7 +87,7 @@ public void writeValue(RdfValue object) throws IOException { } if (object.isBlankNode()) { - writer.write(object.toString()); + writer.write(object.getValue()); return; } From e02cf9aa0b29cc6a8087fbc0308bf43e47a14e70 Mon Sep 17 00:00:00 2001 From: Filip Date: Tue, 20 Feb 2024 23:22:36 +0100 Subject: [PATCH 4/4] Fix RdfLiteralImpl.toString() datatype serialization --- src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java b/src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java index 71b42379..72125642 100644 --- a/src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java +++ b/src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java @@ -95,8 +95,9 @@ public String toString() { builder.append(langTag); } else if (dataType != null && !XsdConstants.STRING.equals(dataType)) { - builder.append("^^"); + builder.append("^^<"); builder.append(dataType); + builder.append('>'); } return builder.toString();