Content of file DefaultResourceProperties.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>DefaultResourceProperties.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">NorthernWind :: Filesystems :: SCM</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-default</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.core.impl.model</a> &gt; <span class="el_source">DefaultResourceProperties.java</span></div><h1>DefaultResourceProperties.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * NorthernWind - lightweight CMS
 * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
 * %%
 * Copyright (C) 2011 - 2023 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.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.northernwind.core.impl.model;

import javax.annotation.Nonnull;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.io.IOException;
import it.tidalwave.util.As;
import it.tidalwave.util.Id;
import it.tidalwave.util.Key;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.northernwind.core.model.ResourcePath;
import it.tidalwave.northernwind.core.model.ResourceProperties;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;
import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;
import static java.util.stream.Collectors.*;

/***********************************************************************************************************************
 *
 * The default implementation of {@link ResourceProperties}.
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
// FIXME: this is a patched copy, needs public constructor for builder - see NW-180
<span class="pc" id="L60">@Slf4j @ToString(exclude={&quot;propertyResolver&quot;, &quot;asSupport&quot;})</span>
public class DefaultResourceProperties implements ResourceProperties
  {
    @SuppressWarnings(&quot;squid:S1171&quot;)
<span class="fc" id="L64">    private static final Map&lt;Class&lt;?&gt;, Function&lt;String, Object&gt;&gt; CONVERTER_MAP =</span>
        new HashMap&lt;&gt;()
<span class="fc" id="L66">          {{</span>
<span class="fc" id="L67">            put(Integer.class,        Integer::parseInt);</span>
<span class="fc" id="L68">            put(Float.class,          Float::parseFloat);</span>
<span class="fc" id="L69">            put(Double.class,         Double::parseDouble);</span>
<span class="fc" id="L70">            put(Boolean.class,        Boolean::parseBoolean);</span>
<span class="fc" id="L71">            put(ZonedDateTime.class,  o -&gt; ZonedDateTime.parse(o, ISO_ZONED_DATE_TIME));</span>
<span class="fc" id="L72">            put(ResourcePath.class,   ResourcePath::of);</span>
<span class="fc" id="L73">          }};</span>

<span class="nc" id="L75">    @Nonnull @Getter</span>
    private final Id id;

    /* Use String as key, and not Key. In this way properties can be managed both in an untyped fashion - e.g. by means
    of Key.of(&quot;foo&quot;, Object.class) - and typed at the same time - e.g. Key.of(&quot;foo&quot;, Boolean.class). */
<span class="nc" id="L80">    private final Map&lt;String, Object&gt; propertyMap = new HashMap&lt;&gt;();</span>

<span class="nc" id="L82">    private final Map&lt;Id, DefaultResourceProperties&gt; groupMap = new HashMap&lt;&gt;();</span>

    @Nonnull
    private final PropertyResolver propertyResolver;

<span class="nc" id="L87">    @Delegate</span>
<span class="nc" id="L88">    private final As asSupport = As.forObject(this);</span>

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public DefaultResourceProperties (@Nonnull final ResourceProperties.Builder builder)
<span class="nc" id="L95">      {</span>
<span class="nc" id="L96">        this.id = builder.getId();</span>
<span class="nc" id="L97">        this.propertyResolver = builder.getPropertyResolver();</span>
<span class="nc" id="L98">        this.propertyMap.putAll(builder.getValues());</span>
//        for (final Entry&lt;Key&lt;?&gt;, Object&gt; entry : builder.getValues().entrySet())
//          {
//            final String s = entry.getKey().stringValue();
//            final Object value = entry.getValue();
//            propertyMap.put(new Key&lt;&gt;(s) {}, value);
//          }
<span class="nc" id="L105">      }</span>

    /*******************************************************************************************************************
     *
     * Deep clone constructor.
     *
     ******************************************************************************************************************/
    public DefaultResourceProperties (@Nonnull final DefaultResourceProperties otherProperties)
<span class="nc" id="L113">      {</span>
<span class="nc" id="L114">        this.id = otherProperties.id;</span>
<span class="nc" id="L115">        this.propertyResolver = otherProperties.propertyResolver;</span>

<span class="nc" id="L117">        otherProperties.propertyMap.forEach(propertyMap::put); // FIXME: clone the property</span>
<span class="nc" id="L118">        otherProperties.groupMap.forEach((k, v) -&gt; groupMap.put(k, new DefaultResourceProperties(v)));</span>
<span class="nc" id="L119">      }</span>

    /*******************************************************************************************************************
     *
     * Legacy code for converting from flat-style properties. This is different than passing from() in the Builder,
     * since that approach doesn't support nested groups.
     *
     ******************************************************************************************************************/
    public DefaultResourceProperties (@Nonnull final Id id,
                                      @Nonnull final Map&lt;String, Object&gt; map,
                                      @Nonnull final PropertyResolver propertyResolver)
