SortingRDFHandler.java

  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. import javax.annotation.Nonnull;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import lombok.experimental.Delegate;
  32. import lombok.RequiredArgsConstructor;
  33. import org.eclipse.rdf4j.model.Statement;
  34. import org.eclipse.rdf4j.model.vocabulary.RDF;
  35. import org.eclipse.rdf4j.rio.RDFHandler;
  36. import org.eclipse.rdf4j.rio.RDFHandlerException;

  37. /***********************************************************************************************************************
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **********************************************************************************************************************/
  42. @RequiredArgsConstructor
  43. public class SortingRDFHandler implements RDFHandler
  44.   {
  45.     interface Exclusions
  46.       {
  47.         public void handleStatement (Statement statement)
  48.           throws RDFHandlerException;

  49.         public void endRDF()
  50.           throws RDFHandlerException;
  51.       }

  52.     @Nonnull @Delegate(excludes = Exclusions.class)
  53.     private final RDFHandler delegate;

  54.     private final List<Statement> statements = new ArrayList<>();

  55.     @Override
  56.     public void handleStatement (@Nonnull final Statement statement)
  57.       throws RDFHandlerException
  58.       {
  59.         statements.add(statement);
  60.       }

  61.     @Override
  62.     public void endRDF()
  63.       throws RDFHandlerException
  64.       {
  65.         statements.sort((st1, st2) ->
  66.           {
  67.             final String s1 = st1.getSubject().stringValue();
  68.             final String s2 = st2.getSubject().stringValue();
  69.             final int c1 = s1.compareTo(s2);

  70.             if (c1 != 0)
  71.               {
  72.                 return c1;
  73.               }

  74.             final String p1 = st1.getPredicate().stringValue();
  75.             final String p2 = st2.getPredicate().stringValue();
  76.             final int c2 = p1.compareTo(p2);

  77.             if (c2 != 0)
  78.               {
  79.                 if (p1.equals(RDF.TYPE.stringValue()))
  80.                   {
  81.                     return -1;
  82.                   }
  83.                 else if (p2.equals(RDF.TYPE.stringValue()))
  84.                   {
  85.                     return +1;
  86.                   }

  87.                 return c2;
  88.               }

  89.             final String o1 = st1.getObject().stringValue();
  90.             final String o2 = st2.getObject().stringValue();

  91.             return o1.compareTo(o2);
  92.           });

  93.         for (final Statement statement : statements)
  94.           {
  95.             delegate.handleStatement(statement);
  96.           }

  97.         delegate.endRDF();
  98.       }
  99.   }