View Javadoc
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  
28  import javax.annotation.Nonnull;
29  import java.util.Optional;
30  import java.nio.file.Path;
31  import static it.tidalwave.role.impl.ServiceLoaderLocator.lazySupplierOf;
32  
33  /***************************************************************************************************************************************************************
34   *
35   * @author  Fabrizio Giudici
36   * @since   3.2-ALPHA-17
37   *
38   **************************************************************************************************************************************************************/
39  public interface PreferencesHandler
40    {
41      static class Inner
42        {
43          private static final LazySupplier<PreferencesHandler> REF = lazySupplierOf(PreferencesHandler.class);
44        }
45  
46      public static final String PROPS_BASE_NAME = PreferencesHandler.class.getPackage().getName();
47  
48      public static final String PROP_APP_NAME = PROPS_BASE_NAME + ".appName";
49  
50      /** Suppress any console output. @since 3.2-ALPHA-21 */
51      public static final String PROP_SUPPRESS_CONSOLE = PROPS_BASE_NAME + ".suppressConsoleOutput";
52  
53      /***********************************************************************************************************************************************************
54       * Returns a singleton instance.
55       *
56       * @return  the singleton instance
57       **********************************************************************************************************************************************************/
58      @Nonnull
59      public static PreferencesHandler getInstance()
60        {
61          return Inner.REF.get();
62        }
63  
64      /***********************************************************************************************************************************************************
65       * Returns the path of the folder where files related to the app should go.
66       *
67       * @return    the path
68       **********************************************************************************************************************************************************/
69      @Nonnull
70      public Path getAppFolder();
71  
72      /***********************************************************************************************************************************************************
73       * Returns the path of the folder where logs should go.
74       *
75       * @return    the path
76       **********************************************************************************************************************************************************/
77      @Nonnull
78      public Path getLogFolder();
79  
80      /***********************************************************************************************************************************************************
81       * Sets the application name. This method must be called at boot from the {@code main} method before doing
82       * anything else.
83       *
84       * @param name    the property name
85       **********************************************************************************************************************************************************/
86      public static void setAppName (@Nonnull final String name)
87        {
88          System.setProperty(PROP_APP_NAME, name);
89        }
90  
91      /***********************************************************************************************************************************************************
92       * Gets a property.
93       *
94       * @param <T>     the property type
95       * @param name    the property name
96       * @return        the property value
97       **********************************************************************************************************************************************************/
98      @Nonnull
99      public <T> Optional<T> getProperty (@Nonnull Key<T> name);
100 
101     /***********************************************************************************************************************************************************
102      * Sets a property, overriding the current value.
103      *
104      * @param <T>     the property type
105      * @param name    the property name
106      * @param value   the property value
107      **********************************************************************************************************************************************************/
108     public <T> void setProperty (@Nonnull final Key<T> name, @Nonnull final T value);
109 
110     /**
111      *
112      * Sets a property, unless it has been already set.
113      *
114      * @param <T>     the property type
115      * @param name    the property name
116      * @param value   the property value
117      **********************************************************************************************************************************************************/
118     public <T> void setDefaultProperty (@Nonnull final Key<T> name, @Nonnull final T value);
119   }