Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rdf API Improvements (toString, equals, hashCode) #322

Merged
merged 8 commits into from
Feb 23, 2024
22 changes: 11 additions & 11 deletions src/main/java/com/apicatalog/jsonld/serialization/RdfToJsonld.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -398,20 +398,20 @@ 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;
reference.subject = subject;
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()) {
Expand All @@ -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);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/apicatalog/rdf/RdfTriple.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
10 changes: 7 additions & 3 deletions src/main/java/com/apicatalog/rdf/impl/RdfLiteralImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -85,15 +86,18 @@ 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) {
builder.append("^^");
} else if (dataType != null && !XsdConstants.STRING.equals(dataType)) {
builder.append("^^<");
builder.append(dataType);
builder.append('>');
}

return builder.toString();
Expand Down
40 changes: 35 additions & 5 deletions src/main/java/com/apicatalog/rdf/impl/RdfNQuadImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.apicatalog.rdf.impl;

import java.util.Objects;
import java.util.Optional;

import com.apicatalog.rdf.RdfNQuad;
Expand All @@ -37,10 +38,39 @@ public Optional<RdfResource> 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
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())
filip26 marked this conversation as resolved.
Show resolved Hide resolved
return false;
RdfNQuadImpl other = (RdfNQuadImpl) obj;
return Objects.equals(graphName, other.graphName);
}

}
5 changes: 4 additions & 1 deletion src/main/java/com/apicatalog/rdf/impl/RdfResourceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) + '>';
}
}
25 changes: 21 additions & 4 deletions src/main/java/com/apicatalog/rdf/impl/RdfTripleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,9 +52,24 @@ public RdfValue getObject() {

@Override
public String toString() {
return "RdfTripleImpl[subject=" + subject
+ ", predicate=" + predicate
+ ", object=" + object
+ "]";
return subject + " " + predicate + " " + 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);
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/apicatalog/rdf/io/nquad/NQuadsWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void writeValue(RdfValue object) throws IOException {
}

if (object.isIRI()) {
writeIri(object.toString());
writeIri(object.getValue());
return;
}

Expand All @@ -87,7 +87,7 @@ public void writeValue(RdfValue object) throws IOException {
}

if (object.isBlankNode()) {
writer.write(object.toString());
writer.write(object.getValue());
return;
}

Expand Down
Loading