View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.rdf.simple;
19  
20  import java.util.UUID;
21  
22  import org.apache.commons.rdf.api.BlankNode;
23  import org.apache.commons.rdf.api.BlankNodeOrIRI;
24  import org.apache.commons.rdf.api.Dataset;
25  import org.apache.commons.rdf.api.Graph;
26  import org.apache.commons.rdf.api.IRI;
27  import org.apache.commons.rdf.api.Literal;
28  import org.apache.commons.rdf.api.Quad;
29  import org.apache.commons.rdf.api.RDFTerm;
30  import org.apache.commons.rdf.api.RDF;
31  import org.apache.commons.rdf.api.Triple;
32  
33  /**
34   * Simple RDF implementation.
35   * <p>
36   * The {@link RDFTerm}, {@link Triple}, {@link Quad}, {@link Graph} and
37   * {@link Dataset} instances created by SimpleRDF are simple in-memory
38   * Implementations that are not thread-safe or efficient, but which may be
39   * useful for testing and prototyping purposes.
40   */
41  public class SimpleRDF implements RDF {
42  
43      /**
44       * Marker interface to say that this RDFTerm is part of the Simple
45       * implementation. Used by {@link GraphImpl} to avoid double remapping.
46       * <p>
47       * This method is package protected to avoid any third-party subclasses.
48       *
49       */
50      static interface SimpleRDFTerm extends RDFTerm {
51      }
52  
53      /**
54       * Unique salt per instance, for {@link #createBlankNode(String)}
55       */
56      private final UUID SALT = UUID.randomUUID();
57  
58      @Override
59      public BlankNode createBlankNode() {
60          return new BlankNodeImpl();
61      }
62  
63      @Override
64      public BlankNode createBlankNode(final String name) {
65          return new BlankNodeImpl(SALT, name);
66      }
67  
68      @Override
69      public Graph createGraph() {
70          // Creates a GraphImpl object using this object as the factory for
71          // delegating all object creation to
72          return new GraphImpl(this);
73      }
74  
75      @Override
76      public Dataset createDataset() throws UnsupportedOperationException {
77          return new DatasetImpl(this);
78      }
79  
80      @Override
81      public IRI createIRI(final String iri) {
82          final IRI result = new IRIImpl(iri);
83          // Reuse any IRI objects already created in Types
84          return Types.get(result).orElse(result);
85      }
86  
87      @Override
88      public Literal createLiteral(final String literal) {
89          return new LiteralImpl(literal);
90      }
91  
92      @Override
93      public Literal createLiteral(final String literal, final IRI dataType) {
94          return new LiteralImpl(literal, dataType);
95      }
96  
97      @Override
98      public Literal createLiteral(final String literal, final String language) {
99          return new LiteralImpl(literal, language);
100     }
101 
102     @Override
103     public Triple createTriple(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
104         return new TripleImpl(subject, predicate, object);
105     }
106 
107     @Override
108     public Quad createQuad(final BlankNodeOrIRI graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object)
109             throws IllegalArgumentException {
110         return new QuadImpl(graphName, subject, predicate, object);
111     }
112 }