Content of file ModelBuilder.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>ModelBuilder.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">blueMarine II :: Media Scanner</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-commons</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.util</a> &gt; <span class="el_source">ModelBuilder.java</span></div><h1>ModelBuilder.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * blueMarine II: Semantic Media Centre
 * http://tidalwave.it/projects/bluemarine2
 *
 * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/bluemarine2-src
 * git clone https://github.com/tidalwave-it/bluemarine2-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.bluemarine2.util;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.impl.TreeModel;

/***********************************************************************************************************************
 *
 * Unlike the similar class in RDF4J, this is thread-safe and can merge to similar objects.
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="pc bpc" id="L48" title="1 of 2 branches missed.">@ThreadSafe</span>
public class ModelBuilder
  {
<span class="fc" id="L51">    private final Model model = new TreeModel();</span>

    @Nonnull private final Resource[] contexts;

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public ModelBuilder (@Nonnull final Resource ... contexts)
<span class="fc" id="L61">      {</span>
<span class="fc" id="L62">        this.contexts = contexts;</span>
<span class="fc" id="L63">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized Model toModel()
      {
<span class="fc" id="L73">        return new TreeModel(model);</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder with (@Nonnull final Resource subject,
                                           @Nonnull final IRI predicate,
                                           @Nonnull final Value object,
                                           @Nonnull final Resource... contexts)
      {
<span class="fc" id="L87">        model.add(subject, predicate, object, contexts);</span>
<span class="fc" id="L88">        return this;</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder with (@Nonnull final Resource subjext,
                                           @Nonnull final IRI predicate,
                                           @Nonnull final Optional&lt;Value&gt; optionalObject,
                                           @Nonnull final Resource... contexts)
      {
<span class="nc" id="L102">        return optionalObject.map(object -&gt; ModelBuilder.this.with(subjext, predicate, object, contexts)).orElse(this);</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder withOptional (@Nonnull final Optional&lt;? extends Resource&gt; optionalSubject,
                                                   @Nonnull final IRI predicate,
                                                   @Nonnull final Value object)
      {
<span class="fc" id="L115">        return optionalSubject.map(subject -&gt; ModelBuilder.this.with(subject, predicate, object)).orElse(this);</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder withOptional (@Nonnull final Resource subject,
                                                   @Nonnull final IRI predicate,
                                                   @Nonnull final Optional&lt;? extends Value&gt; optionalObject)
      {
<span class="fc" id="L128">        return optionalObject.map(object -&gt; ModelBuilder.this.with(subject, predicate, object)).orElse(this);</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder withOptional (@Nonnull final Optional&lt;? extends Resource&gt; optionalSubject,
                                                   @Nonnull final IRI predicate,
                                                   @Nonnull final Optional&lt;? extends Value&gt; optionalObject)
      {
<span class="fc" id="L141">        return optionalObject.map(object -&gt; withOptional(optionalSubject, predicate, object)).orElse(this);</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder with (@Nonnull final List&lt;? extends Resource&gt; subjects,
                                           @Nonnull final IRI predicate,
                                           @Nonnull final Value object)
      {
<span class="fc" id="L154">        subjects.forEach(subject -&gt; ModelBuilder.this.with(subject, predicate, object)); // FIXME ?? this = withOptional(...)</span>
<span class="fc" id="L155">        return this;</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder with (@Nonnull final List&lt;? extends Resource&gt; subjects,
                                           @Nonnull final IRI predicate,
                                           @Nonnull final List&lt;? extends Value&gt; objects)
      {
<span class="pc bpc" id="L168" title="2 of 4 branches missed.">        assert subjects.size() == objects.size();</span>

<span class="fc bfc" id="L170" title="All 2 branches covered.">        for (int i = 0; i &lt; subjects.size(); i++)</span>
          {
<span class="fc" id="L172">            ModelBuilder.this.with(subjects.get(i), predicate, objects.get(i)); // FIXME ?? this = withOptional(...)</span>
          }

<span class="fc" id="L175">        return this;</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder with (@Nonnull final Resource subject,
                                           @Nonnull final IRI predicate,
                                           @Nonnull final Stream&lt;? extends Value&gt; objects)
      {
<span class="fc" id="L188">        objects.forEach(object -&gt; ModelBuilder.this.with(subject, predicate, object)); // FIXME ?? this = withOptional(...)</span>
<span class="fc" id="L189">        return this;</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ModelBuilder withOptional (@Nonnull final Optional&lt;? extends Resource&gt; subject,
                                                   @Nonnull final IRI predicate,
                                                   @Nonnull final Stream&lt;? extends Value&gt; objects)
      {
<span class="fc bfc" id="L202" title="All 2 branches covered.">        if (subject.isPresent())</span>
          {
<span class="fc" id="L204">            objects.forEach(object -&gt; withOptional(subject, predicate, object)); // FIXME ?? this = withOptional(...)</span>
?? this = withOptional(...)
} <span class="fc" id="L207"> return this;</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ @Nonnull public synchronized ModelBuilder with (@Nonnull final Statement statement) { <span class="nc" id="L218"> model.add(statement);</span> <span class="nc" id="L219"> return this;</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ @Nonnull public synchronized ModelBuilder with (@Nonnull final Optional&lt;ModelBuilder&gt; optionalBuiilder) { <span class="nc" id="L230"> optionalBuiilder.ifPresent(ModelBuilder.this::with);</span> <span class="nc" id="L231"> return this;</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ @Nonnull public synchronized ModelBuilder with (@Nonnull final ModelBuilder other) { <span class="nc" id="L242"> return ModelBuilder.this.with(other.toModel());</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ @Nonnull public synchronized ModelBuilder with (@Nonnull final Model other) { <span class="nc" id="L253"> other.forEach(model::add);</span> <span class="nc" id="L254"> return this;</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ @Nonnull public synchronized ModelBuilder with (@Nonnull final List&lt;ModelBuilder&gt; others) { <span class="nc" id="L265"> others.stream().map(ModelBuilder::toModel).forEach(m -&gt; m.forEach(model::add));</span> <span class="nc" id="L266"> return this;</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>