@Override public boolean equals(final Object obj) { if (!(obj instanceof BlankNode)) { return false; } final BlankNode other = (BlankNode) obj; return uniqueReference().equals(other.uniqueReference()); }
@Test public void testCreateBlankNodeIdentifierTwice() throws Exception { BlankNode bnode1, bnode2, bnode3; bnode1 = factory.createBlankNode("example1"); bnode2 = factory.createBlankNode("example1"); bnode3 = factory.createBlankNode("differ"); // We don't know what the identifier is, but it MUST be the same assertEquals(bnode1.uniqueReference(), bnode2.uniqueReference()); // We don't know what the ntriplesString is, but it MUST be the same assertEquals(bnode1.ntriplesString(), bnode2.ntriplesString()); // and here it MUST differ assertNotEquals(bnode1.uniqueReference(), bnode3.uniqueReference()); assertNotEquals(bnode1.ntriplesString(), bnode3.ntriplesString()); }
@Test public void hashCodeBlankNode() throws Exception { final BlankNode bnode1 = factory.createBlankNode(); assertEquals(bnode1.uniqueReference().hashCode(), bnode1.hashCode()); }
assertNotEquals(testNull.ntriplesString(), testAutomatic1.ntriplesString()); assertNotEquals(testAutomatic1.ntriplesString(), testNull.ntriplesString()); assertNotEquals(testNull.ntriplesString(), testManual3a.ntriplesString()); assertNotEquals(testManual3a.ntriplesString(), testNull.ntriplesString()); assertEquals(testAutomatic1.ntriplesString(), testAutomatic1.ntriplesString()); assertEquals(testAutomatic2.ntriplesString(), testAutomatic2.ntriplesString()); assertNotEquals(testAutomatic1.ntriplesString(), testAutomatic2.ntriplesString()); assertNotEquals(testAutomatic2.ntriplesString(), testAutomatic1.ntriplesString()); assertNotEquals(testAutomatic1.ntriplesString(), testManual3a.ntriplesString()); assertEquals(testManual3b.ntriplesString(), testManual3a.ntriplesString()); assertNotEquals(testManual3a.ntriplesString(), testManual4.ntriplesString());
@Test public void testCreateBlankNodeIdentifierTwice() throws Exception { BlankNode bnode1, bnode2, bnode3; try { bnode1 = factory.createBlankNode("example1"); bnode2 = factory.createBlankNode("example1"); bnode3 = factory.createBlankNode("differ"); } catch (UnsupportedOperationException ex) { Assume.assumeNoException(ex); return; } assertEquals(bnode1.internalIdentifier(), bnode2.internalIdentifier()); // We don't know what the ntriplesString is, but it MUST be the same assertEquals(bnode1.ntriplesString(), bnode2.ntriplesString()); // and here it MUST differ assertNotEquals(bnode1.ntriplesString(), bnode3.ntriplesString()); }
assertNotEquals(testNull.internalIdentifier(), testAutomatic1.internalIdentifier()); assertNotEquals(testAutomatic1.internalIdentifier(), testNull.internalIdentifier()); assertNotEquals(testNull.internalIdentifier(), testManual3a.internalIdentifier()); assertNotEquals(testManual3a.internalIdentifier(), testNull.internalIdentifier()); assertEquals(testAutomatic1.internalIdentifier(), testAutomatic1.internalIdentifier()); assertEquals(testAutomatic2.internalIdentifier(), testAutomatic2.internalIdentifier()); assertNotEquals(testAutomatic1.internalIdentifier(), testAutomatic2.internalIdentifier()); assertNotEquals(testAutomatic2.internalIdentifier(), testAutomatic1.internalIdentifier()); assertNotEquals(testAutomatic1.internalIdentifier(), testManual3a.internalIdentifier()); assertEquals(testManual3b.internalIdentifier(), testManual3a.internalIdentifier()); assertNotEquals(testManual3a.internalIdentifier(), testManual4.internalIdentifier());
assertNotEquals(testNull.hashCode(), testAutomatic1.hashCode()); assertNotEquals(testAutomatic1.hashCode(), testNull.hashCode()); assertNotEquals(testNull.hashCode(), testManual3a.hashCode()); assertNotEquals(testManual3a.hashCode(), testNull.hashCode()); assertEquals(testAutomatic1.hashCode(), testAutomatic1.hashCode()); assertEquals(testAutomatic2.hashCode(), testAutomatic2.hashCode()); assertNotEquals(testAutomatic1.hashCode(), testAutomatic2.hashCode()); assertNotEquals(testAutomatic2.hashCode(), testAutomatic1.hashCode()); assertNotEquals(testAutomatic1.hashCode(), testManual3a.hashCode()); assertEquals(testManual3b.hashCode(), testManual3a.hashCode()); assertNotEquals(testManual3a.hashCode(), testManual4.hashCode());
@Test public void testPossiblyInvalidBlankNode() throws Exception { BlankNode withColon; try { withColon = factory.createBlankNode("with:colon"); } catch (final IllegalArgumentException ex) { // Good! return; } // Factory allows :colon, which is OK as long as it's not causing an // invalid ntriplesString assertFalse(withColon.ntriplesString().contains("with:colon")); // and creating it twice gets the same ntriplesString assertEquals(withColon.ntriplesString(), factory.createBlankNode("with:colon").ntriplesString()); }
private RDFTerm inScope(Optional<Graph> localScope, RDFTerm object) { if (!(object instanceof BlankNode) && !(object instanceof IRI) & !(object instanceof Literal)) { throw new IllegalArgumentException( "RDFTerm must be BlankNode, IRI or Literal"); } if (object instanceof BlankNode) { BlankNode blankNode = (BlankNode) object; return new BlankNodeImpl(Objects.requireNonNull(localScope), blankNode.internalIdentifier()); } else if (object instanceof IRI && !(object instanceof IRIImpl)) { IRI iri = (IRI) object; return new IRIImpl(iri.getIRIString()); } else if (object instanceof Literal && !(object instanceof LiteralImpl)) { Literal literal = (Literal) object; if (literal.getLanguageTag().isPresent()) { return new LiteralImpl(literal.getLexicalForm(), literal .getLanguageTag().get()); } else { IRI dataType = (IRI) inScope(localScope, literal.getDatatype()); return new LiteralImpl(literal.getLexicalForm(), dataType); } } else { return object; } }
assertNotEquals(testNull.hashCode(), testAutomatic1.hashCode()); assertNotEquals(testAutomatic1.hashCode(), testNull.hashCode()); assertNotEquals(testNull.hashCode(), testManual3a.hashCode()); assertNotEquals(testManual3a.hashCode(), testNull.hashCode()); assertEquals(testAutomatic1.hashCode(), testAutomatic1.hashCode()); assertEquals(testAutomatic2.hashCode(), testAutomatic2.hashCode()); assertNotEquals(testAutomatic1.hashCode(), testAutomatic2.hashCode()); assertNotEquals(testAutomatic2.hashCode(), testAutomatic1.hashCode()); assertNotEquals(testAutomatic1.hashCode(), testManual3a.hashCode()); assertEquals(testManual3b.hashCode(), testManual3a.hashCode()); assertNotEquals(testManual3a.hashCode(), testManual4.hashCode());
@Override public boolean equals(final Object other) { if (other == this) { return true; } if (other == null) { return false; } if (!(other instanceof BlankNode)) { return false; } final BlankNode bNode = (BlankNode) other; return uniqueReference().equals(bNode.uniqueReference()); }
assertNotEquals(testNull.ntriplesString(), testAutomatic1.ntriplesString()); assertNotEquals(testAutomatic1.ntriplesString(), testNull.ntriplesString()); assertNotEquals(testNull.ntriplesString(), testManual3a.ntriplesString()); assertNotEquals(testManual3a.ntriplesString(), testNull.ntriplesString()); assertEquals(testAutomatic1.ntriplesString(), testAutomatic1.ntriplesString()); assertEquals(testAutomatic2.ntriplesString(), testAutomatic2.ntriplesString()); assertNotEquals(testAutomatic1.ntriplesString(), testAutomatic2.ntriplesString()); assertNotEquals(testAutomatic2.ntriplesString(), testAutomatic1.ntriplesString()); assertNotEquals(testAutomatic1.ntriplesString(), testManual3a.ntriplesString()); assertEquals(testManual3b.ntriplesString(), testManual3a.ntriplesString()); assertNotEquals(testManual3a.ntriplesString(), testManual4.ntriplesString());
@Test public void testCreateBlankNode() throws Exception { BlankNode bnode; try { bnode = factory.createBlankNode(); } catch (UnsupportedOperationException ex) { Assume.assumeNoException(ex); return; } BlankNode bnode2 = factory.createBlankNode(); assertNotEquals( "Second blank node has not got a unique internal identifier", bnode.internalIdentifier(), bnode2.internalIdentifier()); }
@Override public boolean equals(final Object obj) { if (!(obj instanceof BlankNode)) { return false; } final BlankNode other = (BlankNode) obj; return uuid.equals(other.uniqueReference()); } };
@Test public void testPossiblyInvalidBlankNode() throws Exception { BlankNode withColon; try { withColon = factory.createBlankNode("with:colon"); } catch (UnsupportedOperationException ex) { Assume.assumeNoException("createBlankNode(String) not supported", ex); return; } catch (IllegalArgumentException ex) { // Good! return; } // Factory allows :colon, which is OK as long as it's not causing an // invalid ntriplesString assertFalse(withColon.ntriplesString().contains("with:colon")); // and creating it twice gets the same ntriplesString assertEquals(withColon.ntriplesString(), factory.createBlankNode("with:colon").ntriplesString()); }
@Test public void testCreateBlankNodeIdentifier() throws Exception { BlankNode bnode; try { bnode = factory.createBlankNode("example1"); } catch (UnsupportedOperationException ex) { Assume.assumeNoException(ex); return; } assertEquals("example1", bnode.internalIdentifier()); // .. but we can't assume the internal identifier leaks into // ntriplesString // assertEquals("_:example1", bnode.ntriplesString()); }
String asJsonLdString(final BlankNodeOrIRI blankNodeOrIRI) { if (blankNodeOrIRI == null) { return null; } if (blankNodeOrIRI instanceof IRI) { return ((IRI) blankNodeOrIRI).getIRIString(); } else if (blankNodeOrIRI instanceof BlankNode) { final BlankNode blankNode = (BlankNode) blankNodeOrIRI; final String ref = blankNode.uniqueReference(); if (ref.startsWith(bnodePrefix)) { // One of ours (but possibly not a JsonLdBlankNode) - // we can use the suffix directly return ref.replace(bnodePrefix, ""); } // Map to unique bnode identifier, e.g. // _:0dbd92ee-ab1a-45e7-bba2-7ade54f87ec5 final UUID uuid = UUID.nameUUIDFromBytes(ref.getBytes(StandardCharsets.UTF_8)); return "_:" + uuid; } else { throw new IllegalArgumentException("Expected a BlankNode or IRI, not: " + blankNodeOrIRI); } }
@Test public void testBnodes() { final BlankNode bn1 = rdf.createBlankNode(); final BlankNode bn2 = rdf.createBlankNode(); final Triple triple = rdf.createTriple(bn1, DC.subject, bn2); final LabelledTriple t = new LabelledTriple(triple, null, null); assertEquals(bn1.ntriplesString(), t.getSubject(), "Subject bnode value doesn't match!"); assertEquals(bn2.ntriplesString(), t.getObject(), "Object bnode value doesn't match!"); assertEquals(bn2.ntriplesString(), t.getObjectLabel(), "Object bnode label doesn't match!"); } }
@Override public boolean equals(@Nullable Object obj) { if (obj == this) { return true; } if (obj instanceof RDFResourceBlankNode) { RDFResourceBlankNode other = (RDFResourceBlankNode) obj; return resource.equals(other.resource); } // Commons RDF BlankNode.equals() contract if (obj instanceof BlankNode) { BlankNode blankNode = (BlankNode) obj; return uniqueReference().equals(blankNode.uniqueReference()); } return false; }
@Override public boolean equals(final Object obj) { if (!(obj instanceof BlankNode)) { return false; } final BlankNode other = (BlankNode) obj; return uuid.equals(other.uniqueReference()); } };