Skip to content

Package: SortingRDFHandler

SortingRDFHandler

nameinstructionbranchcomplexitylinemethod
SortingRDFHandler(RDFHandler)
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
endRDF()
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
handleComment(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handleNamespace(String, String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
handleStatement(Statement)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$new$0(String, String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$startRDF$1(Pair)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
startRDF()
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.util;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Comparator;
32: import java.util.List;
33: import java.util.function.BiConsumer;
34: import org.eclipse.rdf4j.model.Statement;
35: import org.eclipse.rdf4j.model.Value;
36: import org.eclipse.rdf4j.model.vocabulary.RDF;
37: import org.eclipse.rdf4j.rio.RDFHandler;
38: import org.eclipse.rdf4j.rio.RDFHandlerException;
39: import it.tidalwave.util.Pair;
40: import lombok.RequiredArgsConstructor;
41: import lombok.experimental.Delegate;
42: import static java.util.Comparator.*;
43:
44: /***********************************************************************************************************************
45: *
46: * @author Fabrizio Giudici
47: *
48: **********************************************************************************************************************/
49: @RequiredArgsConstructor
50: public class SortingRDFHandler implements RDFHandler
51: {
52: static class ValueComparator implements Comparator<Value>
53: {
54: @Override
55: public int compare (final Value o1, final Value o2)
56: {
57: if (o1.stringValue().equals(RDF.TYPE.stringValue()))
58: {
59: return -1;
60: }
61: else if (o2.stringValue().equals(RDF.TYPE.stringValue()))
62: {
63: return +1;
64: }
65:
66: return o1.stringValue().compareTo(o2.stringValue());
67: }
68: }
69:
70: interface Exclusions
71: {
72: public void startRDF()
73: throws RDFHandlerException;
74:
75: public void handleNamespace (String prefix, String uri)
76: throws RDFHandlerException;
77:
78: public void handleStatement (Statement statement)
79: throws RDFHandlerException;
80:
81: public void endRDF()
82: throws RDFHandlerException;
83: }
84:
85: private static final ValueComparator VALUE_COMPARATOR = new ValueComparator();
86:
87: @Nonnull @Delegate(excludes = Exclusions.class)
88: private final RDFHandler delegate;
89:
90: private final List<Statement> statements = new ArrayList<>();
91:
92: private final List<Pair<String, String>> nameSpaces = new ArrayList<>();
93:
94: private BiConsumer<String, String> namespaceHandler = (prefix, uri) -> nameSpaces.add(Pair.of(prefix, uri));
95:
96: @Override
97: public void handleNamespace (@Nonnull final String prefix, @Nonnull final String uri)
98: {
99: namespaceHandler.accept(prefix, uri);
100: }
101:
102: @Override
103: public void handleStatement (@Nonnull final Statement statement)
104: {
105: statements.add(statement);
106: }
107:
108: @Override
109: public void startRDF()
110: throws RDFHandlerException
111: {
112: delegate.startRDF();
113: namespaceHandler = delegate::handleNamespace;
114: nameSpaces.forEach(p -> delegate.handleNamespace(p.a, p.b));
115: }
116:
117: @Override
118: public void endRDF()
119: throws RDFHandlerException
120: {
121: statements.stream()
122: .sorted(comparing(Statement::getSubject, VALUE_COMPARATOR)
123: .thenComparing(Statement::getPredicate, VALUE_COMPARATOR)
124: .thenComparing(Statement::getObject, VALUE_COMPARATOR))
125: .forEach(delegate::handleStatement);
126: delegate.endRDF();
127: }
128: }