Content of file DefaultSiteFinder.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>DefaultSiteFinder.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 :: Mercurial</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">DefaultSiteFinder.java</span></div><h1>DefaultSiteFinder.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.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.spi.HierarchicFinderSupport;
import it.tidalwave.northernwind.core.model.SiteFinder;
import it.tidalwave.northernwind.core.impl.util.RegexTreeMap;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L51">@Immutable @RequiredArgsConstructor(access = AccessLevel.PRIVATE)</span>
<span class="fc" id="L52">@ToString(callSuper = true, exclude = {&quot;mapByRelativePath&quot;, &quot;mapByRelativeUri&quot; })</span>
public class DefaultSiteFinder&lt;T&gt; extends HierarchicFinderSupport&lt;T, SiteFinder&lt;T&gt;&gt; implements SiteFinder&lt;T&gt;
  {
    private static final long serialVersionUID = 3242345356779345L;

    /* package */
    @Nonnull final transient Map&lt;String, T&gt; mapByRelativePath;

    /* package */
    @Nullable final transient RegexTreeMap&lt;T&gt; mapByRelativeUri;

    @Nullable
    private final String relativePath;

    @Nullable
    private final String relativeUri;

    /*******************************************************************************************************************
     *
     * Constructor used to create an instance with the given data.
     *
     * @param finderName            the name (for debugging)
     * @param mapByRelativePath     the map of resources by relative path
     * @param mapByRelativeUri      the map of resources by relative uri
     *
     ******************************************************************************************************************/
    public DefaultSiteFinder (@Nonnull final String finderName,
                              @CheckForNull final Map&lt;String, T&gt; mapByRelativePath,
                              @CheckForNull final RegexTreeMap&lt;T&gt; mapByRelativeUri)
      {
<span class="fc" id="L82">        super(finderName);</span>
<span class="fc" id="L83">        this.mapByRelativePath = mapByRelativePath;</span>
<span class="fc" id="L84">        this.mapByRelativeUri = mapByRelativeUri;</span>
<span class="fc" id="L85">        this.relativePath = null;</span>
<span class="fc" id="L86">        this.relativeUri = null;</span>
<span class="fc" id="L87">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private static &lt;T&gt; DefaultSiteFinder&lt;T&gt; fields (@CheckForNull final Map&lt;String, T&gt; mapByRelativePath,
                                                    @CheckForNull final RegexTreeMap&lt;T&gt; mapByRelativeUri,
                                                    @CheckForNull final String relativePath,
                                                    @CheckForNull final String relativeUri)
      {
<span class="fc" id="L100">        Objects.requireNonNull(mapByRelativePath, &quot;Searching for a relativePath, but no map&quot;);</span>
<span class="fc" id="L101">        return new DefaultSiteFinder&lt;&gt;(mapByRelativePath, mapByRelativeUri, relativePath, relativeUri);</span>
      }

    /*******************************************************************************************************************
     *
     * Clone constructor. See documentation of {@link HierarchicFinderSupport} for more information.
     *
     * @param other     the {@code Finder} to clone
     * @param override  the override object
     *
     ******************************************************************************************************************/
    // FIXME: should be protected
should be protected
public DefaultSiteFinder (@Nonnull final DefaultSiteFinder&lt;T&gt; other, @Nonnull final Object override) { <span class="fc" id="L115"> super(other, override);</span> <span class="fc" id="L116"> final DefaultSiteFinder&lt;T&gt; source = getSource(DefaultSiteFinder.class, other, override);</span> <span class="fc" id="L117"> this.mapByRelativePath = source.mapByRelativePath;</span> <span class="fc" id="L118"> this.mapByRelativeUri = source.mapByRelativeUri;</span> <span class="fc" id="L119"> this.relativePath = source.relativePath;</span> <span class="fc" id="L120"> this.relativeUri = source.relativeUri;</span> <span class="fc" id="L121"> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public SiteFinder&lt;T&gt; withRelativePath (@Nonnull final String relativePath) { <span class="fc" id="L131"> return clonedWith(fields(mapByRelativePath, mapByRelativeUri, relativePath, relativeUri));</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public SiteFinder&lt;T&gt; withRelativeUri (@Nonnull final String relativeUri) { <span class="nc" id="L142"> return clonedWith(fields(mapByRelativePath, mapByRelativeUri, relativePath, relativeUri));</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public T result() throws NotFoundException { try { <span class="nc" id="L156"> return super.result();</span> } <span class="nc" id="L158"> catch (NotFoundException e)</span> { <span class="nc" id="L160"> var message = &quot;????&quot;;</span> <span class="nc bnc" id="L162" title="All 2 branches missed."> if (relativePath != null)</span> { <span class="nc" id="L164"> message = String.format(&quot;relativePath: %s&quot;, relativePath);</span> // message = String.format(&quot;relativePath: %s, set: %s&quot;, relativePath, mapByRelativePath.keySet()); } <span class="nc bnc" id="L167" title="All 2 branches missed."> else if (relativeUri != null)</span> { <span class="nc" id="L169"> message = String.format(&quot;relativeUri: %s&quot;, relativeUri);</span> // message = String.format(&quot;relativeUri: %s, set: %s&quot;, relativeUri, mapByRelativeUri.keySet()); } <span class="nc" id="L173"> throw new NotFoundException(message);</span> } } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull protected List&lt;T&gt; computeResults() { <span class="fc" id="L185"> final List&lt;T&gt; results = new ArrayList&lt;&gt;();</span> <span class="pc bpc" id="L187" title="1 of 2 branches missed."> if (relativePath != null)</span> { <span class="fc" id="L189"> addResults(results, mapByRelativePath, relativePath);</span> } <span class="nc bnc" id="L192" title="All 2 branches missed."> else if (relativeUri != null)</span> { <span class="nc bnc" id="L194" title="All 2 branches missed."> if (mapByRelativeUri == null)</span> { <span class="nc" id="L196"> throw new IllegalArgumentException(&quot;Searching for a relativeUri, but no map - &quot; + this);</span> } <span class="nc" id="L199"> addResults(results, mapByRelativeUri, relativeUri);</span> } else { <span class="nc" id="L204"> results.addAll(mapByRelativePath.values());</span> } <span class="fc" id="L207"> return results;</span> } /******************************************************************************************************************* * * * ******************************************************************************************************************/ private static &lt;Type&gt; void addResults (@Nonnull final List&lt;? super Type&gt; results, @Nonnull final Map&lt;String, Type&gt; map, @Nonnull final String relativePath) { <span class="pc bpc" id="L219" title="1 of 2 branches missed."> if (!relativePath.contains(&quot;*&quot;)) // FIXME: better way to guess a regexp?</span> { <span class="nc" id="L221"> final var result = map.get(relativePath);</span> <span class="nc bnc" id="L223" title="All 2 branches missed."> if (result != null)</span> { <span class="nc" id="L225"> results.add(result);</span> } <span class="nc" id="L227"> }</span> else { <span class="fc" id="L231"> final var pattern = Pattern.compile(relativePath);</span> <span class="fc bfc" id="L233" title="All 2 branches covered."> for (final var entry : map.entrySet())</span> { <span class="pc bpc" id="L235" title="1 of 2 branches missed."> if (pattern.matcher(entry.getKey()).matches())</span> { <span class="fc" id="L237"> results.add(entry.getValue());</span> } <span class="fc" id="L239"> }</span> } <span class="fc" id="L241"> }</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>