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.stream.Stream;
22  
23  class DummyGraph implements Graph {
24  
25      boolean streamCalled = false;
26      boolean filteredStreamCalled;
27  
28      @Override
29      public void add(final Triple triple) {
30          if (! contains(triple)) {
31              throw new IllegalStateException("DummyGraph can't be modified");
32          }
33      }
34      @Override
35      public void add(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
36          if (! contains(subject, predicate, object)) {
37              throw new IllegalStateException("DummyGraph can't be modified");
38          }
39      }
40      @Override
41      public boolean contains(final Triple triple) {
42          return triple.equals(new DummyTriple());
43      }
44      @Override
45      public boolean contains(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
46          return (subject == null || subject.equals(new DummyIRI(1))) &&
47                  (predicate == null || predicate.equals(new DummyIRI(2))) &&
48                  (object == null || object.equals(new DummyIRI(3)));
49      }
50      @Override
51      public void remove(final Triple triple) {
52          if (contains(triple)) {
53              throw new IllegalStateException("DummyGraph can't be modified");
54          }
55      }
56      @Override
57      public void remove(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
58          if (contains(subject, predicate, object)) {
59              throw new IllegalStateException("DummyGraph can't be modified");
60          }
61      }
62      @Override
63      public void clear() {
64          throw new IllegalStateException("DummyGraph can't be modified");
65      }
66      @Override
67      public long size() {
68          return 1;
69      }
70      @Override
71      public Stream<? extends Triple> stream() {
72          streamCalled = true;
73          return Arrays.asList(new DummyTriple()).stream();
74      }
75  
76      @Override
77      public Stream<? extends Triple> stream(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
78          filteredStreamCalled = true;
79          if (contains(subject, predicate, object)) {
80              return Stream.of(new DummyTriple());
81          }
82          return Stream.empty();
83      }
84  }