Skip to content

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 :: Core :: Default Implementation</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.Map.Entry;
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.impl.util.RegexTreeMap;
import it.tidalwave.northernwind.core.model.SiteFinder;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L52">@Immutable @RequiredArgsConstructor(access = AccessLevel.PRIVATE)</span>
<span class="fc" id="L53">@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="L83">        super(finderName);</span>
<span class="fc" id="L84">        this.mapByRelativePath = mapByRelativePath;</span>
<span class="fc" id="L85">        this.mapByRelativeUri = mapByRelativeUri;</span>
<span class="fc" id="L86">        this.relativePath = null;</span>
<span class="fc" id="L87">        this.relativeUri = null;</span>
<span class="fc" id="L88">      }</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="L101">        Objects.requireNonNull(mapByRelativePath, &quot;Searching for a relativePath, but no map&quot;);</span>
<span class="fc" id="L102">        return new DefaultSiteFinder(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
    public DefaultSiteFinder (@Nonnull final DefaultSiteFinder&lt;T&gt; other, @Nonnull final Object override)
      {
<span class="fc" id="L116">        super(other, override);</span>
<span class="fc" id="L117">        final DefaultSiteFinder&lt;T&gt; source = getSource(DefaultSiteFinder.class, other, override);</span>
<span class="fc" id="L118">        this.mapByRelativePath = source.mapByRelativePath;</span>
<span class="fc" id="L119">        this.mapByRelativeUri = source.mapByRelativeUri;</span>
<span class="fc" id="L120">        this.relativePath = source.relativePath;</span>
<span class="fc" id="L121">        this.relativeUri = source.relativeUri;</span>
<span class="fc" id="L122">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public SiteFinder&lt;T&gt; withRelativePath (@Nonnull final String relativePath)
      {
<span class="fc" id="L132">        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="L143">        return clonedWith(fields(mapByRelativePath, mapByRelativeUri, relativePath, relativeUri));</span>
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public T result()
      throws NotFoundException
      {
        try
          {
<span class="nc" id="L157">            return super.result();</span>
          }
<span class="nc" id="L159">        catch (NotFoundException e)</span>
          {
<span class="nc" id="L161">            String message = &quot;????&quot;;</span>

<span class="nc bnc" id="L163" title="All 2 branches missed.">            if (relativePath != null)</span>
              {
<span class="nc" id="L165">                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="L168" title="All 2 branches missed.">            else if (relativeUri != null)</span>
              {
<span class="nc" id="L170">                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="L174">            throw new NotFoundException(message);</span>
          }
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    protected List&lt;T&gt; computeResults()
      {
<span class="fc" id="L186">        final List&lt;T&gt; results = new ArrayList&lt;&gt;();</span>

<span class="pc bpc" id="L188" title="1 of 2 branches missed.">        if (relativePath != null)</span>
          {
<span class="fc" id="L190">            addResults(results, mapByRelativePath, relativePath);</span>
          }

<span class="nc bnc" id="L193" title="All 2 branches missed.">        else if (relativeUri != null)</span>
          {
<span class="nc bnc" id="L195" title="All 2 branches missed.">            if (mapByRelativeUri == null)</span>
              {
<span class="nc" id="L197">                throw new IllegalArgumentException(&quot;Searching for a relativeUri, but no map - &quot; + this);</span>
              }

<span class="nc" id="L200">            addResults(results, mapByRelativeUri, relativeUri);</span>
          }

        else
          {
<span class="nc" id="L205">            results.addAll(mapByRelativePath.values());</span>
          }

<span class="fc" id="L208">        return results;</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    private static &lt;Type&gt; void addResults (@Nonnull final List&lt;Type&gt; results,
                                           @Nonnull final Map&lt;String, Type&gt; map,
                                           @Nonnull final String relativePath)
      {
<span class="pc bpc" id="L220" title="1 of 2 branches missed.">        if (!relativePath.contains(&quot;*&quot;)) // FIXME: better way to guess a regexp?</span>
better way to guess a regexp?
{ <span class="nc" id="L222"> final Type result = map.get(relativePath);</span> <span class="nc bnc" id="L224" title="All 2 branches missed."> if (result != null)</span> { <span class="nc" id="L226"> results.add(result);</span> } <span class="nc" id="L228"> }</span> else { <span class="fc" id="L232"> final Pattern pattern = Pattern.compile(relativePath);</span> <span class="fc bfc" id="L234" title="All 2 branches covered."> for (final Entry&lt;String, Type&gt; entry : map.entrySet())</span> { <span class="pc bpc" id="L236" title="1 of 2 branches missed."> if (pattern.matcher(entry.getKey()).matches())</span> { <span class="fc" id="L238"> results.add(entry.getValue());</span> } <span class="fc" id="L240"> }</span> } <span class="fc" id="L242"> }</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>