@Override public boolean equals(final Object obj) { if (!(obj instanceof BlankNode)) { return false; } final BlankNode other = (BlankNode) obj; return uniqueReference().equals(other.uniqueReference()); }
@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()); }
@Override public boolean equals(final Object obj) { if (!(obj instanceof BlankNode)) { return false; } final BlankNode other = (BlankNode) obj; return uuid.equals(other.uniqueReference()); } };
@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; }
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); } }
@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()); } };
/** * Skolemize a blank node. * * @param term the RDF term * @return a skolemized node, if a blank node; otherwise the original term */ default RDFTerm skolemize(final RDFTerm term) { if (term instanceof BlankNode) { return getInstance().createIRI(TRELLIS_BNODE_PREFIX + ((BlankNode) term).uniqueReference()); } return term; }
@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; }
/** * Skolemize a blank node. * * @param term the RDF term * @return a skolemized node, if a blank node; otherwise the original term */ default RDFTerm skolemize(final RDFTerm term) { if (term instanceof BlankNode) { return getInstance().createIRI(TRELLIS_BNODE_PREFIX + ((BlankNode) term).uniqueReference()); } return term; }
assertNotEquals(testNull.uniqueReference(), testAutomatic1.uniqueReference()); assertNotEquals(testAutomatic1.uniqueReference(), testNull.uniqueReference()); assertNotEquals(testNull.uniqueReference(), testManual3a.uniqueReference()); assertNotEquals(testManual3a.uniqueReference(), testNull.uniqueReference()); assertEquals(testAutomatic1.uniqueReference(), testAutomatic1.uniqueReference()); assertEquals(testAutomatic2.uniqueReference(), testAutomatic2.uniqueReference()); assertNotEquals(testAutomatic1.uniqueReference(), testAutomatic2.uniqueReference()); assertNotEquals(testAutomatic2.uniqueReference(), testAutomatic1.uniqueReference()); assertNotEquals(testAutomatic1.uniqueReference(), testManual3a.uniqueReference()); assertEquals(testManual3b.uniqueReference(), testManual3a.uniqueReference()); assertNotEquals(testManual3a.uniqueReference(), testManual4.uniqueReference());
@Test public void testCreateBlankNode() throws Exception { final BlankNode bnode = factory.createBlankNode(); final BlankNode bnode2 = factory.createBlankNode(); assertNotEquals("Second blank node has not got a unique internal identifier", bnode.uniqueReference(), bnode2.uniqueReference()); }
private void setUpResourceService() { when(mockResourceService.supportedInteractionModels()).thenReturn(allInteractionModels); when(mockResourceService.get(any(IRI.class))).thenAnswer(inv -> completedFuture(mockResource)); when(mockResourceService.create(any(Metadata.class), any(Dataset.class))).thenReturn(completedFuture(null)); when(mockResourceService.replace(any(Metadata.class), any(Dataset.class))).thenReturn(completedFuture(null)); when(mockResourceService.delete(any(Metadata.class))).thenReturn(completedFuture(null)); when(mockResourceService.add(any(IRI.class), any(Dataset.class))).thenReturn(completedFuture(null)); when(mockResourceService.skolemize(any(Literal.class))).then(returnsFirstArg()); when(mockResourceService.skolemize(any(IRI.class))).then(returnsFirstArg()); when(mockResourceService.skolemize(any(BlankNode.class))).thenAnswer(inv -> rdf.createIRI(TRELLIS_BNODE_PREFIX + ((BlankNode) inv.getArgument(0)).uniqueReference())); when(mockResourceService.toInternal(any(RDFTerm.class), any())).thenCallRealMethod(); when(mockResourceService.toExternal(any(RDFTerm.class), any())).thenCallRealMethod(); when(mockResourceService.touch(any(IRI.class))).thenReturn(completedFuture(null)); }
final String ref = ((BlankNode) term).uniqueReference(); if (ref.startsWith(bnodePrefix)) {
@Test public void hashCodeBlankNode() throws Exception { final BlankNode bnode1 = factory.createBlankNode(); assertEquals(bnode1.uniqueReference().hashCode(), bnode1.hashCode()); }
private <T extends RDFTerm> RDFTerm internallyMap(final T object) { if (object == null || object instanceof SimpleRDFTerm) { return object; } if (object instanceof BlankNode && !(object instanceof BlankNodeImpl)) { final BlankNode blankNode = (BlankNode) object; // This guarantees that adding the same BlankNode multiple times to // this graph will generate a local object that is mapped to an // equivalent object, based on the code in the package private // BlankNodeImpl class return factory.createBlankNode(blankNode.uniqueReference()); } else if (object instanceof IRI && !(object instanceof IRIImpl)) { final IRI iri = (IRI) object; return factory.createIRI(iri.getIRIString()); } else if (object instanceof Literal && !(object instanceof LiteralImpl)) { final Literal literal = (Literal) object; if (literal.getLanguageTag().isPresent()) { return factory.createLiteral(literal.getLexicalForm(), literal.getLanguageTag().get()); } return factory.createLiteral(literal.getLexicalForm(), (IRI) internallyMap(literal.getDatatype())); } else { throw new IllegalArgumentException("Not a BlankNode, IRI or Literal: " + object); } }
@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()); }
when(mockResourceService.skolemize(any(IRI.class))).then(returnsFirstArg()); when(mockResourceService.skolemize(any(BlankNode.class))).thenAnswer(inv -> rdf.createIRI(TRELLIS_BNODE_PREFIX + ((BlankNode) inv.getArgument(0)).uniqueReference())); when(mockResourceService.touch(any(IRI.class))).thenReturn(completedFuture(null)); when(mockResource.stream()).thenAnswer(inv -> Stream.of(
private <T extends RDFTerm> RDFTerm internallyMap(final T object) { if (object == null || object instanceof SimpleRDFTerm) { // No need to re-map our own objects. // We support null as internallyMap() is also used by the filters, // and the // factory constructors later do null checks return object; } if (object instanceof BlankNode) { final BlankNode blankNode = (BlankNode) object; // This guarantees that adding the same BlankNode multiple times to // this graph will generate a local object that is mapped to an // equivalent object, based on the code in the package private // BlankNodeImpl class return factory.createBlankNode(blankNode.uniqueReference()); } else if (object instanceof IRI) { final IRI iri = (IRI) object; return factory.createIRI(iri.getIRIString()); } else if (object instanceof Literal) { final Literal literal = (Literal) object; if (literal.getLanguageTag().isPresent()) { return factory.createLiteral(literal.getLexicalForm(), literal.getLanguageTag().get()); } return factory.createLiteral(literal.getLexicalForm(), (IRI) internallyMap(literal.getDatatype())); } else { throw new IllegalArgumentException("RDFTerm was neither a BlankNode, IRI nor Literal: " + object); } }
@Test public void testCreateBlankNodeIdentifierTwiceDifferentFactories() throws Exception { BlankNode bnode1, differentFactory; bnode1 = factory.createBlankNode(); // it MUST differ from a second factory differentFactory = createFactory().createBlankNode(); // NOTE: We can't make similar assumption if we provide a // name to createBlankNode(String) as its documentation // only says: // // * BlankNodes created using this method with the same parameter, for // * different instances of RDFFactory, SHOULD NOT be equivalent. // // https://github.com/apache/incubator-commonsrdf/pull/7#issuecomment-92312779 assertNotEquals(bnode1, differentFactory); assertNotEquals(bnode1.uniqueReference(), differentFactory.uniqueReference()); // but we can't require: // assertNotEquals(bnode1.ntriplesString(), // differentFactory.ntriplesString()); }