<span class="nc" id="L130">      {</span>
<span class="nc" id="L131">        this.id = id;</span>
<span class="nc" id="L132">        this.propertyResolver = propertyResolver;</span>

<span class="nc" id="L134">        final Map&lt;Id, Map&lt;String, Object&gt;&gt; othersMap = new HashMap&lt;&gt;();</span>

<span class="nc bnc" id="L136" title="All 2 branches missed.">        for (final var entry : map.entrySet())</span>
          {
<span class="nc" id="L138">            final var s = entry.getKey();</span>
<span class="nc" id="L139">            final var value = entry.getValue();</span>

<span class="nc bnc" id="L141" title="All 2 branches missed.">            if (!s.contains(&quot;.&quot;))</span>
              {
<span class="nc" id="L143">                propertyMap.put(s, value);</span>
              }
            else
              {
<span class="nc" id="L147">                final var x = s.split(&quot;\\.&quot;);</span>
<span class="nc" id="L148">                final var groupId = new Id(x[0]);</span>
<span class="nc" id="L149">                final var otherMap = othersMap.computeIfAbsent(groupId, __ -&gt; new HashMap&lt;&gt;());</span>
<span class="nc" id="L150">                otherMap.put(x[1], value);</span>
              }
<span class="nc" id="L152">          }</span>

<span class="nc bnc" id="L154" title="All 2 branches missed.">        for (final var entry : othersMap.entrySet())</span>
          {
<span class="nc" id="L156">            groupMap.put(entry.getKey(), new DefaultResourceProperties(entry.getKey(), entry.getValue(), propertyResolver));</span>
<span class="nc" id="L157">          }</span>
<span class="nc" id="L158">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public &lt;T&gt; Optional&lt;T&gt; getProperty (@Nonnull final Key&lt;? extends T&gt; key)
      {
        try
          {
<span class="nc" id="L170">            final var value = propertyMap.get(key.getName());</span>
<span class="nc bnc" id="L171" title="All 2 branches missed.">            return Optional.of(convertValue(key, (value != null) ? value : propertyResolver.resolveProperty(id, key)));</span>
          }
<span class="nc" id="L173">        catch (IOException e)</span>
          {
<span class="nc" id="L175">            log.trace(&quot;Could not resolve property&quot;, e);</span>
<span class="nc" id="L176">            return Optional.empty();</span>
          }
<span class="nc" id="L178">        catch (NotFoundException e)</span>
          {
<span class="nc" id="L180">            log.trace(&quot;Could not resolve property {}&quot;, e.getMessage());</span>
<span class="nc" id="L181">            return Optional.empty();</span>
          }
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public ResourceProperties getGroup (@Nonnull final Id id)
      {
<span class="nc" id="L193">        final var properties = groupMap.get(id);</span>
<span class="nc bnc" id="L194" title="All 2 branches missed.">        return properties != null ? properties : new DefaultResourceProperties(this);</span>
//                                  : new DefaultResourceProperties(new Builder().withId(id).withPropertyResolver(propertyResolver));
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Collection&lt;Key&lt;?&gt;&gt; getKeys()
      {
<span class="nc" id="L206">        return propertyMap.keySet().stream().map(Key::of).collect(toList());</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Collection&lt;Id&gt; getGroupIds() // FIXME: should be a Set
      {
<span class="nc" id="L217">        return new CopyOnWriteArrayList&lt;&gt;(groupMap.keySet());</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public &lt;T&gt; DefaultResourceProperties withProperty (@Nonnull final Key&lt;T&gt; key, @Nonnull final T value)
      {
<span class="nc" id="L228">        final var result = new DefaultResourceProperties(this);</span>
<span class="nc" id="L229">        result.propertyMap.put(key.getName(), value); // FIXME: clone property</span>
<span class="nc" id="L230">        return result;</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public DefaultResourceProperties withoutProperty (@Nonnull final Key&lt;?&gt; key)
      {
<span class="nc" id="L241">        final var result = new DefaultResourceProperties(this);</span>
<span class="nc" id="L242">        result.propertyMap.remove(key.getName());</span>
<span class="nc" id="L243">        return result;</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public DefaultResourceProperties withProperties (@Nonnull final ResourceProperties properties)
      {
<span class="nc" id="L254">        final var result = new DefaultResourceProperties(this);</span>
<span class="nc" id="L255">        result.groupMap.put(properties.getId(), new DefaultResourceProperties((DefaultResourceProperties)properties));</span>
<span class="nc" id="L256">        return result;</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public ResourceProperties merged (@Nonnull final ResourceProperties properties)
      {
<span class="nc" id="L267">        final var otherProperties = (DefaultResourceProperties)properties;</span>

<span class="nc bnc" id="L269" title="All 2 branches missed.">        if (!id.equals(otherProperties.id))</span>
          {
<span class="nc" id="L271">            throw new IllegalArgumentException(&quot;Id mismatch &quot; + id + &quot; vs &quot; + otherProperties.id);</span>
          }

<span class="nc" id="L274">        ResourceProperties result = new DefaultResourceProperties(this);</span>

<span class="nc bnc" id="L276" title="All 2 branches missed.">        for (final var entry : otherProperties.propertyMap.entrySet())</span>
          {
<span class="nc" id="L278">            result = result.withProperty(Key.of(entry.getKey()), entry.getValue());</span>
<span class="nc" id="L279">          }</span>

<span class="nc bnc" id="L281" title="All 2 branches missed.">        for (final var entry : otherProperties.groupMap.entrySet())</span>
          {
<span class="nc" id="L283">            final var groupId = entry.getKey();</span>
<span class="nc" id="L284">            final ResourceProperties propertyGroup = entry.getValue();</span>
<span class="nc bnc" id="L285" title="All 2 branches missed.">            result = (!groupMap.containsKey(groupId)) ? result.withProperties(propertyGroup)</span>
<span class="nc" id="L286">                                                      : result.withProperties(groupMap.get(groupId).merged(propertyGroup));</span>
<span class="nc" id="L287">          }</span>

<span class="nc" id="L289">        return result;</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public ResourceProperties withId (@Nonnull final Id id)
      {
<span class="nc" id="L300">        return new DefaultResourceProperties(this);</span>
//        return new DefaultResourceProperties(new Builder().withId(id).withPropertyResolver(propertyResolver));
      }

    /*******************************************************************************************************************
     *
     * Converts a property value from String to its expected value. This is because properties are read by unmarshaller
     * as string.
     *
     ******************************************************************************************************************/
    @Nonnull
    /* visible for testing */ static &lt;T&gt; T convertValue (@Nonnull final Key&lt;T&gt; key, @Nonnull final Object value)
      {
<span class="fc" id="L313">        log.trace(&quot;convertValue({}, {})&quot;, key, value);</span>
        final T result;

        try
          {
<span class="fc bfc" id="L318" title="All 2 branches covered.">            if (key.getType().isAssignableFrom(value.getClass()))</span>
              {
<span class="fc" id="L320">                result = key.getType().cast(value);</span>
              }
<span class="pc bpc" id="L322" title="1 of 2 branches missed.">            else if (&quot;tags&quot;.equals(key.getName())) // workaround as Zephyr stores it as a comma-separated string</span>
              {
<span class="nc" id="L324">                result = (T)List.of(((String)value).split(&quot;,&quot;));</span>
              }
//            else if (value instanceof List)
//              {
//                final List&lt;Object&gt; list = (List&lt;Object&gt;)value;
//                Class&lt;?&gt; elementType = String.class; // FIXME: should get the generic of the list
should get the generic of the list
// // return (T)list.stream() // .map(i -&gt; CONVERTER_MAP.getOrDefault(elementType, o -&gt; o).apply(value)) // .collect(toList()); // } else { <span class="pc" id="L337"> result = (T)CONVERTER_MAP.getOrDefault(key.getType(), o -&gt; o).apply((String)value);</span> } <span class="fc" id="L340"> log.trace(&quot;&gt;&gt;&gt;&gt; returning {} ({})&quot;, result, result.getClass().getName());</span> <span class="fc" id="L341"> return result;</span> } <span class="nc" id="L343"> catch (Exception e)</span> { <span class="nc" id="L345"> throw new RuntimeException(String.format(&quot;Can't convert '%s' to %s(%s)&quot;, value, key, key.getType()), e);</span> } } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>