Package ch.kk7.confij

Class ConfijBuilder<T>

java.lang.Object
ch.kk7.confij.ConfijBuilder<T>

public class ConfijBuilder<T>
extends java.lang.Object
  • Constructor Details

    • ConfijBuilder

      protected ConfijBuilder​(@NonNull @NonNull java.lang.reflect.Type forType)
  • Method Details

    • lazySetDefaults

      protected void lazySetDefaults()
    • of

      public static <X> ConfijBuilder<X> of​(java.lang.Class<X> forClass)
      The most common constructor to create a new configuration builder of given type.
      Type Parameters:
      X - the configuration instance type
      Parameters:
      forClass - the configuration instance class
      Returns:
      a ConfiJ builder
    • of

      public static <X> ConfijBuilder<X> of​(GenericType<X> forType)
      This constructor is used to pass full generics type information, and avoid problems with type erasure (that basically removes most usable type references from runtime Class objects).
      Type Parameters:
      X - the configuration instance type
      Parameters:
      forType - type holder class
      Returns:
      a ConfiJ builder
      See Also:
      GenericType
    • loadFrom

      public ConfijBuilder<T> loadFrom​(ConfijSource... source)
      Read configuration values from these ordered sources. Intended for more complex scenarios, where loadFrom(String...) isn't good enough.
      Parameters:
      source - list of configuration sources
      Returns:
      self
    • loadFrom

      public ConfijBuilder<T> loadFrom​(java.lang.String... sourceStr)
      Read configuration values from these ordered AnySources.
      Parameters:
      sourceStr - list of path's to configuration sources
      Returns:
      self
    • loadFrom

      public ConfijBuilder<T> loadFrom​(ConfijResource... resources)
      Read configuration values from these ordered resources (like from FS, git,...). The format of the content is guessed.
      Parameters:
      resources - list of resources (like files,...) to read configurations from
      Returns:
      self
    • loadFrom

      public ConfijBuilder<T> loadFrom​(ConfijResource resource, ConfijFormat format)
      Read configuration values from a well defined source and format. Equivalent to loadFrom(ConfijSource...).
      Parameters:
      resource - resource (like files,...) to read configurations from
      format - the actual format which understants the content of resource
      Returns:
      self
    • loadOptionalFrom

      public ConfijBuilder<T> loadOptionalFrom​(java.lang.String... maybeSourceStr)
      Attempt reading configurationd from all of these ordered AnySources. Read failures are dropped silently. Usefull for optional configuration, like for tests only.
      Parameters:
      maybeSourceStr - optional sources, reading all of them is attempted
      Returns:
      self
      See Also:
      MaybeSource
    • loadFromFirstOf

      public ConfijBuilder<T> loadFromFirstOf​(java.lang.String firstSource, java.lang.String secondSource, java.lang.String... otherSources)
      Attempt reading configurations from these ordered AnySources. Ignore all configurations before and after the first successful read. Usefull if a set of configurations should be ignored if another one is present.
      Parameters:
      firstSource - an optional source
      secondSource - an optional source
      otherSources - more optional sources, only read if not an earlier one existed
      Returns:
      self
      See Also:
      OrSource
    • validateOnlyWith

      public ConfijBuilder<T> validateOnlyWith​(@NonNull @NonNull ConfijValidator<T> validator)
      replace the default validation/post-processing steps with this new validator. The default is to load all ServiceLoaders implementing ConfijValidator. To combine multiple validators use a MultiValidator.
      Parameters:
      validator - the new validator
      Returns:
      self
    • validationAllowsNull

      public ConfijBuilder<T> validationAllowsNull()
    • validationDisabled

      public ConfijBuilder<T> validationDisabled()
      Explicitly disable all validation/post-processing steps.
      Returns:
      self
      See Also:
      validateOnlyWith(ConfijValidator)
    • templatingWith

      public ConfijBuilder<T> templatingWith​(@NonNull @NonNull ValueResolver valueResolver)
    • templatingDisabled

      public ConfijBuilder<T> templatingDisabled()
      disables templating (variable substitution), such that your values containing ${variables} just stay as they are.
      Returns:
      self
    • bindValuesWith

      public ConfijBuilder<T> bindValuesWith​(ValueMapperFactory valueMapperFactory)
      Globally register an additional ValueMapperFactory. This allows for custom classes in your configuration definition. Same as writing
      
         \@ValueMapper(CustomMapper.class)
         interface MyConfigRoot { ... }
       
      Parameters:
      valueMapperFactory - the new mapper with highest preference
      Returns:
      self
      See Also:
      bindValuesForClassWith(ValueMapperInstance, Class)
    • bindValuesForClassWith

      public <I> ConfijBuilder<T> bindValuesForClassWith​(ValueMapperInstance<I> valueMapper, java.lang.Class<I> forClass)
      Convenience method to register a ValueMapperFactory which will only handle a single class.
      Type Parameters:
      I - the target type
      Parameters:
      valueMapper - functional interface to convert a string to I
      forClass - the class of I
      Returns:
      self
    • reloadStrategy

      public ConfijBuilder<T> reloadStrategy​(@NonNull @NonNull ConfijReloadStrategy reloadStrategy)
      define when/how new configurations are loaded. default is a NeverReloadStrategy.
      Parameters:
      reloadStrategy - the new reload strategy to be used
      Returns:
      self
    • reloadPeriodically

      public ConfijBuilder<T> reloadPeriodically​(@NonNull @NonNull java.time.Duration duration)
      A PeriodicReloadStrategy with initial delay equal to interval duration
      Parameters:
      duration - time between reload attempts
      Returns:
      self
    • newBindingContext

      @NonNull protected @NonNull BindingContext newBindingContext()
    • newNodeBindingContext

      @NonNull protected @NonNull NodeBindingContext newNodeBindingContext()
    • newPipeline

      @NonNull protected @NonNull ConfijPipeline<T> newPipeline()
    • build

      public T build()
      Finalize the configuration pipeline and build a single instance of it. In cases where you enable periodic configuration reloading, use buildWrapper() instead.
      Returns:
      a fully initialized configuration instance
    • buildWrapper

      public ConfijBuilder.ConfijWrapper<T> buildWrapper()
      Finalize the configuration pipeline and build a single instance holding the most recent configuration. In cases where no periodic configuration reload strategy is defined, build() is simpler to use instead.
      Returns:
      a wrapper with the most up-to-date instance and ways to register reload notification hooks.