PreferencesHandler.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * 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
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util;

  27. import javax.annotation.Nonnull;
  28. import java.util.Optional;
  29. import java.nio.file.Path;
  30. import static it.tidalwave.role.impl.ServiceLoaderLocator.lazySupplierOf;

  31. /***************************************************************************************************************************************************************
  32.  *
  33.  * @author  Fabrizio Giudici
  34.  * @since   3.2-ALPHA-17
  35.  *
  36.  **************************************************************************************************************************************************************/
  37. public interface PreferencesHandler
  38.   {
  39.     static class Inner
  40.       {
  41.         private static final LazySupplier<PreferencesHandler> REF = lazySupplierOf(PreferencesHandler.class);
  42.       }

  43.     public static final String PROPS_BASE_NAME = PreferencesHandler.class.getPackage().getName();

  44.     public static final String PROP_APP_NAME = PROPS_BASE_NAME + ".appName";

  45.     /** Suppress any console output. @since 3.2-ALPHA-21 */
  46.     public static final String PROP_SUPPRESS_CONSOLE = PROPS_BASE_NAME + ".suppressConsoleOutput";

  47.     /***********************************************************************************************************************************************************
  48.      * Returns a singleton instance.
  49.      *
  50.      * @return  the singleton instance
  51.      **********************************************************************************************************************************************************/
  52.     @Nonnull
  53.     public static PreferencesHandler getInstance()
  54.       {
  55.         return Inner.REF.get();
  56.       }

  57.     /***********************************************************************************************************************************************************
  58.      * Returns the path of the folder where files related to the app should go.
  59.      *
  60.      * @return    the path
  61.      **********************************************************************************************************************************************************/
  62.     @Nonnull
  63.     public Path getAppFolder();

  64.     /***********************************************************************************************************************************************************
  65.      * Returns the path of the folder where logs should go.
  66.      *
  67.      * @return    the path
  68.      **********************************************************************************************************************************************************/
  69.     @Nonnull
  70.     public Path getLogFolder();

  71.     /***********************************************************************************************************************************************************
  72.      * Sets the application name. This method must be called at boot from the {@code main} method before doing
  73.      * anything else.
  74.      *
  75.      * @param name    the property name
  76.      **********************************************************************************************************************************************************/
  77.     public static void setAppName (@Nonnull final String name)
  78.       {
  79.         System.setProperty(PROP_APP_NAME, name);
  80.       }

  81.     /***********************************************************************************************************************************************************
  82.      * Gets a property.
  83.      *
  84.      * @param <T>     the property type
  85.      * @param name    the property name
  86.      * @return        the property value
  87.      **********************************************************************************************************************************************************/
  88.     @Nonnull
  89.     public <T> Optional<T> getProperty (@Nonnull Key<T> name);

  90.     /***********************************************************************************************************************************************************
  91.      * Sets a property, overriding the current value.
  92.      *
  93.      * @param <T>     the property type
  94.      * @param name    the property name
  95.      * @param value   the property value
  96.      **********************************************************************************************************************************************************/
  97.     public <T> void setProperty (@Nonnull final Key<T> name, @Nonnull final T value);

  98.     /**
  99.      *
  100.      * Sets a property, unless it has been already set.
  101.      *
  102.      * @param <T>     the property type
  103.      * @param name    the property name
  104.      * @param value   the property value
  105.      **********************************************************************************************************************************************************/
  106.     public <T> void setDefaultProperty (@Nonnull final Key<T> name, @Nonnull final T value);
  107.   }