Content of file PathAwareEntityDecorator.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>PathAwareEntityDecorator.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 Server</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-model</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.model.impl</a> &gt; <span class="el_source">PathAwareEntityDecorator.java</span></div><h1>PathAwareEntityDecorator.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.model.impl;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.nio.file.Path;
import java.nio.file.Paths;
import it.tidalwave.bluemarine2.model.MediaFolder;
import it.tidalwave.bluemarine2.model.spi.Entity;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import lombok.Getter;
import static it.tidalwave.role.Identifiable._Identifiable_;
import static it.tidalwave.role.SimpleComposite._SimpleComposite_;

/***********************************************************************************************************************
 *
 * An adapter for {@link Entity} to a {@link MediaFolder}. It can be used to adapt entities that naturally do
 * not belong to a hierarchy, such as an artist, to contexts where a hierarchy is needed (e.g. for browsing).
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
public class PathAwareEntityDecorator extends EntityDecorator implements PathAwareEntity
  {
    @Nonnull
    protected final PathAwareEntity parent;

<span class="nc" id="L55">    @Getter @Nonnull</span>
    protected final Path pathSegment;

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    protected PathAwareEntityDecorator (@Nonnull final Entity delegate,
                                        @Nonnull final PathAwareEntity parent,
                                        @Nonnull final Path pathSegment,
                                        @Nonnull final Collection&lt;Object&gt; roles)
      {
<span class="fc" id="L66">        super(delegate, roles);</span>
<span class="fc" id="L67">        this.pathSegment = pathSegment;</span>
<span class="fc" id="L68">        this.parent = parent;</span>
<span class="fc" id="L69">      }</span>

    protected PathAwareEntityDecorator (@Nonnull final Entity delegate,
                                        @Nonnull final PathAwareEntity parent,
                                        @Nonnull final Path pathSegment)
      {
<span class="fc" id="L75">        this(delegate, parent, pathSegment, Collections.emptyList());</span>
<span class="fc" id="L76">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Optional&lt;PathAwareEntity&gt; getParent()
      {
<span class="nc" id="L86">        return Optional.of(parent);</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Path getPath()
      {
<span class="fc" id="L97">        return parent.getPath().resolve(pathSegment);</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public String toString()
      {
<span class="nc" id="L108">        return String.format(&quot;%s(path=%s, delegate=%s, parent=%s)&quot;, getClass().getSimpleName(), getPath(), delegate, parent);</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public String toDumpString()
      {
<span class="fc" id="L119">        return String.format(&quot;Entity(path=%s, delegate=%s, parent=Folder(path=%s))&quot;, getPath(), delegate, parent.getPath());</span>
      }

    /*******************************************************************************************************************
     *
     * Creates a wrapped entity, with the given parent.
     *
     * @param       parent      the parent
     * @param       entity      the source entity
     * @return                  the wrapped entity
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static PathAwareEntity wrappedEntity (@Nonnull final PathAwareEntity parent, @Nonnull final Entity entity)
      {
<span class="pc bpc" id="L134" title="1 of 2 branches missed.">        if (entity instanceof PathAwareEntity) // FIXME: possibly avoid calling</span>
possibly avoid calling
{ <span class="nc" id="L136"> return (PathAwareEntity)entity;</span> } <span class="fc" id="L139"> final Path pathSegment = idToPathSegment(entity);</span> <span class="fc bfc" id="L140" title="All 2 branches covered."> return entity.maybeAs(_SimpleComposite_).isPresent()</span> <span class="fc" id="L141"> ? new PathAwareMediaFolderDecorator(entity, parent, pathSegment)</span> <span class="fc" id="L142"> : new PathAwareEntityDecorator(entity, parent, pathSegment);</span> } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private static Path idToPathSegment (@Nonnull final Entity entity) { <span class="fc" id="L151"> return Paths.get(entity.as(_Identifiable_).getId().stringValue().replace('/', '_'));</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>