Interface Option<T>

  • Type Parameters:
    T - The value type of the options interface that this option customizes.
    All Superinterfaces:
    Serializable

    public interface Option<T>
    extends Serializable
    Contains utilities for implementing options interfaces.

    An options interface is an interface that only declare methods that take no arguments, and return a value object, typically with default value implementations. Example:

    
     class UserOfOptions {
         public interface MyOptions {
             default Date targetDate() { return new Date(); }
             default Font prettyFont() { return new Font( fontName(), Font.PLAIN, fontSize() ); }
             default String fontName() { return "Verdana"; }
             default int fontSize() { return 11; }
         }
         private final Date targetDate;
         private final Font prettyFont;
         public UserOfOptions( MyOptions options ) {
             this.targetDate = options.targetDate();
             this.prettyFont = options.prettyFont();
         }
     }
     
    • Method Detail

      • value

        Object value​(T options)
      • dynamicOptions

        @SafeVarargs
        static <T> T dynamicOptions​(Class<T> optionsType,
                                    Function<Method,​Object> lookup,
                                    Option<? super T>... options)
        Create a dynamic implementation of the supplied options interface using the specified options to define values. The specified options must be lambdas where the parameter name of the lambda is the name of the option of the options interface that lambda overrides. When the lambda is invoked, the instance of the options interface is given as the sole parameter, this allows custom options that depend on other options of the options interface. For options that are not overridden the lookup function provided is used to lookup the value, and if the lookup function returns null the default value is used.
        Type Parameters:
        T - the type of the options interface.
        Parameters:
        optionsType - the options interface to implement.
        lookup - the function to use to look up values not explicitly overridden.
        options - lambdas that define the overridden options.
        Returns:
        an instance of the options interface.
      • options

        @SafeVarargs
        static <T> T options​(Class<T> optionsType,
                             Option<? super T>... options)
        Create a dynamic implementation of the supplied options interface using the specified options to define values. The specified options must be lambdas where the parameter name of the lambda is the name of the option of the options interface that lambda overrides. When the lambda is invoked, the instance of the options interface is given as the sole parameter, this allows custom options that depend on other options of the options interface. For options that are not given, the default value is used.
        Type Parameters:
        T - the type of the options interface.
        Parameters:
        optionsType - the options interface to implement.
        options - lambdas that define the overridden options.
        Returns:
        an instance of the options interface.