Package: SortingRDFHandler
SortingRDFHandler
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SortingRDFHandler(RDFHandler) |
|
|
|
|
|
||||||||||||||||||||
endRDF() |
|
|
|
|
|
||||||||||||||||||||
handleComment(String) |
|
|
|
|
|
||||||||||||||||||||
handleNamespace(String, String) |
|
|
|
|
|
||||||||||||||||||||
handleStatement(Statement) |
|
|
|
|
|
||||||||||||||||||||
lambda$endRDF$0(Statement, Statement) |
|
|
|
|
|
||||||||||||||||||||
startRDF() |
|
|
|
|
|
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.List;
32: import org.eclipse.rdf4j.model.Statement;
33: import org.eclipse.rdf4j.model.vocabulary.RDF;
34: import org.eclipse.rdf4j.rio.RDFHandler;
35: import org.eclipse.rdf4j.rio.RDFHandlerException;
36: import lombok.RequiredArgsConstructor;
37: import lombok.experimental.Delegate;
38:
39: /***********************************************************************************************************************
40: *
41: * @author Fabrizio Giudici
42: *
43: **********************************************************************************************************************/
44: @RequiredArgsConstructor
45: public class SortingRDFHandler implements RDFHandler
46: {
47: interface Exclusions
48: {
49: public void handleStatement (Statement statement)
50: throws RDFHandlerException;
51:
52: public void endRDF()
53: throws RDFHandlerException;
54: }
55:
56: @Nonnull @Delegate(excludes = Exclusions.class)
57: private final RDFHandler delegate;
58:
59: private final List<Statement> statements = new ArrayList<>();
60:
61: @Override
62: public void handleStatement (@Nonnull final Statement statement)
63: throws RDFHandlerException
64: {
65: statements.add(statement);
66: }
67:
68: @Override
69: public void endRDF()
70: throws RDFHandlerException
71: {
72: statements.sort((st1, st2) ->
73: {
74: final String s1 = st1.getSubject().stringValue();
75: final String s2 = st2.getSubject().stringValue();
76: final int c1 = s1.compareTo(s2);
77:
78:• if (c1 != 0)
79: {
80: return c1;
81: }
82:
83: final String p1 = st1.getPredicate().stringValue();
84: final String p2 = st2.getPredicate().stringValue();
85: final int c2 = p1.compareTo(p2);
86:
87:• if (c2 != 0)
88: {
89:• if (p1.equals(RDF.TYPE.stringValue()))
90: {
91: return -1;
92: }
93:• else if (p2.equals(RDF.TYPE.stringValue()))
94: {
95: return +1;
96: }
97:
98: return c2;
99: }
100:
101: final String o1 = st1.getObject().stringValue();
102: final String o2 = st2.getObject().stringValue();
103:
104: return o1.compareTo(o2);
105: });
106:
107:• for (final Statement statement : statements)
108: {
109: delegate.handleStatement(statement);
110: }
111:
112: delegate.endRDF();
113: }
114: }