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">SteelBlue - JavaFX Bindings</a> > <a href="index.source.html" class="el_package">it.tidalwave.util.impl</a> > <span class="el_source">DefaultPreferencesHandler.java</span></div><h1>DefaultPreferencesHandler.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* SteelBlue: DCI User Interfaces
* http://tidalwave.it/projects/steelblue
*
* Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); 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 "AS IS" 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/steelblue-src
* git clone https://github.com/tidalwave-it/steelblue-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.util.impl;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
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>
public class DefaultPreferencesHandler implements PreferencesHandler
{
<span class="nc" id="L51"> @Getter</span>
private final Path appFolder;
<span class="nc" id="L54"> @Getter</span>
private final Path logFolder;
<span class="nc" id="L57"> private final Map<Key<?>, Object> properties = new HashMap<>();</span>
public DefaultPreferencesHandler()
<span class="nc" id="L60"> {</span>
try
{
<span class="nc" id="L63"> final String appName = System.getProperty(PROP_APP_NAME);</span>
<span class="nc" id="L64"> Objects.requireNonNull(appName,</span>
"You must call PreferencesHandler.setAppName(\"...\") before getting here");
<span class="nc" id="L67"> final String osName = System.getProperty("os.name").toLowerCase();</span>
<span class="nc" id="L68"> String pattern = "";</span>
<span class="nc bnc" id="L70" title="All 4 branches missed."> switch (osName)</span>
{
case "linux":
<span class="nc" id="L73"> pattern = "%s/.%s/";</span>
<span class="nc" id="L74"> break;</span>
case "mac os x":
<span class="nc" id="L77"> pattern = "%s/Library/Application Support/%s/";</span>
<span class="nc" id="L78"> break;</span>
case "windows":
<span class="nc" id="L81"> pattern = "%s/AppData/Local/%s/";</span>
<span class="nc" id="L82"> break;</span>
default:
<span class="nc" id="L85"> throw new ExceptionInInitializerError("Unknown o.s.: " + osName);</span>
}
<span class="nc" id="L88"> final String home = System.getProperty("user.home", "/tmp");</span>
<span class="nc" id="L89"> appFolder = Paths.get(String.format(pattern, home, appName)).toAbsolutePath();</span>
<span class="nc" id="L90"> logFolder = appFolder.resolve("logs").toAbsolutePath();</span>
<span class="nc" id="L91"> Files.createDirectories(logFolder);</span>
<span class="nc" id="L92"> System.err.println("App folder: " + appFolder);</span>
<span class="nc" id="L93"> System.err.println("Logging folder: " + logFolder);</span>
}
<span class="nc" id="L95"> catch (IOException e)</span>
{
<span class="nc" id="L97"> throw new RuntimeException(e);</span>
<span class="nc" id="L98"> }</span>
<span class="nc" id="L99"> }</span>
@Override
public <T> Optional<T> getProperty (@Nonnull final Key<T> name)
{
<span class="nc" id="L104"> return Optional.ofNullable((T)properties.get(name));</span>
}
@Override
public <T> void setProperty (@Nonnull final Key<T> name, @Nonnull final T value)
{
<span class="nc" id="L110"> properties.put(name, value);</span>
// FIXME: should be made persistent (JSON?)
| should be made persistent (JSON?) | |
<span class="nc" id="L112"> }</span>
@Override
public <T> void setDefaultProperty (@Nonnull final Key<T> name, @Nonnull final T value)
{
<span class="nc bnc" id="L117" title="All 2 branches missed."> if (!properties.containsKey(name))</span>
{
<span class="nc" id="L119"> setProperty(name, value);</span>
}
<span class="nc" id="L121"> }</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>