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.api;
19  
20  import java.util.Arrays;
21  import java.util.Optional;
22  import java.util.stream.Stream;
23  
24  class DummyDataset implements Dataset {
25      
26      boolean streamCalled = false;
27      boolean filteredStreamCalled;
28      
29      @Override
30      public void add(final Quad Quad) {
31          if (! contains(Quad)) {
32              throw new IllegalStateException("DummyDataset can't be modified");
33          }
34      }
35      
36      @Override
37      public void add(final BlankNodeOrIRI graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
38          if (! contains(Optional.ofNullable(graphName), subject, predicate, object)) { 
39              throw new IllegalStateException("DummyDataset can't be modified");
40          }
41      }
42      
43      @Override
44      public boolean contains(final Quad Quad) {
45          return Quad.equals(new DummyQuad());
46      }
47      
48      @Override
49      public boolean contains(final Optional<BlankNodeOrIRI> graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {        
50          return (graphName == null || ! graphName.isPresent()) &&
51                  (subject == null || subject.equals(new DummyIRI(1))) && 
52                  (predicate == null || predicate.equals(new DummyIRI(2))) && 
53                  (object == null || object.equals(new DummyIRI(3)));
54      }
55      @Override
56      public void remove(final Quad Quad) {
57          if (contains(Quad)) {
58              throw new IllegalStateException("DummyDataset can't be modified");
59          }
60      }
61      @Override
62      public void remove(final Optional<BlankNodeOrIRI> graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
63          if (contains(graphName, subject, predicate, object)) {
64              throw new IllegalStateException("DummyDataset can't be modified");
65          }
66      }
67      @Override
68      public void clear() {
69          throw new IllegalStateException("DummyDataset can't be modified");
70      }
71      @Override
72      public long size() {
73          return 1;
74      }
75      @Override
76      public Stream<? extends Quad> stream() {
77          streamCalled = true;
78          return Arrays.asList(new DummyQuad()).stream();
79      }
80  
81      @Override
82      public Stream<? extends Quad> stream(final Optional<BlankNodeOrIRI> graphName, final BlankNodeOrIRI subject, final IRI predicate,
83              final RDFTerm object) {
84          filteredStreamCalled = true;
85          if (contains(graphName, subject, predicate, object)) { 
86              return Stream.of(new DummyQuad());
87          } else {
88              return Stream.empty();
89          }
90      }
91  
92      @Override
93      public Graph getGraph() {
94          return new DummyGraph();
95      }
96  
97      @Override
98      public Optional<Graph> getGraph(final BlankNodeOrIRI graphName) {
99          if (graphName == null) { 
100             return Optional.of(getGraph());
101         } else {
102             return Optional.empty();
103         }
104     }
105 
106     @Override
107     public Stream<BlankNodeOrIRI> getGraphNames() {
108         return Stream.empty();
109     }
110 }