Debian Codegraph @

wdtk-rdf/src/main/java/org/wikidata/wdtk/rdf/values/BufferedValueConverter.java

raw
1 package org.wikidata.wdtk.rdf.values;
2
3 /*
4 * #%L
5 * Wikidata Toolkit RDF
6 * %%
7 * Copyright (C) 2014 Wikidata Toolkit Developers
8 * %%
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * #L%
21 */
22
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.eclipse.rdf4j.model.Resource;
29 import org.eclipse.rdf4j.rio.RDFHandlerException;
30 import org.wikidata.wdtk.rdf.OwlDeclarationBuffer;
31 import org.wikidata.wdtk.rdf.PropertyRegister;
32 import org.wikidata.wdtk.rdf.RdfWriter;
33
34 public abstract class BufferedValueConverter<V extends org.wikidata.wdtk.datamodel.interfaces.Value>
35 extends AbstractValueConverter<V> {
36
37 final List<V> valueQueue = new ArrayList<>();
38 final List<Resource> valueSubjectQueue = new ArrayList<>();
39 final HashSet<Resource> declaredValues = new HashSet<>();
40
41 public BufferedValueConverter(RdfWriter rdfWriter,
42 PropertyRegister propertyRegister,
43 OwlDeclarationBuffer rdfConversionBuffer) {
44 super(rdfWriter, propertyRegister, rdfConversionBuffer);
45 }
46
47 /**
48 * Adds the given value to the list of values that should still be
49 * serialized. The given RDF resource will be used as a subject.
50 *
51 * @param value
52 * the value to be serialized
53 * @param resource
54 * the RDF resource that is used as a subject for serialization
55 */
56 void addValue(V value, Resource resource) {
57 this.valueQueue.add(value);
58 this.valueSubjectQueue.add(resource);
59 }
60
61 @Override
62 public void writeAuxiliaryTriples() throws RDFHandlerException {
63 Iterator<V> valueIterator = this.valueQueue.iterator();
64 for (Resource resource : this.valueSubjectQueue) {
65 if (!this.declaredValues.add(resource)) {
66 valueIterator.next();
67 continue;
68 }
69 writeValue(valueIterator.next(), resource);
70 }
71 this.valueSubjectQueue.clear();
72 this.valueQueue.clear();
73 }
74
75 /**
76 * Writes the triples for a single value, using the given resource as
77 * subject.
78 *
79 * @param value
80 * @param resource
81 * @throws RDFHandlerException
82 */
83 public abstract void writeValue(V value, Resource resource)
84 throws RDFHandlerException;
85
86 }