Content of file DefaultPreferencesHandler.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>DefaultPreferencesHandler.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">TheseFoolishThings :: Examples :: DCI :: Displayable</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-util</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.util.impl</a> &gt; <span class="el_source">DefaultPreferencesHandler.java</span></div><h1>DefaultPreferencesHandler.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * TheseFoolishThings: Miscellaneous utilities
 * http://tidalwave.it/projects/thesefoolishthings
 *
 * Copyright (C) 2009 - 2023 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/thesefoolishthings-src
 * git clone https://github.com/tidalwave-it/thesefoolishthings-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.util.impl;

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import it.tidalwave.util.Key;
import it.tidalwave.util.PreferencesHandler;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L48">@RequiredArgsConstructor</span>
// PreferencesHandler can be used to programmatically set the log folder, so don't inject logger
public class DefaultPreferencesHandler implements PreferencesHandler
  {
<span class="nc" id="L52">    @Getter</span>
    private final Path appFolder;

<span class="nc" id="L55">    @Getter</span>
    private final Path logFolder;

<span class="nc" id="L58">    private final Map&lt;Key&lt;?&gt;, Object&gt; properties = new HashMap&lt;&gt;();</span>

    public DefaultPreferencesHandler()
<span class="nc" id="L61">      {</span>
        try
          {
<span class="nc" id="L64">            final var appName = System.getProperty(PROP_APP_NAME);</span>
<span class="nc" id="L65">            Objects.requireNonNull(appName, &quot;You must call PreferencesHandler.setAppName(\&quot;...\&quot;) before getting here&quot;);</span>

<span class="nc" id="L67">            final var osName = System.getProperty(&quot;os.name&quot;).toLowerCase();</span>
<span class="nc" id="L68">            var pattern = &quot;&quot;;</span>

<span class="nc bnc" id="L70" title="All 4 branches missed.">            switch (osName)</span>
              {
                case &quot;linux&quot;:
<span class="nc" id="L73">                  pattern = &quot;%s/.%s/&quot;;</span>
<span class="nc" id="L74">                  break;</span>

                case &quot;mac os x&quot;:
<span class="nc" id="L77">                  pattern = &quot;%s/Library/Application Support/%s/&quot;;</span>
<span class="nc" id="L78">                  break;</span>

                case &quot;windows&quot;:
<span class="nc" id="L81">                  pattern = &quot;%s/AppData/Local/%s/&quot;;</span>
<span class="nc" id="L82">                  break;</span>

                default:
<span class="nc" id="L85">                  throw new ExceptionInInitializerError(&quot;Unknown o.s.: &quot; + osName);</span>
              }

<span class="nc" id="L88">            final var home = System.getProperty(&quot;user.home&quot;, &quot;/tmp&quot;);</span>
<span class="nc" id="L89">            appFolder = Paths.get(String.format(pattern, home, appName)).toAbsolutePath();</span>
<span class="nc" id="L90">            logFolder = appFolder.resolve(&quot;logs&quot;).toAbsolutePath();</span>
<span class="nc" id="L91">            Files.createDirectories(logFolder);</span>
            // PreferencesHandler can be used to programmatically set the log folder, so don't log yet
<span class="nc bnc" id="L93" title="All 2 branches missed.">            if (!Boolean.getBoolean(PROP_SUPPRESS_CONSOLE))</span>
              {
<span class="nc" id="L95">                System.out.printf(&quot;App folder: %s\n&quot;, appFolder);</span>
<span class="nc" id="L96">                System.out.printf(&quot;Logging folder: %s\n&quot;, logFolder);</span>
              }
          }
<span class="nc" id="L99">        catch (IOException e)</span>
          {
<span class="nc" id="L101">            throw new RuntimeException(e);</span>
<span class="nc" id="L102">          }</span>
<span class="nc" id="L103">      }</span>

    @Override @Nonnull
    public &lt;T&gt; Optional&lt;T&gt; getProperty (@Nonnull final Key&lt;T&gt; name)
      {
<span class="nc" id="L108">        return Optional.ofNullable((T)properties.get(name));</span>
      }

    @Override
    public &lt;T&gt; void setProperty (@Nonnull final Key&lt;T&gt; name, @Nonnull final T value)
      {
<span class="nc" id="L114">        properties.put(name, value);</span>
        // FIXME: should be made persistent (JSON?)
should be made persistent (JSON?)
<span class="nc" id="L116"> }</span> @Override public &lt;T&gt; void setDefaultProperty (@Nonnull final Key&lt;T&gt; name, @Nonnull final T value) { <span class="nc bnc" id="L121" title="All 2 branches missed."> if (!properties.containsKey(name))</span> { <span class="nc" id="L123"> setProperty(name, value);</span> } <span class="nc" id="L125"> }</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>