Content of file MetadataSupport.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>MetadataSupport.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 :: Catalog</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.spi</a> &gt; <span class="el_source">MetadataSupport.java</span></div><h1>MetadataSupport.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.spi;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.nio.file.Path;
import it.tidalwave.util.Key;
import it.tidalwave.bluemarine2.model.MediaItem;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="pc" id="L50">@RequiredArgsConstructor @ToString(of = &quot;properties&quot;) @Slf4j</span>
public class MetadataSupport implements MediaItem.Metadata
  {
<span class="nc" id="L53">    @Getter @Nonnull</span>
    protected final Path path;

    @Nonnull
    protected Optional&lt;Function&lt;Key&lt;?&gt;, MediaItem.Metadata&gt;&gt; fallback;

<span class="fc" id="L59">    protected final Map&lt;Key&lt;?&gt;, Object&gt; properties = new HashMap&lt;&gt;();</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public MetadataSupport (@Nonnull final Path path)
      {
<span class="fc" id="L68">        this(path, Optional.empty());</span>
<span class="fc" id="L69">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public &lt;T&gt; Optional&lt;T&gt; get (@Nonnull final Key&lt;T&gt; key)
      {
<span class="fc bfc" id="L79" title="All 2 branches covered.">        return properties.containsKey(key) ? Optional.ofNullable((T)properties.get(key))</span>
<span class="pc" id="L80">                                           : fallback.flatMap(fb -&gt; fb.apply(key).get(key));</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public &lt;T&gt; T getAll (@Nonnull final Key&lt;T&gt; key)
      {
<span class="nc" id="L91">        final T list = (T)properties.get(key);</span>
<span class="nc bnc" id="L92" title="All 2 branches missed.">        return (list != null) ? list :</span>
<span class="nc" id="L93">                fallback.flatMap(fb -&gt; fb.apply(key).get(key)).orElse((T)Collections.emptyList());</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public boolean containsKey (@Nonnull final Key&lt;?&gt; key)
      {
<span class="nc" id="L104">        return properties.containsKey(key);</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Set&lt;Key&lt;?&gt;&gt; getKeys()
      {
<span class="fc" id="L115">        return Collections.unmodifiableSet(properties.keySet());</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Set&lt;Map.Entry&lt;Key&lt;?&gt;, ?&gt;&gt; getEntries()
      {
<span class="fc" id="L126">        return Collections.unmodifiableSet(properties.entrySet());</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public &lt;T&gt; MediaItem.Metadata with (@Nonnull final Key&lt;T&gt; key, @Nonnull final T value)
      {
<span class="fc" id="L137">        final MetadataSupport clone = new MetadataSupport(path, fallback);</span>
<span class="fc" id="L138">        clone.properties.putAll(this.properties);</span>
<span class="fc" id="L139">        clone.put(key, value);</span>

<span class="fc bfc" id="L141" title="All 2 branches covered.">        if (value instanceof ITunesComment)</span>
          {
<span class="fc" id="L143">            clone.put(CDDB, ((ITunesComment) value).getCddb());</span>
          }

<span class="fc" id="L146">        return clone;</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public MediaItem.Metadata withFallback (@Nonnull final Function&lt;Key&lt;?&gt;, MediaItem.Metadata&gt; fallback)
      {
<span class="fc" id="L157">        final MetadataSupport clone = new MetadataSupport(path, Optional.of(fallback));</span>
<span class="fc" id="L158">        clone.properties.putAll(this.properties);</span>
<span class="fc" id="L159">        return clone;</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public &lt;T&gt; MediaItem.Metadata with (@Nonnull final Key&lt;T&gt; key, @Nonnull final Optional&lt;T&gt; value)
      {
<span class="fc" id="L170">        return value.map(t -&gt; with(key, t)).orElse(this);</span>
      }

    /*******************************************************************************************************************
     *
     * FIXME: remove this, make it truly immutable.
remove this, make it truly immutable.
* ******************************************************************************************************************/ protected &lt;V&gt; void put (@Nonnull final Key&lt;V&gt; key, @Nullable final V value) { <span class="fc bfc" id="L180" title="All 2 branches covered."> if (value != null)</span> { <span class="fc" id="L182"> properties.put(key, value);</span> } <span class="fc" id="L184"> }</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>