Class ConfigurationService

java.lang.Object
org.meeuw.configuration.ConfigurationService

public class ConfigurationService extends Object
A set of static methods to maintain a thread local Configuration. Every configuration has a set of ConfigurationAspect and values. These values are initially loaded via java service loading. This initial default values can be changed, and also the values per thread.

So configuration is:

  1. Type safe. Every aspect has its own implementation with their own 'withers'
  2. JVM global defaults can be set.
  3. This default configuration object is the initial value of every thread local configuration
Configuration and their aspects are unmodifiable, and can only be entirely replaced by updated values.
Since:
0.7
  • Field Details

  • Method Details

    • setupUserPreferences

      public static void setupUserPreferences()
      Persists settings via ConfigurationPreferences (a wrapper around Preferences). This may result in a config file in $HOME/.java/.userPrefs (UNIXes), in $HOME/Library/Preferences/org.meeuw.configuration.plist (OS X) or registry entries (Windows)
    • defaultConfiguration

      public static void defaultConfiguration(Consumer<Configuration.Builder> consumer)
      Configures the default configuration object.
      Parameters:
      consumer - the code to configure the new default configuration. it will receive a Configuration.Builder with the existing configuration.
      See Also:
    • getConfiguration

      public static Configuration getConfiguration()
      Returns:
      the current (thread local) configuration object
    • setConfiguration

      public static void setConfiguration(Configuration configuration)
      Sets the given configuration object as a thread local
      Parameters:
      configuration - the new configuration
    • setConfiguration

      public static ConfigurationService.Reset setConfiguration(Configuration configuration, Consumer<Configuration.Builder> consumer)
      Since:
      0.10
    • setConfiguration

      public static ConfigurationService.Reset setConfiguration(Consumer<Configuration.Builder> consumer)
      Since:
      0.10
    • setConfiguration

      public static void setConfiguration(Configuration.Builder configuration)
      Since:
      0.10
    • resetToDefaults

      public static void resetToDefaults()
      Unsets the configuration thread local, effectively resetting it the default settings.
    • resetToDefaultDefaults

      public static void resetToDefaultDefaults()
      Resets all settings (via defaultConfiguration(Consumer).
      See Also:
    • sync

      public static boolean sync()
    • getConfigurationAspect

      public static <E extends ConfigurationAspect> E getConfigurationAspect(Class<E> clazz)
      Type Parameters:
      E - The type of the aspect to obtain
      Parameters:
      clazz - the class of the aspect to obtain
      Returns:
      an aspect of the current configuration object
    • withConfiguration

      public static <R> R withConfiguration(Configuration configuration, Supplier<R> supplier)
      Executes code with a certain configuration. Will set given configuration, and restore the existing one after calling the supplier
      Parameters:
      configuration - The configuration to run in
      supplier - The code to execute
    • withConfiguration

      public static void withConfiguration(Configuration configuration, Runnable r)
      Runs a piece of code with a certain Configuration This is just withConfiguration(Configuration, Supplier), but accepting a Runnable
    • withAspect

      public static <E extends ConfigurationAspect, R> R withAspect(Class<E> configurationAspectClass, UnaryOperator<E> aspectConfigurer, Supplier<R> r)
      Runs a piece of code, but before that configure one configuration aspect
      Parameters:
      configurationAspectClass - the class of the configuration aspect to configure
      aspectConfigurer - unary operator on a ConfigurationAspect, returns a new value for that aspect
    • withAspect

      public static <E extends ConfigurationAspect> void withAspect(Class<E> configurationAspectClass, UnaryOperator<E> aspectConfigurer, Runnable r)
    • withAspect

      public static <E extends ConfigurationAspect> void withAspect(E configurationAspect, Runnable r)
    • withAspect

      public static <E extends ConfigurationAspect, R> R withAspect(E configurationAspect, Supplier<R> r)
    • withConfiguration

      public static void withConfiguration(Consumer<Configuration.Builder> configuration, Runnable runnable)