T - The value type of the options interface that this option customizes.@FunctionalInterface public interface Option<T> extends Serializable
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();
}
}
This class contains a DSL that uses reflection to make it
convenient to create a custom instance of an options interface. Example:
UserOfOptions[] custom = {
new UserOfOptions( options( UserOfOptions.MyOptions.class,
fontName -> "Times New Roman",
fontSize -> 22 ) ),
new UserOfOptions( options( UserOfOptions.MyOptions.class,
targetDate -> new Date( 1963, 11, 22 ),
prettyFont -> new Font( "Arial", Font.BOLD, prettyFont.fontSize() ) ),
};
| Modifier and Type | Interface and Description |
|---|---|
static class |
Option.OptionHandler<T>
Implementation detail: the
InvocationHandler used for implementing an options interface. |
| Modifier and Type | Method and Description |
|---|---|
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.
|
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.
|
Object |
value(T options) |
@SafeVarargs static <T> T dynamicOptions(Class<T> optionsType, Function<Method,Object> lookup, Option<? super T>... options)
null the default value is used.T - the type of the options interface.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.@SafeVarargs static <T> T options(Class<T> optionsType, Option<? super T>... options)
T - the type of the options interface.optionsType - the options interface to implement.options - lambdas that define the overridden options.Copyright © 2019. All rights reserved.