<?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 :: Model</a> > <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-commons</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.util</a> > <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 "License"); 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 "AS IS" 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="nc bnc" id="L48" title="All 2 branches missed.">@ThreadSafe</span>
public class ModelBuilder
{
<span class="nc" id="L51"> private final Model model = new TreeModel();</span>
@Nonnull private final Resource[] contexts;
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
public ModelBuilder (@Nonnull final Resource ... contexts)
<span class="nc" id="L61"> {</span>
<span class="nc" id="L62"> this.contexts = contexts;</span>
<span class="nc" id="L63"> }</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public synchronized Model toModel()
{
<span class="nc" 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="nc" id="L87"> model.add(subject, predicate, object, contexts);</span>
<span class="nc" id="L88"> return this;</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public synchronized ModelBuilder with (@Nonnull final Resource subjext,
@Nonnull final IRI predicate,
@Nonnull final Optional<Value> optionalObject,
@Nonnull final Resource... contexts)
{
<span class="nc" id="L102"> return optionalObject.map(object -> ModelBuilder.this.with(subjext, predicate, object, contexts)).orElse(this);</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public synchronized ModelBuilder withOptional (@Nonnull final Optional<? extends Resource> optionalSubject,
@Nonnull final IRI predicate,
@Nonnull final Value object)
{
<span class="nc" id="L115"> return optionalSubject.map(subject -> 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<? extends Value> optionalObject)
{
<span class="nc" id="L128"> return optionalObject.map(object -> ModelBuilder.this.with(subject, predicate, object)).orElse(this);</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public synchronized ModelBuilder withOptional (@Nonnull final Optional<? extends Resource> optionalSubject,
@Nonnull final IRI predicate,
@Nonnull final Optional<? extends Value> optionalObject)
{
<span class="nc" id="L141"> return optionalObject.map(object -> withOptional(optionalSubject, predicate, object)).orElse(this);</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public synchronized ModelBuilder with (@Nonnull final List<? extends Resource> subjects,
@Nonnull final IRI predicate,
@Nonnull final Value object)
{
<span class="nc" id="L154"> subjects.forEach(subject -> ModelBuilder.this.with(subject, predicate, object)); // FIXME ?? this = withOptional(...)</span>
<span class="nc" id="L155"> return this;</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public synchronized ModelBuilder with (@Nonnull final List<? extends Resource> subjects,
@Nonnull final IRI predicate,
@Nonnull final List<? extends Value> objects)
{
<span class="nc bnc" id="L168" title="All 4 branches missed."> assert subjects.size() == objects.size();</span>
<span class="nc bnc" id="L170" title="All 2 branches missed."> for (int i = 0; i < subjects.size(); i++)</span>
{
<span class="nc" id="L172"> ModelBuilder.this.with(subjects.get(i), predicate, objects.get(i)); // FIXME ?? this = withOptional(...)</span>
?? this = withOptional(...)
}
<span class="nc" id="L175"> return this;</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public synchronized ModelBuilder with (@Nonnull final Resource subject,
@Nonnull final IRI predicate,
@Nonnull final Stream<? extends Value> objects)
{
<span class="nc" id="L188"> objects.forEach(object -> ModelBuilder.this.with(subject, predicate, object)); // FIXME ?? this = withOptional(...)</span>
<span class="nc" id="L189"> return this;</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public synchronized ModelBuilder withOptional (@Nonnull final Optional<? extends Resource> subject,
@Nonnull final IRI predicate,
@Nonnull final Stream<? extends Value> objects)
{
<span class="nc bnc" id="L202" title="All 2 branches missed."> if (subject.isPresent())</span>
{
<span class="nc" id="L204"> objects.forEach(object -> withOptional(subject, predicate, object)); // FIXME ?? this = withOptional(...)</span>
}
<span class="nc" 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<ModelBuilder> 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<ModelBuilder> others)
{
<span class="nc" id="L265"> others.stream().map(ModelBuilder::toModel).forEach(m -> 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>