http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java index aaa5636..968e279 100644 --- a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java +++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java @@ -17,19 +17,17 @@ */ package org.apache.commons.rdf.api; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.Optional; - import org.junit.Assume; import org.junit.Before; import org.junit.Test; +import java.util.Optional; + +import static org.junit.Assert.*; + /** * Test Graph implementation - * <p> + * <p/> * To add to your implementation's tests, create a subclass with a name ending * in <code>Test</code> and provide {@link #createFactory()} which minimally * must support {@link RDFTermFactory#createGraph()} and @@ -41,239 +39,239 @@ import org.junit.Test; */ public abstract class AbstractGraphTest { - private RDFTermFactory factory; - private Graph graph; - private IRI alice; - private IRI bob; - private IRI name; - private IRI knows; - private IRI member; - private BlankNode bnode1; - private BlankNode bnode2; - private Literal aliceName; - private Literal bobName; - private Literal secretClubName; - private Literal companyName; - private Triple bobNameTriple; - - public abstract RDFTermFactory createFactory(); - - @Before - public void createGraphAndAdd() { - factory = createFactory(); - graph = factory.createGraph(); - assertEquals(0, graph.size()); - - alice = factory.createIRI("http://example.com/alice"); - bob = factory.createIRI("http://example.com/bob"); - name = factory.createIRI("http://xmlns.com/foaf/0.1/name"); - knows = factory.createIRI("http://xmlns.com/foaf/0.1/knows"); - member = factory.createIRI("http://xmlns.com/foaf/0.1/member"); - try { - bnode1 = factory.createBlankNode("org1"); - bnode2 = factory.createBlankNode("org2"); - } catch (UnsupportedOperationException ex) { - // leave as null - } - - try { - secretClubName = factory.createLiteral("The Secret Club"); - companyName = factory.createLiteral("A company"); - aliceName = factory.createLiteral("Alice"); - bobName = factory.createLiteral("Bob", "en-US"); - } catch (UnsupportedOperationException ex) { - // leave as null - } - - if (aliceName != null) { - graph.add(alice, name, aliceName); - } - graph.add(alice, knows, bob); - - if (bnode1 != null) { - graph.add(alice, member, bnode1); - } - - if (bobName != null) { - try { - bobNameTriple = factory.createTriple(bob, name, bobName); - } catch (UnsupportedOperationException ex) { - // leave as null - } - if (bobNameTriple != null) { - graph.add(bobNameTriple); - } - } - if (bnode1 != null) { - graph.add(factory.createTriple(bob, member, bnode1)); - graph.add(factory.createTriple(bob, member, bnode2)); - if (secretClubName != null) { - graph.add(bnode1, name, secretClubName); - graph.add(bnode2, name, companyName); - } - } - } - - @Test - public void graphToString() { - Assume.assumeNotNull(aliceName, companyName); - System.out.println(graph); - assertTrue(graph - .toString() - .contains( - "<http://example.com/alice> <http://xmlns.com/foaf/0.1/name> \"Alice\" .")); - assertTrue(graph.toString().contains( - " <http://xmlns.com/foaf/0.1/name> \"A company\" .")); - - } - - @Test - public void size() throws Exception { - assertTrue(graph.size() > 0); - Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, - companyName, bobNameTriple); - // Can only reliably predict size if we could create all triples - assertEquals(8, graph.size()); - } - - @Test - public void contains() throws Exception { - assertFalse(graph.contains(bob, knows, alice)); // or so he claims.. - - assertTrue(graph.contains(alice, knows, bob)); - - Optional<? extends Triple> first = graph.getTriples().skip(4) - .findFirst(); - Assume.assumeTrue(first.isPresent()); - Triple existingTriple = first.get(); - assertTrue(graph.contains(existingTriple)); - - Triple nonExistingTriple = factory.createTriple(bob, knows, alice); - assertFalse(graph.contains(nonExistingTriple)); - - Triple triple = null; - try { - triple = factory.createTriple(alice, knows, bob); - } catch (UnsupportedOperationException ex) { - } - if (triple != null) { - // FIXME: Should not this always be true? - // assertTrue(graph.contains(triple)); - } - } - - @Test - public void remove() throws Exception { - long fullSize = graph.size(); - graph.remove(alice, knows, bob); - long shrunkSize = graph.size(); - assertEquals(1, fullSize - shrunkSize); - - graph.remove(alice, knows, bob); - assertEquals(shrunkSize, graph.size()); // unchanged - - graph.add(alice, knows, bob); - graph.add(alice, knows, bob); - graph.add(alice, knows, bob); - // Undetermined size at this point -- but at least it - // should be bigger - assertTrue(graph.size() > shrunkSize); - - // and after a single remove they should all be gone - graph.remove(alice, knows, bob); - assertEquals(shrunkSize, graph.size()); - - Optional<? extends Triple> anyTriple = graph.getTriples().findAny(); - Assume.assumeTrue(anyTriple.isPresent()); - - Triple otherTriple = anyTriple.get(); - graph.remove(otherTriple); - assertEquals(shrunkSize - 1, graph.size()); - graph.remove(otherTriple); - assertEquals(shrunkSize - 1, graph.size()); // no change - graph.add(otherTriple); - assertEquals(shrunkSize, graph.size()); - } - - @Test - public void clear() throws Exception { - graph.clear(); - assertFalse(graph.contains(alice, knows, bob)); - assertEquals(0, graph.size()); - graph.clear(); // no-op - assertEquals(0, graph.size()); - } - - @Test - public void getTriples() throws Exception { - - long tripleCount = graph.getTriples().count(); - assertTrue(tripleCount > 0); - assertTrue(graph.getTriples().allMatch(t -> graph.contains(t))); - // Check exact count - Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, - companyName, bobNameTriple); - assertEquals(8, tripleCount); - } - - @Test - public void getTriplesQuery() throws Exception { - - long aliceCount = graph.getTriples(alice, null, null).count(); - assertTrue(aliceCount > 0); - Assume.assumeNotNull(aliceName); - assertEquals(3, aliceCount); - - Assume.assumeNotNull(bnode1, bnode2, bobName, companyName, secretClubName); - assertEquals(4, graph.getTriples(null, name, null).count()); - Assume.assumeNotNull(bnode1); - assertEquals(3, graph.getTriples(null, member, null).count()); - } - - /** - * An attempt to use the Java 8 streams to look up a more complicated query. - * - * FYI, the equivalent SPARQL version (untested): - * - * <pre> - * SELECT ?orgName WHERE { - * ?org foaf:name ?orgName . - * ?alice foaf:member ?org . - * ?bob foaf:member ?org . - * ?alice foaf:knows ?bob . - * FILTER NOT EXIST { ?bob foaf:knows ?alice } - * } - * </pre> - * - * @throws Exception - */ - @Test - public void whyJavaStreamsMightNotTakeOverFromSparql() throws Exception { - Assume.assumeNotNull(bnode1, bnode2, secretClubName); - // Find a secret organizations - assertEquals( - "\"The Secret Club\"", - graph.getTriples(null, knows, null) - // Find One-way "knows" - .filter(t -> !graph.contains( - (BlankNodeOrIRI) t.getObject(), knows, - t.getSubject())) - .map(knowsTriple -> graph - // and those they know, what are they member of? - .getTriples( - (BlankNodeOrIRI) knowsTriple - .getObject(), member, null) - // keep those which first-guy is a member of - .filter(memberTriple -> graph.contains( - knowsTriple.getSubject(), member, - // First hit is good enough - memberTriple.getObject())).findFirst() - .get().getObject()) - // then look up the name of that org - .map(org -> graph - .getTriples((BlankNodeOrIRI) org, name, null) - .findFirst().get().getObject().ntriplesString()) - .findFirst().get()); - - } + private RDFTermFactory factory; + private Graph graph; + private IRI alice; + private IRI bob; + private IRI name; + private IRI knows; + private IRI member; + private BlankNode bnode1; + private BlankNode bnode2; + private Literal aliceName; + private Literal bobName; + private Literal secretClubName; + private Literal companyName; + private Triple bobNameTriple; + + public abstract RDFTermFactory createFactory(); + + @Before + public void createGraphAndAdd() { + factory = createFactory(); + graph = factory.createGraph(); + assertEquals(0, graph.size()); + + alice = factory.createIRI("http://example.com/alice"); + bob = factory.createIRI("http://example.com/bob"); + name = factory.createIRI("http://xmlns.com/foaf/0.1/name"); + knows = factory.createIRI("http://xmlns.com/foaf/0.1/knows"); + member = factory.createIRI("http://xmlns.com/foaf/0.1/member"); + try { + bnode1 = factory.createBlankNode("org1"); + bnode2 = factory.createBlankNode("org2"); + } catch (UnsupportedOperationException ex) { + // leave as null + } + + try { + secretClubName = factory.createLiteral("The Secret Club"); + companyName = factory.createLiteral("A company"); + aliceName = factory.createLiteral("Alice"); + bobName = factory.createLiteral("Bob", "en-US"); + } catch (UnsupportedOperationException ex) { + // leave as null + } + + if (aliceName != null) { + graph.add(alice, name, aliceName); + } + graph.add(alice, knows, bob); + + if (bnode1 != null) { + graph.add(alice, member, bnode1); + } + + if (bobName != null) { + try { + bobNameTriple = factory.createTriple(bob, name, bobName); + } catch (UnsupportedOperationException ex) { + // leave as null + } + if (bobNameTriple != null) { + graph.add(bobNameTriple); + } + } + if (bnode1 != null) { + graph.add(factory.createTriple(bob, member, bnode1)); + graph.add(factory.createTriple(bob, member, bnode2)); + if (secretClubName != null) { + graph.add(bnode1, name, secretClubName); + graph.add(bnode2, name, companyName); + } + } + } + + @Test + public void graphToString() { + Assume.assumeNotNull(aliceName, companyName); + System.out.println(graph); + assertTrue(graph + .toString() + .contains( + "<http://example.com/alice> <http://xmlns.com/foaf/0.1/name> \"Alice\" .")); + assertTrue(graph.toString().contains( + " <http://xmlns.com/foaf/0.1/name> \"A company\" .")); + + } + + @Test + public void size() throws Exception { + assertTrue(graph.size() > 0); + Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, + companyName, bobNameTriple); + // Can only reliably predict size if we could create all triples + assertEquals(8, graph.size()); + } + + @Test + public void contains() throws Exception { + assertFalse(graph.contains(bob, knows, alice)); // or so he claims.. + + assertTrue(graph.contains(alice, knows, bob)); + + Optional<? extends Triple> first = graph.getTriples().skip(4) + .findFirst(); + Assume.assumeTrue(first.isPresent()); + Triple existingTriple = first.get(); + assertTrue(graph.contains(existingTriple)); + + Triple nonExistingTriple = factory.createTriple(bob, knows, alice); + assertFalse(graph.contains(nonExistingTriple)); + + Triple triple = null; + try { + triple = factory.createTriple(alice, knows, bob); + } catch (UnsupportedOperationException ex) { + } + if (triple != null) { + // FIXME: Should not this always be true? + // assertTrue(graph.contains(triple)); + } + } + + @Test + public void remove() throws Exception { + long fullSize = graph.size(); + graph.remove(alice, knows, bob); + long shrunkSize = graph.size(); + assertEquals(1, fullSize - shrunkSize); + + graph.remove(alice, knows, bob); + assertEquals(shrunkSize, graph.size()); // unchanged + + graph.add(alice, knows, bob); + graph.add(alice, knows, bob); + graph.add(alice, knows, bob); + // Undetermined size at this point -- but at least it + // should be bigger + assertTrue(graph.size() > shrunkSize); + + // and after a single remove they should all be gone + graph.remove(alice, knows, bob); + assertEquals(shrunkSize, graph.size()); + + Optional<? extends Triple> anyTriple = graph.getTriples().findAny(); + Assume.assumeTrue(anyTriple.isPresent()); + + Triple otherTriple = anyTriple.get(); + graph.remove(otherTriple); + assertEquals(shrunkSize - 1, graph.size()); + graph.remove(otherTriple); + assertEquals(shrunkSize - 1, graph.size()); // no change + graph.add(otherTriple); + assertEquals(shrunkSize, graph.size()); + } + + @Test + public void clear() throws Exception { + graph.clear(); + assertFalse(graph.contains(alice, knows, bob)); + assertEquals(0, graph.size()); + graph.clear(); // no-op + assertEquals(0, graph.size()); + } + + @Test + public void getTriples() throws Exception { + + long tripleCount = graph.getTriples().count(); + assertTrue(tripleCount > 0); + assertTrue(graph.getTriples().allMatch(t -> graph.contains(t))); + // Check exact count + Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, + companyName, bobNameTriple); + assertEquals(8, tripleCount); + } + + @Test + public void getTriplesQuery() throws Exception { + + long aliceCount = graph.getTriples(alice, null, null).count(); + assertTrue(aliceCount > 0); + Assume.assumeNotNull(aliceName); + assertEquals(3, aliceCount); + + Assume.assumeNotNull(bnode1, bnode2, bobName, companyName, secretClubName); + assertEquals(4, graph.getTriples(null, name, null).count()); + Assume.assumeNotNull(bnode1); + assertEquals(3, graph.getTriples(null, member, null).count()); + } + + /** + * An attempt to use the Java 8 streams to look up a more complicated query. + * <p/> + * FYI, the equivalent SPARQL version (untested): + * <p/> + * <pre> + * SELECT ?orgName WHERE { + * ?org foaf:name ?orgName . + * ?alice foaf:member ?org . + * ?bob foaf:member ?org . + * ?alice foaf:knows ?bob . + * FILTER NOT EXIST { ?bob foaf:knows ?alice } + * } + * </pre> + * + * @throws Exception + */ + @Test + public void whyJavaStreamsMightNotTakeOverFromSparql() throws Exception { + Assume.assumeNotNull(bnode1, bnode2, secretClubName); + // Find a secret organizations + assertEquals( + "\"The Secret Club\"", + graph.getTriples(null, knows, null) + // Find One-way "knows" + .filter(t -> !graph.contains( + (BlankNodeOrIRI) t.getObject(), knows, + t.getSubject())) + .map(knowsTriple -> graph + // and those they know, what are they member of? + .getTriples( + (BlankNodeOrIRI) knowsTriple + .getObject(), member, null) + // keep those which first-guy is a member of + .filter(memberTriple -> graph.contains( + knowsTriple.getSubject(), member, + // First hit is good enough + memberTriple.getObject())).findFirst() + .get().getObject()) + // then look up the name of that org + .map(org -> graph + .getTriples((BlankNodeOrIRI) org, name, null) + .findFirst().get().getObject().ntriplesString()) + .findFirst().get()); + + } }
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java index b45f6ba..9eec559 100644 --- a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java +++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java @@ -17,397 +17,394 @@ */ package org.apache.commons.rdf.api; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotSame; - import org.junit.Assume; import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.*; + /** * Test RDFTermFactory implementation (and thus its RDFTerm implementations) - * <p> + * <p/> * To add to your implementation's tests, create a subclass with a name ending * in <code>Test</code> and provide {@link #createFactory()} which minimally * supports one of the operations, but ideally supports all operations. - * + * * @see RDFTermFactory * @see com.github.commonsrdf.simple.SimpleRDFTermFactoryTest */ public abstract class AbstractRDFTermFactoryTest { - private RDFTermFactory factory; - - /** - * testCreate a new, distinct {@link RDFTermFactory} object using the - * implementation being tested here. - * - * @return a new, distinct {@link RDFTermFactory} object using the - * implementation being tested here - */ - public abstract RDFTermFactory createFactory(); - - @Before - public void setUp() { - factory = createFactory(); - } - - @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()); - } - - @Test - public void testCreateBlankNodeIdentifierEmpty() throws Exception { - try { - factory.createBlankNode(""); - } catch (UnsupportedOperationException e) { - Assume.assumeNoException(e); - } catch (IllegalArgumentException e) { - // Expected exception - } - } - - @Test - public void testCreateBlankNodeIdentifier() throws Exception { - try { - factory.createBlankNode("example1"); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - // We can't assume anything about the resulting bnode - // assertEquals("example1", bnode.internalIdentifier()); - // assertEquals("_:example1", bnode.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; - } - // We don't know what the identifier is, but it MUST be the same - 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.internalIdentifier(), - bnode3.internalIdentifier()); - assertNotEquals(bnode1.ntriplesString(), bnode3.ntriplesString()); - } - - @Test - public void testCreateGraph() { - Graph graph; - try { - graph = factory.createGraph(); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - - assertEquals("Graph was not empty", 0, graph.size()); - graph.add(factory.createBlankNode(), - factory.createIRI("http://example.com/"), - factory.createBlankNode()); - - Graph graph2 = factory.createGraph(); - assertNotSame(graph, graph2); - assertEquals("Graph was empty after adding", 1, graph.size()); - assertEquals("New graph was not empty", 0, graph2.size()); - } - - @Test - public void testCreateIRI() throws Exception { - IRI example; - try { - example = factory.createIRI("http://example.com/"); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException("createIRI not supported", ex); - return; - } - - assertEquals("http://example.com/", example.getIRIString()); - assertEquals("<http://example.com/>", example.ntriplesString()); - - IRI term = factory.createIRI("http://example.com/vocab#term"); - assertEquals("http://example.com/vocab#term", term.getIRIString()); - assertEquals("<http://example.com/vocab#term>", term.ntriplesString()); - - // and now for the international fun! - - IRI latin1 = factory.createIRI("http://accént.example.com/première"); - assertEquals("http://accént.example.com/première", - latin1.getIRIString()); - assertEquals("<http://accént.example.com/première>", - latin1.ntriplesString()); - - IRI cyrillic = factory.createIRI("http://example.иÑпÑÑание/ÐиÑиллиÑа"); - assertEquals("http://example.иÑпÑÑание/ÐиÑиллиÑа", - cyrillic.getIRIString()); - assertEquals("<http://example.иÑпÑÑание/ÐиÑиллиÑа>", - cyrillic.ntriplesString()); - - IRI deseret = factory.createIRI("http://ð.example.com/ð"); - assertEquals("http://ð.example.com/ð", deseret.getIRIString()); - assertEquals("<http://ð.example.com/ð>", deseret.ntriplesString()); - } - - @Test - public void testCreateIRIRelative() throws Exception { - // Although relative IRIs are defined in - // http://www.w3.org/TR/rdf11-concepts/#section-IRIs - // it is not a requirement for an implementation to support - // it (all instances of an relative IRI should eventually - // be possible to resolve to an absolute IRI) - try { - factory.createIRI("../relative"); - } catch (UnsupportedOperationException | IllegalArgumentException ex) { - Assume.assumeNoException(ex); - return; - } - IRI relative = factory.createIRI("../relative"); - assertEquals("../relative", relative.getIRIString()); - assertEquals("<../relative>", relative.ntriplesString()); - - IRI relativeTerm = factory.createIRI("../relative#term"); - assertEquals("../relative#term", relativeTerm.getIRIString()); - assertEquals("<../relative#term>", relativeTerm.ntriplesString()); - - IRI emptyRelative = factory.createIRI(""); // <> equals the base URI - assertEquals("", emptyRelative.getIRIString()); - assertEquals("<>", emptyRelative.ntriplesString()); - } - - @Test - public void testCreateLiteral() throws Exception { - Literal example; - try { - example = factory.createLiteral("Example"); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - - assertEquals("Example", example.getLexicalForm()); - assertFalse(example.getLanguageTag().isPresent()); - assertEquals("http://www.w3.org/2001/XMLSchema#string", example - .getDatatype().getIRIString()); - // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html - assertEquals("\"Example\"", example.ntriplesString()); - } - - @Test - public void testCreateLiteralDateTime() throws Exception { - Literal dateTime; - try { - dateTime = factory - .createLiteral( - "2014-12-27T00:50:00T-0600", - factory.createIRI("http://www.w3.org/2001/XMLSchema#dateTime")); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - assertEquals("2014-12-27T00:50:00T-0600", dateTime.getLexicalForm()); - assertFalse(dateTime.getLanguageTag().isPresent()); - assertEquals("http://www.w3.org/2001/XMLSchema#dateTime", dateTime - .getDatatype().getIRIString()); - assertEquals( - "\"2014-12-27T00:50:00T-0600\"^^<http://www.w3.org/2001/XMLSchema#dateTime>", - dateTime.ntriplesString()); - } - - @Test - public void testCreateLiteralLang() throws Exception { - Literal example; - try { - example = factory.createLiteral("Example", "en"); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - - assertEquals("Example", example.getLexicalForm()); - assertEquals("en", example.getLanguageTag().get()); - assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", - example.getDatatype().getIRIString()); - assertEquals("\"Example\"@en", example.ntriplesString()); - } - - @Test - public void testCreateLiteralLangISO693_3() throws Exception { - // see https://issues.apache.org/jira/browse/JENA-827 - Literal vls; - try { - vls = factory.createLiteral("Herbert Van de Sompel", "vls"); // JENA-827 - // reference - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - - assertEquals("vls", vls.getLanguageTag().get()); - assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", - vls.getDatatype().getIRIString()); - assertEquals("\"Herbert Van de Sompel\"@vls", vls.ntriplesString()); - } - - @Test - public void testCreateLiteralString() throws Exception { - Literal example; - try { - example = factory.createLiteral("Example", factory - .createIRI("http://www.w3.org/2001/XMLSchema#string")); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - assertEquals("Example", example.getLexicalForm()); - assertFalse(example.getLanguageTag().isPresent()); - assertEquals("http://www.w3.org/2001/XMLSchema#string", example - .getDatatype().getIRIString()); - // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html - assertEquals("\"Example\"", example.ntriplesString()); - } - - @Test - public void testCreateTripleBnodeBnode() { - BlankNode subject; - IRI predicate; - BlankNode object; - Triple triple; - try { - subject = factory.createBlankNode("b1"); - predicate = factory.createIRI("http://example.com/pred"); - object = factory.createBlankNode("b2"); - triple = factory.createTriple(subject, predicate, object); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - - // bnode equivalence should be OK as we used the same - // factory and have not yet inserted Triple into a Graph - assertEquals(subject, triple.getSubject()); - assertEquals(predicate, triple.getPredicate()); - assertEquals(object, triple.getObject()); - } - - @Test - public void testCreateTripleBnodeIRI() { - BlankNode subject; - IRI predicate; - IRI object; - Triple triple; - try { - subject = factory.createBlankNode("b1"); - predicate = factory.createIRI("http://example.com/pred"); - object = factory.createIRI("http://example.com/obj"); - triple = factory.createTriple(subject, predicate, object); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - - // bnode equivalence should be OK as we used the same - // factory and have not yet inserted Triple into a Graph - assertEquals(subject, triple.getSubject()); - assertEquals(predicate, triple.getPredicate()); - assertEquals(object, triple.getObject()); - } - - @Test - public void testCreateTripleBnodeTriple() { - BlankNode subject; - IRI predicate; - Literal object; - Triple triple; - try { - subject = factory.createBlankNode(); - predicate = factory.createIRI("http://example.com/pred"); - object = factory.createLiteral("Example", "en"); - triple = factory.createTriple(subject, predicate, object); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException(ex); - return; - } - - // bnode equivalence should be OK as we used the same - // factory and have not yet inserted Triple into a Graph - assertEquals(subject, triple.getSubject()); - assertEquals(predicate, triple.getPredicate()); - assertEquals(object, triple.getObject()); - } - - @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(expected = IllegalArgumentException.class) - public void testInvalidIRI() throws Exception { - try { - factory.createIRI("<no_brackets>"); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException("createIRI not supported", ex); - return; - } - } - - @Test(expected = IllegalArgumentException.class) - public void testInvalidLiteralLang() throws Exception { - try { - factory.createLiteral("Example", "with space"); - } catch (UnsupportedOperationException ex) { - Assume.assumeNoException( - "createLiteral(String,String) not supported", ex); - return; - } - } - - @Test(expected = Exception.class) - public void testInvalidTriplePredicate() { - BlankNode subject = factory.createBlankNode("b1"); - BlankNode predicate = factory.createBlankNode("b2"); - BlankNode object = factory.createBlankNode("b3"); - factory.createTriple(subject, (IRI) predicate, object); - } + private RDFTermFactory factory; + + /** + * testCreate a new, distinct {@link RDFTermFactory} object using the + * implementation being tested here. + * + * @return a new, distinct {@link RDFTermFactory} object using the + * implementation being tested here + */ + public abstract RDFTermFactory createFactory(); + + @Before + public void setUp() { + factory = createFactory(); + } + + @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()); + } + + @Test + public void testCreateBlankNodeIdentifierEmpty() throws Exception { + try { + factory.createBlankNode(""); + } catch (UnsupportedOperationException e) { + Assume.assumeNoException(e); + } catch (IllegalArgumentException e) { + // Expected exception + } + } + + @Test + public void testCreateBlankNodeIdentifier() throws Exception { + try { + factory.createBlankNode("example1"); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + // We can't assume anything about the resulting bnode + // assertEquals("example1", bnode.internalIdentifier()); + // assertEquals("_:example1", bnode.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; + } + // We don't know what the identifier is, but it MUST be the same + 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.internalIdentifier(), + bnode3.internalIdentifier()); + assertNotEquals(bnode1.ntriplesString(), bnode3.ntriplesString()); + } + + @Test + public void testCreateGraph() { + Graph graph; + try { + graph = factory.createGraph(); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + + assertEquals("Graph was not empty", 0, graph.size()); + graph.add(factory.createBlankNode(), + factory.createIRI("http://example.com/"), + factory.createBlankNode()); + + Graph graph2 = factory.createGraph(); + assertNotSame(graph, graph2); + assertEquals("Graph was empty after adding", 1, graph.size()); + assertEquals("New graph was not empty", 0, graph2.size()); + } + + @Test + public void testCreateIRI() throws Exception { + IRI example; + try { + example = factory.createIRI("http://example.com/"); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException("createIRI not supported", ex); + return; + } + + assertEquals("http://example.com/", example.getIRIString()); + assertEquals("<http://example.com/>", example.ntriplesString()); + + IRI term = factory.createIRI("http://example.com/vocab#term"); + assertEquals("http://example.com/vocab#term", term.getIRIString()); + assertEquals("<http://example.com/vocab#term>", term.ntriplesString()); + + // and now for the international fun! + + IRI latin1 = factory.createIRI("http://accént.example.com/première"); + assertEquals("http://accént.example.com/première", + latin1.getIRIString()); + assertEquals("<http://accént.example.com/première>", + latin1.ntriplesString()); + + IRI cyrillic = factory.createIRI("http://example.иÑпÑÑание/ÐиÑиллиÑа"); + assertEquals("http://example.иÑпÑÑание/ÐиÑиллиÑа", + cyrillic.getIRIString()); + assertEquals("<http://example.иÑпÑÑание/ÐиÑиллиÑа>", + cyrillic.ntriplesString()); + + IRI deseret = factory.createIRI("http://ð.example.com/ð"); + assertEquals("http://ð.example.com/ð", deseret.getIRIString()); + assertEquals("<http://ð.example.com/ð>", deseret.ntriplesString()); + } + + @Test + public void testCreateIRIRelative() throws Exception { + // Although relative IRIs are defined in + // http://www.w3.org/TR/rdf11-concepts/#section-IRIs + // it is not a requirement for an implementation to support + // it (all instances of an relative IRI should eventually + // be possible to resolve to an absolute IRI) + try { + factory.createIRI("../relative"); + } catch (UnsupportedOperationException | IllegalArgumentException ex) { + Assume.assumeNoException(ex); + return; + } + IRI relative = factory.createIRI("../relative"); + assertEquals("../relative", relative.getIRIString()); + assertEquals("<../relative>", relative.ntriplesString()); + + IRI relativeTerm = factory.createIRI("../relative#term"); + assertEquals("../relative#term", relativeTerm.getIRIString()); + assertEquals("<../relative#term>", relativeTerm.ntriplesString()); + + IRI emptyRelative = factory.createIRI(""); // <> equals the base URI + assertEquals("", emptyRelative.getIRIString()); + assertEquals("<>", emptyRelative.ntriplesString()); + } + + @Test + public void testCreateLiteral() throws Exception { + Literal example; + try { + example = factory.createLiteral("Example"); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + + assertEquals("Example", example.getLexicalForm()); + assertFalse(example.getLanguageTag().isPresent()); + assertEquals("http://www.w3.org/2001/XMLSchema#string", example + .getDatatype().getIRIString()); + // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html + assertEquals("\"Example\"", example.ntriplesString()); + } + + @Test + public void testCreateLiteralDateTime() throws Exception { + Literal dateTime; + try { + dateTime = factory + .createLiteral( + "2014-12-27T00:50:00T-0600", + factory.createIRI("http://www.w3.org/2001/XMLSchema#dateTime")); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + assertEquals("2014-12-27T00:50:00T-0600", dateTime.getLexicalForm()); + assertFalse(dateTime.getLanguageTag().isPresent()); + assertEquals("http://www.w3.org/2001/XMLSchema#dateTime", dateTime + .getDatatype().getIRIString()); + assertEquals( + "\"2014-12-27T00:50:00T-0600\"^^<http://www.w3.org/2001/XMLSchema#dateTime>", + dateTime.ntriplesString()); + } + + @Test + public void testCreateLiteralLang() throws Exception { + Literal example; + try { + example = factory.createLiteral("Example", "en"); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + + assertEquals("Example", example.getLexicalForm()); + assertEquals("en", example.getLanguageTag().get()); + assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", + example.getDatatype().getIRIString()); + assertEquals("\"Example\"@en", example.ntriplesString()); + } + + @Test + public void testCreateLiteralLangISO693_3() throws Exception { + // see https://issues.apache.org/jira/browse/JENA-827 + Literal vls; + try { + vls = factory.createLiteral("Herbert Van de Sompel", "vls"); // JENA-827 + // reference + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + + assertEquals("vls", vls.getLanguageTag().get()); + assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", + vls.getDatatype().getIRIString()); + assertEquals("\"Herbert Van de Sompel\"@vls", vls.ntriplesString()); + } + + @Test + public void testCreateLiteralString() throws Exception { + Literal example; + try { + example = factory.createLiteral("Example", factory + .createIRI("http://www.w3.org/2001/XMLSchema#string")); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + assertEquals("Example", example.getLexicalForm()); + assertFalse(example.getLanguageTag().isPresent()); + assertEquals("http://www.w3.org/2001/XMLSchema#string", example + .getDatatype().getIRIString()); + // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html + assertEquals("\"Example\"", example.ntriplesString()); + } + + @Test + public void testCreateTripleBnodeBnode() { + BlankNode subject; + IRI predicate; + BlankNode object; + Triple triple; + try { + subject = factory.createBlankNode("b1"); + predicate = factory.createIRI("http://example.com/pred"); + object = factory.createBlankNode("b2"); + triple = factory.createTriple(subject, predicate, object); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + + // bnode equivalence should be OK as we used the same + // factory and have not yet inserted Triple into a Graph + assertEquals(subject, triple.getSubject()); + assertEquals(predicate, triple.getPredicate()); + assertEquals(object, triple.getObject()); + } + + @Test + public void testCreateTripleBnodeIRI() { + BlankNode subject; + IRI predicate; + IRI object; + Triple triple; + try { + subject = factory.createBlankNode("b1"); + predicate = factory.createIRI("http://example.com/pred"); + object = factory.createIRI("http://example.com/obj"); + triple = factory.createTriple(subject, predicate, object); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + + // bnode equivalence should be OK as we used the same + // factory and have not yet inserted Triple into a Graph + assertEquals(subject, triple.getSubject()); + assertEquals(predicate, triple.getPredicate()); + assertEquals(object, triple.getObject()); + } + + @Test + public void testCreateTripleBnodeTriple() { + BlankNode subject; + IRI predicate; + Literal object; + Triple triple; + try { + subject = factory.createBlankNode(); + predicate = factory.createIRI("http://example.com/pred"); + object = factory.createLiteral("Example", "en"); + triple = factory.createTriple(subject, predicate, object); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + return; + } + + // bnode equivalence should be OK as we used the same + // factory and have not yet inserted Triple into a Graph + assertEquals(subject, triple.getSubject()); + assertEquals(predicate, triple.getPredicate()); + assertEquals(object, triple.getObject()); + } + + @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(expected = IllegalArgumentException.class) + public void testInvalidIRI() throws Exception { + try { + factory.createIRI("<no_brackets>"); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException("createIRI not supported", ex); + return; + } + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidLiteralLang() throws Exception { + try { + factory.createLiteral("Example", "with space"); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException( + "createLiteral(String,String) not supported", ex); + return; + } + } + + @Test(expected = Exception.class) + public void testInvalidTriplePredicate() { + BlankNode subject = factory.createBlankNode("b1"); + BlankNode predicate = factory.createBlankNode("b2"); + BlankNode object = factory.createBlankNode("b3"); + factory.createTriple(subject, (IRI) predicate, object); + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/api/src/test/java/org/apache/commons/rdf/api/DefaultRDFTermFactoryTest.java ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/commons/rdf/api/DefaultRDFTermFactoryTest.java b/api/src/test/java/org/apache/commons/rdf/api/DefaultRDFTermFactoryTest.java index 054a27c..674140d 100644 --- a/api/src/test/java/org/apache/commons/rdf/api/DefaultRDFTermFactoryTest.java +++ b/api/src/test/java/org/apache/commons/rdf/api/DefaultRDFTermFactoryTest.java @@ -21,14 +21,13 @@ package org.apache.commons.rdf.api; * The default RDFTermFactory might be useless (every method throws * UnsupportedOperationException), but this test ensures that * AbstractRDFTermFactoryTest does not fall over on unsupported operations. - * */ public class DefaultRDFTermFactoryTest extends AbstractRDFTermFactoryTest { - @Override - public RDFTermFactory createFactory() { - return new RDFTermFactory() { - }; - } + @Override + public RDFTermFactory createFactory() { + return new RDFTermFactory() { + }; + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index ae50999..6fc9636 100644 --- a/pom.xml +++ b/pom.xml @@ -47,7 +47,8 @@ <scm> <url>https://git-wip-us.apache.org/repos/asf/incubator-commonsrdf.git</url> <connection>scm:git:https://git-wip-us.apache.org/repos/asf/incubator-commonsrdf.git</connection> - <developerConnection>scm:git:https://git-wip-us.apache.org/repos/asf/incubator-commonsrdf.git</developerConnection> + <developerConnection>scm:git:https://git-wip-us.apache.org/repos/asf/incubator-commonsrdf.git + </developerConnection> <tag>HEAD</tag> </scm> @@ -118,8 +119,8 @@ </developers> <modules> - <module>api</module> - <module>simple</module> + <module>api</module> + <module>simple</module> </modules> <dependencyManagement> @@ -163,42 +164,42 @@ </executions> </plugin> <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-jar-plugin</artifactId> - <version>2.6</version> - <configuration> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>2.6</version> <configuration> - <skip>true</skip> + <configuration> + <skip>true</skip> + </configuration> </configuration> - </configuration> </plugin> <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-source-plugin</artifactId> - <version>2.2.1</version> - <executions> - <execution> - <goals> - <goal>jar</goal> - <goal>test-jar</goal> - </goals> - </execution> - </executions> - <configuration> - <skipIfEmpty>true</skipIfEmpty> - </configuration> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-source-plugin</artifactId> + <version>2.2.1</version> + <executions> + <execution> + <goals> + <goal>jar</goal> + <goal>test-jar</goal> + </goals> + </execution> + </executions> + <configuration> + <skipIfEmpty>true</skipIfEmpty> + </configuration> </plugin> <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-javadoc-plugin</artifactId> - <version>2.9.1</version> - <executions> - <execution> - <goals> - <goal>jar</goal> - </goals> - </execution> - </executions> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.9.1</version> + <executions> + <execution> + <goals> + <goal>jar</goal> + </goals> + </execution> + </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/simple/pom.xml ---------------------------------------------------------------------- diff --git a/simple/pom.xml b/simple/pom.xml index af1a930..89880f0 100644 --- a/simple/pom.xml +++ b/simple/pom.xml @@ -18,39 +18,39 @@ --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <parent> + <parent> <groupId>org.apache.commons</groupId> <artifactId>commons-rdf-parent</artifactId> - <version>0.0.3-SNAPSHOT</version> - </parent> - - <artifactId>commons-rdf-simple</artifactId> - <packaging>jar</packaging> - - <name>Commons RDF: Simple Implementation</name> - <description>Simple (if not naive) implementation of Commons RDF API</description> - - <dependencies> - <dependency> - <groupId>${project.parent.groupId}</groupId> - <artifactId>commons-rdf-api</artifactId> - <version>${project.parent.version}</version> - </dependency> - <dependency> - <groupId>${project.parent.groupId}</groupId> - <artifactId>commons-rdf-api</artifactId> - <version>${project.parent.version}</version> - <classifier>tests</classifier> - <scope>test</scope> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - </dependencies> + <version>0.0.3-SNAPSHOT</version> + </parent> + + <artifactId>commons-rdf-simple</artifactId> + <packaging>jar</packaging> + + <name>Commons RDF: Simple Implementation</name> + <description>Simple (if not naive) implementation of Commons RDF API</description> + + <dependencies> + <dependency> + <groupId>${project.parent.groupId}</groupId> + <artifactId>commons-rdf-api</artifactId> + <version>${project.parent.version}</version> + </dependency> + <dependency> + <groupId>${project.parent.groupId}</groupId> + <artifactId>commons-rdf-api</artifactId> + <version>${project.parent.version}</version> + <classifier>tests</classifier> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java index e54a7d9..fb3a871 100644 --- a/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java +++ b/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java @@ -17,84 +17,84 @@ */ package org.apache.commons.rdf.simple; +import org.apache.commons.rdf.api.BlankNode; + import java.nio.charset.StandardCharsets; import java.util.Objects; import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; -import org.apache.commons.rdf.api.BlankNode; - /** * A simple implementation of BlankNode. */ final class BlankNodeImpl implements BlankNode { - private static AtomicLong bnodeCounter = new AtomicLong(); + private static AtomicLong bnodeCounter = new AtomicLong(); - private static final Object DEFAULT_SALT = new Object(); + private static final Object DEFAULT_SALT = new Object(); - private final String id; + private final String id; - public BlankNodeImpl() { - this(DEFAULT_SALT, "genid:" + bnodeCounter.incrementAndGet()); - } + public BlankNodeImpl() { + this(DEFAULT_SALT, "genid:" + bnodeCounter.incrementAndGet()); + } - public BlankNodeImpl(Object uuidSalt, String id) { - if (Objects.requireNonNull(id).isEmpty()) { - throw new IllegalArgumentException("Invalid blank node id: " + id); - } - String uuidInput = uuidSalt.toString() + ":" + id; - // Both the scope and the id are used to create the UUID, ensuring that - // a caller can reliably create the same bnode if necessary by sending - // in the same scope. - // In addition, it would be very difficult for the default constructor - // to interfere with this process since it uses a local object as its - // reference. - this.id = UUID.nameUUIDFromBytes( - uuidInput.getBytes(StandardCharsets.UTF_8)).toString(); - } + public BlankNodeImpl(Object uuidSalt, String id) { + if (Objects.requireNonNull(id).isEmpty()) { + throw new IllegalArgumentException("Invalid blank node id: " + id); + } + String uuidInput = uuidSalt.toString() + ":" + id; + // Both the scope and the id are used to create the UUID, ensuring that + // a caller can reliably create the same bnode if necessary by sending + // in the same scope. + // In addition, it would be very difficult for the default constructor + // to interfere with this process since it uses a local object as its + // reference. + this.id = UUID.nameUUIDFromBytes( + uuidInput.getBytes(StandardCharsets.UTF_8)).toString(); + } - @Override - public String internalIdentifier() { - return id; - } + @Override + public String internalIdentifier() { + return id; + } - @Override - public String ntriplesString() { - return "_:" + id; - } + @Override + public String ntriplesString() { + return "_:" + id; + } - @Override - public String toString() { - return ntriplesString(); - } + @Override + public String toString() { + return ntriplesString(); + } - @Override - public int hashCode() { - return id.hashCode(); - } + @Override + public int hashCode() { + return id.hashCode(); + } - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - // We don't support equality with other implementations - if (!(obj instanceof BlankNodeImpl)) { - return false; - } - BlankNodeImpl other = (BlankNodeImpl) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - return true; - } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + // We don't support equality with other implementations + if (!(obj instanceof BlankNodeImpl)) { + return false; + } + BlankNodeImpl other = (BlankNodeImpl) obj; + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + return true; + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java index 50083d1..d90b228 100644 --- a/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java +++ b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java @@ -17,6 +17,8 @@ */ package org.apache.commons.rdf.simple; +import org.apache.commons.rdf.api.*; + import java.util.HashSet; import java.util.Objects; import java.util.Set; @@ -24,163 +26,155 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.apache.commons.rdf.api.BlankNode; -import org.apache.commons.rdf.api.BlankNodeOrIRI; -import org.apache.commons.rdf.api.Graph; -import org.apache.commons.rdf.api.IRI; -import org.apache.commons.rdf.api.Literal; -import org.apache.commons.rdf.api.RDFTerm; -import org.apache.commons.rdf.api.Triple; - /** * A simple, memory-based implementation of Graph. - * <p> + * <p/> * {@link Triple}s in the graph are kept in a {@link Set}. - * <p> + * <p/> * All Stream operations are performed using parallel and unordered directives. */ final class GraphImpl implements Graph { - private static final int TO_STRING_MAX = 10; - private final Set<Triple> triples = new HashSet<Triple>(); - private final SimpleRDFTermFactory factory; - - GraphImpl(SimpleRDFTermFactory simpleRDFTermFactory) { - this.factory = simpleRDFTermFactory; - } - - @Override - public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { - BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject); - IRI newPredicate = (IRI) internallyMap(predicate); - RDFTerm newObject = internallyMap(object); - Triple result = factory.createTriple(newSubject, newPredicate, newObject); - triples.add(result); - } - - @Override - public void add(Triple triple) { - BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(triple - .getSubject()); - IRI newPredicate = (IRI) internallyMap(triple.getPredicate()); - RDFTerm newObject = internallyMap(triple.getObject()); - // Check if any of the object references changed during the mapping, to - // avoid creating a new Triple object if possible - if (newSubject == triple.getSubject() - && newPredicate == triple.getPredicate() - && newObject == triple.getObject()) { - triples.add(triple); - } else { - Triple result = factory.createTriple(newSubject, newPredicate, - newObject); - triples.add(result); - } - } - - private <T extends RDFTerm> RDFTerm internallyMap(T object) { - if (object instanceof BlankNode && !(object instanceof BlankNodeImpl)) { - 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.internalIdentifier()); - } else if (object instanceof IRI && !(object instanceof IRIImpl)) { - IRI iri = (IRI) object; - return factory.createIRI(iri.getIRIString()); - } else if (object instanceof Literal - && !(object instanceof LiteralImpl)) { - Literal literal = (Literal) object; - if (literal.getLanguageTag().isPresent()) { - return factory.createLiteral(literal.getLexicalForm(), literal - .getLanguageTag().get()); - } else { - return factory.createLiteral(literal.getLexicalForm(), - (IRI) internallyMap(literal.getDatatype())); - } - } else { - // The object is a local implementation, and is not a BlankNode, so - // can be returned directly - return object; - } - } - - @Override - public void clear() { - triples.clear(); - } - - @Override - public boolean contains(BlankNodeOrIRI subject, IRI predicate, - RDFTerm object) { - return getTriples(subject, predicate, object).findFirst().isPresent(); - } - - @Override - public boolean contains(Triple triple) { - return triples.contains(Objects.requireNonNull(triple)); - } - - @Override - public Stream<Triple> getTriples() { - return triples.parallelStream().unordered(); - } - - @Override - public Stream<Triple> getTriples(final BlankNodeOrIRI subject, - final IRI predicate, final RDFTerm object) { - final BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject); - final IRI newPredicate = (IRI) internallyMap(predicate); - final RDFTerm newObject = internallyMap(object); - - return getTriples(t -> { - // Lacking the requirement for .equals() we have to be silly - // and test ntriples string equivalance - if (subject != null && !t.getSubject().equals(newSubject)) { - return false; - } - if (predicate != null && !t.getPredicate().equals(newPredicate)) { - return false; - } - if (object != null && !t.getObject().equals(newObject)) { - return false; - } - return true; - }); - } - - private Stream<Triple> getTriples(final Predicate<Triple> filter) { - return getTriples().filter(filter); - } - - @Override - public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { - Stream<Triple> toRemove = getTriples(subject, predicate, object); - for (Triple t : toRemove.collect(Collectors.toList())) { - // Avoid ConcurrentModificationException in ArrayList - remove(t); - } - } - - @Override - public void remove(Triple triple) { - triples.remove(Objects.requireNonNull(triple)); - } - - @Override - public long size() { - return triples.size(); - } - - @Override - public String toString() { - String s = getTriples().limit(TO_STRING_MAX).map(Object::toString) - .collect(Collectors.joining("\n")); - if (size() > TO_STRING_MAX) { - return s + "\n# ... +" + (size() - TO_STRING_MAX) + " more"; - } else { - return s; - } - } + private static final int TO_STRING_MAX = 10; + private final Set<Triple> triples = new HashSet<Triple>(); + private final SimpleRDFTermFactory factory; + + GraphImpl(SimpleRDFTermFactory simpleRDFTermFactory) { + this.factory = simpleRDFTermFactory; + } + + @Override + public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject); + IRI newPredicate = (IRI) internallyMap(predicate); + RDFTerm newObject = internallyMap(object); + Triple result = factory.createTriple(newSubject, newPredicate, newObject); + triples.add(result); + } + + @Override + public void add(Triple triple) { + BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(triple + .getSubject()); + IRI newPredicate = (IRI) internallyMap(triple.getPredicate()); + RDFTerm newObject = internallyMap(triple.getObject()); + // Check if any of the object references changed during the mapping, to + // avoid creating a new Triple object if possible + if (newSubject == triple.getSubject() + && newPredicate == triple.getPredicate() + && newObject == triple.getObject()) { + triples.add(triple); + } else { + Triple result = factory.createTriple(newSubject, newPredicate, + newObject); + triples.add(result); + } + } + + private <T extends RDFTerm> RDFTerm internallyMap(T object) { + if (object instanceof BlankNode && !(object instanceof BlankNodeImpl)) { + 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.internalIdentifier()); + } else if (object instanceof IRI && !(object instanceof IRIImpl)) { + IRI iri = (IRI) object; + return factory.createIRI(iri.getIRIString()); + } else if (object instanceof Literal + && !(object instanceof LiteralImpl)) { + Literal literal = (Literal) object; + if (literal.getLanguageTag().isPresent()) { + return factory.createLiteral(literal.getLexicalForm(), literal + .getLanguageTag().get()); + } else { + return factory.createLiteral(literal.getLexicalForm(), + (IRI) internallyMap(literal.getDatatype())); + } + } else { + // The object is a local implementation, and is not a BlankNode, so + // can be returned directly + return object; + } + } + + @Override + public void clear() { + triples.clear(); + } + + @Override + public boolean contains(BlankNodeOrIRI subject, IRI predicate, + RDFTerm object) { + return getTriples(subject, predicate, object).findFirst().isPresent(); + } + + @Override + public boolean contains(Triple triple) { + return triples.contains(Objects.requireNonNull(triple)); + } + + @Override + public Stream<Triple> getTriples() { + return triples.parallelStream().unordered(); + } + + @Override + public Stream<Triple> getTriples(final BlankNodeOrIRI subject, + final IRI predicate, final RDFTerm object) { + final BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject); + final IRI newPredicate = (IRI) internallyMap(predicate); + final RDFTerm newObject = internallyMap(object); + + return getTriples(t -> { + // Lacking the requirement for .equals() we have to be silly + // and test ntriples string equivalance + if (subject != null && !t.getSubject().equals(newSubject)) { + return false; + } + if (predicate != null && !t.getPredicate().equals(newPredicate)) { + return false; + } + if (object != null && !t.getObject().equals(newObject)) { + return false; + } + return true; + }); + } + + private Stream<Triple> getTriples(final Predicate<Triple> filter) { + return getTriples().filter(filter); + } + + @Override + public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + Stream<Triple> toRemove = getTriples(subject, predicate, object); + for (Triple t : toRemove.collect(Collectors.toList())) { + // Avoid ConcurrentModificationException in ArrayList + remove(t); + } + } + + @Override + public void remove(Triple triple) { + triples.remove(Objects.requireNonNull(triple)); + } + + @Override + public long size() { + return triples.size(); + } + + @Override + public String toString() { + String s = getTriples().limit(TO_STRING_MAX).map(Object::toString) + .collect(Collectors.joining("\n")); + if (size() > TO_STRING_MAX) { + return s + "\n# ... +" + (size() - TO_STRING_MAX) + " more"; + } else { + return s; + } + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java index a735737..0f6b065 100644 --- a/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java +++ b/simple/src/main/java/org/apache/commons/rdf/simple/IRIImpl.java @@ -17,56 +17,55 @@ */ package org.apache.commons.rdf.simple; -import java.net.URI; - import org.apache.commons.rdf.api.IRI; +import java.net.URI; + /** * A simple implementation of IRI. - * */ final class IRIImpl implements IRI { - private final String iri; + private final String iri; - public IRIImpl(String iri) { - // should throw IllegalArgumentException on most illegal IRIs - URI.create(iri); - // NOTE: We don't keep the URI as it uses outdated RFC2396 and will get - // some IDNs wrong - this.iri = iri; - } + public IRIImpl(String iri) { + // should throw IllegalArgumentException on most illegal IRIs + URI.create(iri); + // NOTE: We don't keep the URI as it uses outdated RFC2396 and will get + // some IDNs wrong + this.iri = iri; + } - @Override - public String getIRIString() { - return iri; - } + @Override + public String getIRIString() { + return iri; + } - @Override - public String ntriplesString() { - return "<" + getIRIString() + ">"; - } + @Override + public String ntriplesString() { + return "<" + getIRIString() + ">"; + } - @Override - public String toString() { - return ntriplesString(); - } + @Override + public String toString() { + return ntriplesString(); + } - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || !(obj instanceof IRI)) { - return false; - } - IRI other = (IRI) obj; - return getIRIString().equals(other.getIRIString()); - } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || !(obj instanceof IRI)) { + return false; + } + IRI other = (IRI) obj; + return getIRIString().equals(other.getIRIString()); + } - @Override - public int hashCode() { - return iri.hashCode(); - } + @Override + public int hashCode() { + return iri.hashCode(); + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java index 99f3b0d..aafc41d 100644 --- a/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java +++ b/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java @@ -17,122 +17,121 @@ */ package org.apache.commons.rdf.simple; +import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.Literal; + import java.util.IllformedLocaleException; import java.util.Locale; import java.util.Objects; import java.util.Optional; -import org.apache.commons.rdf.api.IRI; -import org.apache.commons.rdf.api.Literal; - /** * A simple implementation of Literal. - * */ final class LiteralImpl implements Literal { - private static final String QUOTE = "\""; - - private final IRI dataType; - private final String languageTag; - private final String lexicalForm; - - public LiteralImpl(String literal) { - this(literal, Types.XSD_STRING); - } - - public LiteralImpl(String lexicalForm, IRI dataType) { - this.lexicalForm = Objects.requireNonNull(lexicalForm); - this.dataType = Types.get(Objects.requireNonNull(dataType)).orElse( - dataType); - if (Types.RDF_LANGSTRING.equals(this.dataType)) { - throw new IllegalArgumentException( - "Cannot create a non-language literal with type " - + Types.RDF_LANGSTRING); - } - this.languageTag = null; - } - - public LiteralImpl(String literal, String languageTag) { - this.lexicalForm = Objects.requireNonNull(literal); - this.languageTag = Objects.requireNonNull(languageTag).toLowerCase( - Locale.ENGLISH); - if (languageTag.isEmpty()) { - // TODO: Check against - // http://www.w3.org/TR/n-triples/#n-triples-grammar - throw new IllegalArgumentException("Language tag can't be null"); - } - try { - new Locale.Builder().setLanguageTag(languageTag); - } catch (IllformedLocaleException ex) { - throw new IllegalArgumentException("Invalid languageTag: " - + languageTag, ex); - } - - // System.out.println(aLocale); - this.dataType = Types.RDF_LANGSTRING; - } - - @Override - public IRI getDatatype() { - return dataType; - } - - @Override - public Optional<String> getLanguageTag() { - return Optional.ofNullable(languageTag); - } - - @Override - public String getLexicalForm() { - return lexicalForm; - } - - @Override - public String ntriplesString() { - StringBuilder sb = new StringBuilder(); - sb.append(QUOTE); - // Escape special characters - sb.append(getLexicalForm().replace("\\", "\\\\"). // escaped to \\ - replace("\"", "\\\""). // escaped to \" - replace("\r", "\\r"). // escaped to \r - replace("\n", "\\n")); // escaped to \n - sb.append(QUOTE); - - // getLanguageTag().ifPresent(s -> sb.append("@" + s)); - if (getLanguageTag().isPresent()) { - sb.append("@"); - sb.append(getLanguageTag().get()); - - } else if (!getDatatype().equals(Types.XSD_STRING)) { - sb.append("^^"); - sb.append(getDatatype().ntriplesString()); - } - return sb.toString(); - } - - @Override - public String toString() { - return ntriplesString(); - } - - @Override - public int hashCode() { - return Objects.hash(dataType, lexicalForm, languageTag); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || !(obj instanceof Literal)) { - return false; - } - Literal literal = (Literal) obj; - return getDatatype().equals(literal.getDatatype()) - && getLexicalForm().equals(literal.getLexicalForm()) - && getLanguageTag().equals(literal.getLanguageTag()); - } + private static final String QUOTE = "\""; + + private final IRI dataType; + private final String languageTag; + private final String lexicalForm; + + public LiteralImpl(String literal) { + this(literal, Types.XSD_STRING); + } + + public LiteralImpl(String lexicalForm, IRI dataType) { + this.lexicalForm = Objects.requireNonNull(lexicalForm); + this.dataType = Types.get(Objects.requireNonNull(dataType)).orElse( + dataType); + if (Types.RDF_LANGSTRING.equals(this.dataType)) { + throw new IllegalArgumentException( + "Cannot create a non-language literal with type " + + Types.RDF_LANGSTRING); + } + this.languageTag = null; + } + + public LiteralImpl(String literal, String languageTag) { + this.lexicalForm = Objects.requireNonNull(literal); + this.languageTag = Objects.requireNonNull(languageTag).toLowerCase( + Locale.ENGLISH); + if (languageTag.isEmpty()) { + // TODO: Check against + // http://www.w3.org/TR/n-triples/#n-triples-grammar + throw new IllegalArgumentException("Language tag can't be null"); + } + try { + new Locale.Builder().setLanguageTag(languageTag); + } catch (IllformedLocaleException ex) { + throw new IllegalArgumentException("Invalid languageTag: " + + languageTag, ex); + } + + // System.out.println(aLocale); + this.dataType = Types.RDF_LANGSTRING; + } + + @Override + public IRI getDatatype() { + return dataType; + } + + @Override + public Optional<String> getLanguageTag() { + return Optional.ofNullable(languageTag); + } + + @Override + public String getLexicalForm() { + return lexicalForm; + } + + @Override + public String ntriplesString() { + StringBuilder sb = new StringBuilder(); + sb.append(QUOTE); + // Escape special characters + sb.append(getLexicalForm().replace("\\", "\\\\"). // escaped to \\ + replace("\"", "\\\""). // escaped to \" + replace("\r", "\\r"). // escaped to \r + replace("\n", "\\n")); // escaped to \n + sb.append(QUOTE); + + // getLanguageTag().ifPresent(s -> sb.append("@" + s)); + if (getLanguageTag().isPresent()) { + sb.append("@"); + sb.append(getLanguageTag().get()); + + } else if (!getDatatype().equals(Types.XSD_STRING)) { + sb.append("^^"); + sb.append(getDatatype().ntriplesString()); + } + return sb.toString(); + } + + @Override + public String toString() { + return ntriplesString(); + } + + @Override + public int hashCode() { + return Objects.hash(dataType, lexicalForm, languageTag); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || !(obj instanceof Literal)) { + return false; + } + Literal literal = (Literal) obj; + return getDatatype().equals(literal.getDatatype()) + && getLexicalForm().equals(literal.getLexicalForm()) + && getLanguageTag().equals(literal.getLanguageTag()); + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java b/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java index 6305e63..4322054 100644 --- a/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java +++ b/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java @@ -17,67 +17,60 @@ */ package org.apache.commons.rdf.simple; -import org.apache.commons.rdf.api.BlankNode; -import org.apache.commons.rdf.api.BlankNodeOrIRI; -import org.apache.commons.rdf.api.Graph; -import org.apache.commons.rdf.api.IRI; -import org.apache.commons.rdf.api.Literal; -import org.apache.commons.rdf.api.RDFTerm; -import org.apache.commons.rdf.api.RDFTermFactory; -import org.apache.commons.rdf.api.Triple; +import org.apache.commons.rdf.api.*; /** * A simple implementation of RDFTermFactory. - * <p> + * <p/> * The {@link RDFTerm} and {@link Graph} instances created by this factory are * simple in-memory Implementations that are not thread-safe or efficient, but * which may be useful for testing and prototyping purposes. */ public class SimpleRDFTermFactory implements RDFTermFactory { - @Override - public BlankNode createBlankNode() { - return new BlankNodeImpl(); - } + @Override + public BlankNode createBlankNode() { + return new BlankNodeImpl(); + } - @Override - public BlankNode createBlankNode(String identifier) { - // Creates a BlankNodeImpl object using this object as the salt - return new BlankNodeImpl(this, identifier); - } + @Override + public BlankNode createBlankNode(String identifier) { + // Creates a BlankNodeImpl object using this object as the salt + return new BlankNodeImpl(this, identifier); + } - @Override - public Graph createGraph() { - // Creates a GraphImpl object using this object as the factory for - // delegating all object creation to - return new GraphImpl(this); - } + @Override + public Graph createGraph() { + // Creates a GraphImpl object using this object as the factory for + // delegating all object creation to + return new GraphImpl(this); + } - @Override - public IRI createIRI(String iri) { - IRI result = new IRIImpl(iri); - // Reuse any IRI objects already created in Types - return Types.get(result).orElse(result); - } + @Override + public IRI createIRI(String iri) { + IRI result = new IRIImpl(iri); + // Reuse any IRI objects already created in Types + return Types.get(result).orElse(result); + } - @Override - public Literal createLiteral(String literal) { - return new LiteralImpl(literal); - } + @Override + public Literal createLiteral(String literal) { + return new LiteralImpl(literal); + } - @Override - public Literal createLiteral(String literal, IRI dataType) { - return new LiteralImpl(literal, dataType); - } + @Override + public Literal createLiteral(String literal, IRI dataType) { + return new LiteralImpl(literal, dataType); + } - @Override - public Literal createLiteral(String literal, String language) { - return new LiteralImpl(literal, language); - } + @Override + public Literal createLiteral(String literal, String language) { + return new LiteralImpl(literal, language); + } - @Override - public Triple createTriple(BlankNodeOrIRI subject, IRI predicate, - RDFTerm object) { - return new TripleImpl(subject, predicate, object); - } + @Override + public Triple createTriple(BlankNodeOrIRI subject, IRI predicate, + RDFTerm object) { + return new TripleImpl(subject, predicate, object); + } } http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/5ee4c293/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java index 0fe76d9..ab9380f 100644 --- a/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java +++ b/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java @@ -17,78 +17,74 @@ */ package org.apache.commons.rdf.simple; -import java.util.Objects; - import org.apache.commons.rdf.api.BlankNodeOrIRI; import org.apache.commons.rdf.api.IRI; import org.apache.commons.rdf.api.RDFTerm; import org.apache.commons.rdf.api.Triple; +import java.util.Objects; + /** * A simple implementation of Triple. - * */ final class TripleImpl implements Triple { - private final BlankNodeOrIRI subject; - private final IRI predicate; - private final RDFTerm object; + private final BlankNodeOrIRI subject; + private final IRI predicate; + private final RDFTerm object; - /** - * Construct Triple from its constituent parts. - * <p> - * The objects are not changed. All mapping of BNode objects is done in - * {@link SimpleRDFTermFactory#createTriple(BlankNodeOrIRI, IRI, RDFTerm)}. - * - * @param subject - * subject of triple - * @param predicate - * predicate of triple - * @param object - * object of triple - */ - public TripleImpl(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { - this.subject = Objects.requireNonNull(subject); - this.predicate = Objects.requireNonNull(predicate); - this.object = Objects.requireNonNull(object); - } + /** + * Construct Triple from its constituent parts. + * <p/> + * The objects are not changed. All mapping of BNode objects is done in + * {@link SimpleRDFTermFactory#createTriple(BlankNodeOrIRI, IRI, RDFTerm)}. + * + * @param subject subject of triple + * @param predicate predicate of triple + * @param object object of triple + */ + public TripleImpl(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + this.subject = Objects.requireNonNull(subject); + this.predicate = Objects.requireNonNull(predicate); + this.object = Objects.requireNonNull(object); + } - @Override - public BlankNodeOrIRI getSubject() { - return subject; - } + @Override + public BlankNodeOrIRI getSubject() { + return subject; + } - @Override - public IRI getPredicate() { - return predicate; - } + @Override + public IRI getPredicate() { + return predicate; + } - @Override - public RDFTerm getObject() { - return object; - } + @Override + public RDFTerm getObject() { + return object; + } - @Override - public String toString() { - return getSubject().ntriplesString() + " " - + getPredicate().ntriplesString() + " " - + getObject().ntriplesString() + " ."; - } + @Override + public String toString() { + return getSubject().ntriplesString() + " " + + getPredicate().ntriplesString() + " " + + getObject().ntriplesString() + " ."; + } - @Override - public int hashCode() { - return Objects.hash(subject, predicate, object); - } + @Override + public int hashCode() { + return Objects.hash(subject, predicate, object); + } - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Triple)) { - return false; - } - Triple other = (Triple) obj; - return getSubject().equals(other.getSubject()) - && getPredicate().equals(other.getPredicate()) - && getObject().equals(other.getObject()); - } + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Triple)) { + return false; + } + Triple other = (Triple) obj; + return getSubject().equals(other.getSubject()) + && getPredicate().equals(other.getPredicate()) + && getObject().equals(other.getObject()); + } }