@@ -10,8 +10,8 @@ For example, `FactoryA` should take `FactoryAOptions` and
1010expose an optional parameter "` int a ` " with the same meaning and default values.
1111
1212To maintain convenience and type-safety, both ` FactoryAOptions ` and ` FactoryBOptions ` should expose
13- a method ` a(int) ` to set the optional parameter. But ` FactoryAOptions::a ` should return an ` FactoryAOptions ` ,
14- and ` FactoryBOptions::a ` should return an ` FactoryBOptions ` to allow chaining more parameters of
13+ a method ` a(int) ` to set the optional parameter. But ` FactoryAOptions::a ` should return a ` FactoryAOptions ` ,
14+ and ` FactoryBOptions::a ` should return a ` FactoryBOptions ` to allow chaining more parameters of
1515` FactoryAOptions ` and ` FactoryBOptions ` respectively, while retaining the type of the builder.
1616
1717Using scijava-optional, this can be achieved as follows:
@@ -21,26 +21,26 @@ one exposing methods to set the parameters, one exposing methods to retrieve par
2121
2222For setting:
2323``` java
24- interface OptionA <T extends OptionA< T > > extends Options<T > {
24+ interface OptionA <T> extends Options<T > {
2525 default T a (int a ) {
26- return add (" a" , a);
26+ return setValue (" a" , a);
2727 }
2828}
2929```
3030where the ` a() ` method records the parameter value (with key ` "a" ` ) via the
31- ` add ()` method of the ` Options ` super-interface.
31+ ` setValue ()` method of the ` Options ` super-interface.
3232
3333For getting:
3434``` java
3535interface ValueA extends Values {
3636 ...
37- default int a () {}
38- return value ( "a ", 0 );
37+ default int a () {
38+ return getValueOrDefault ( " a" , 0 );
3939 }
4040}
4141```
4242where the ` a() ` method returns the parameter value (with key ` "a" ` and default value ` 0 ` )
43- via the ` value ()` method of the ` Values ` super-interface.
43+ via the ` getValueOrDefault ()` method of the ` Values ` super-interface.
4444
4545Finally, the implementation of ` FactoryAOptions ` derives from ` AbstractOptions ` and all desired
4646subsets of options
@@ -49,17 +49,12 @@ public class FactoryAOptions
4949 extends AbstractOptions< FactoryAOptions >
5050 implements OptionA< FactoryAOptions > , ...
5151{
52- static class FactoryAValues
52+ public class FactoryAValues
5353 extends AbstractValues
5454 implements ValueA , ...
55- {
56- ...
57- public FactoryAValues (FactoryAOptions options ) {
58- super ( options );
59- }
60- }
55+ {}
6156
62- public final FactoryAValues values = new FactoryAValues (this ); ;
57+ public final FactoryAValues values = new FactoryAValues () ;
6358
6459 // =======================================================================
6560
@@ -76,49 +71,36 @@ public class FactoryAOptions
7671 protected FactoryAOptions copyOrThis () {
7772 return new FactoryAOptions (this );
7873 }
79-
80- // =======================================================================
81-
82- // The following is not necessary, but can be overridden like this
83- // to make it show up more nicely in IDE auto-complete
84-
85- @Override
86- public FactoryAOptions a (int a ) {
87- return OptionA . super . a(a);
88- }
8974}
9075```
9176The parameter values are exposed through inner class ` FactoryAValues ` that derives from ` AbstractValues `
9277and all desired subsets of option values.
9378
9479The only thing that has been omitted from the above example is the parts that provide a nice ` toString ` implementation
95- for the values. This is be achieved by
80+ for the values. This is be achieved by overriding the ` forEach() ` methods in the ` Values ` interfaces and
81+ implementation
9682``` java
9783interface ValueA extends Values {
98- default void buildToString (AbstractValues .ValuesToString sb ) {
99- sb. append(" a" , a());
84+ default void forEach (BiConsumer<String , Object > action ) {
85+ action. accept(" a" , a());
86+ // and so on, for other parameters defined in this Values interface
10087 }
10188
10289 default int a () {
103- return value (" a" , 0 );
90+ return getValueOrDefault (" a" , 0 );
10491 }
10592}
10693```
10794and
10895``` java
109- static class FactoryAValues
96+ public class FactoryAValues
11097 extends AbstractValues
11198 implements ValueA , ...
11299{
113100 @Override
114- public String toString () {
115- final ValuesToString sb = new ValuesToString ();
116- ValueA . super . buildToString(sb);
117- return sb. toString();
118- }
119-
120- public FactoryAValues (FactoryAOptions options ) {
121- super (options);
101+ default void forEach (BiConsumer<String , Object > action ) {
102+ ValueA . super . forEach( action );
103+ // and so on, for other implemented Values interfaces
122104 }
123105}
124106```
0 commit comments