Skip to content

Commit 42ce11b

Browse files
committed
Add examples by @maarzt
1 parent bc1a2ff commit 42ce11b

5 files changed

Lines changed: 158 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.scijava.optional.examples.advanced;
2+
3+
public class Demo
4+
{
5+
public static void main( String... args )
6+
{
7+
final OptionsAB options = new OptionsAB().b( "Hello" ).a( 42 );
8+
System.out.println( "options = " + options );
9+
System.out.println( "options.values = " + options.values );
10+
System.out.println();
11+
System.out.println( "Option a: " + options.values.a() );
12+
System.out.println( "Option b: " + options.values.b() );
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.scijava.optional.examples.advanced;
2+
3+
import java.util.function.BiConsumer;
4+
import org.scijava.optional.Options;
5+
import org.scijava.optional.Values;
6+
7+
interface OptionsA< T > extends Options< T >
8+
{
9+
default T a( int a )
10+
{
11+
return setValue( "a", a );
12+
}
13+
14+
interface Val extends Values
15+
{
16+
default int a()
17+
{
18+
return getValueOrDefault( "a", 0 );
19+
}
20+
21+
default void forEach( BiConsumer< String, Object > action )
22+
{
23+
action.accept( "a", a() );
24+
}
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.scijava.optional.examples.advanced;
2+
3+
import java.util.function.BiConsumer;
4+
import org.scijava.optional.AbstractOptions;
5+
6+
/**
7+
* {@code OptionsAB} inherits options defined in interfaces {@code OptionsA} and {@code OptionsB}
8+
*/
9+
public class OptionsAB
10+
extends AbstractOptions< OptionsAB >
11+
implements OptionsA< OptionsAB >, OptionsB< OptionsAB >
12+
{
13+
public Values values = new Values();
14+
15+
/**
16+
* {@code OptionsAB.Values} inherits default values defined in interfaces {@code OptionsA.Val} and {@code OptionsB.Val}
17+
*/
18+
public class Values extends AbstractValues implements OptionsA.Val, OptionsB.Val
19+
{
20+
@Override
21+
public void forEach( BiConsumer< String, Object > action )
22+
{
23+
OptionsA.Val.super.forEach( action );
24+
OptionsB.Val.super.forEach( action );
25+
}
26+
}
27+
}
28+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.scijava.optional.examples.advanced;
2+
3+
import java.util.function.BiConsumer;
4+
import org.scijava.optional.Options;
5+
import org.scijava.optional.Values;
6+
7+
public interface OptionsB< T > extends Options< T >
8+
{
9+
default T b( String b )
10+
{
11+
return setValue( "b", b );
12+
}
13+
14+
interface Val extends Values
15+
{
16+
default String b()
17+
{
18+
return getValueOrDefault( "b", "" );
19+
}
20+
21+
default void forEach( BiConsumer< String, Object > action )
22+
{
23+
action.accept( "b", b() );
24+
}
25+
}
26+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.scijava.optional.examples.simple;
2+
3+
import java.util.function.BiConsumer;
4+
import org.scijava.optional.AbstractOptions;
5+
6+
public class Demo
7+
{
8+
public static void main( String... args )
9+
{
10+
print( "new MyOptions()", new MyOptions() );
11+
print( "new MyOptions().number( 42 )", new MyOptions().number( 42 ) );
12+
print( "new MyOptions().text( \"Hello\" )", new MyOptions().text( "Hello" ) );
13+
print( "new MyOptions().text( \"World\" ).number( 12 )", new MyOptions().text( "World" ).number( 12 ) );
14+
}
15+
16+
private static void print( String title, MyOptions options )
17+
{
18+
System.out.println( title );
19+
System.out.println( " options = " + options );
20+
System.out.println( " options.values = " + options.values );
21+
System.out.println( " options.values.number() = " + options.values.number() );
22+
System.out.println( " options.values.text() = " + options.values.text() );
23+
System.out.println();
24+
}
25+
26+
public static class MyOptions extends AbstractOptions< MyOptions >
27+
{
28+
public MyOptions number( int number )
29+
{
30+
return setValue( "number", number );
31+
}
32+
33+
public MyOptions text( String text )
34+
{
35+
return setValue( "text", text );
36+
}
37+
38+
public Values values = new Values();
39+
40+
public class Values extends AbstractValues
41+
{
42+
public int number()
43+
{
44+
return getValueOrDefault( "number", -1 );
45+
}
46+
47+
public String text()
48+
{
49+
return getValueOrDefault( "text", "" );
50+
}
51+
52+
/*
53+
* Overriding forEach() makes default values show up in Values.toString().
54+
* (Try commenting it out.)
55+
*/
56+
@Override
57+
public void forEach( final BiConsumer< String, Object > action )
58+
{
59+
action.accept( "number", number() );
60+
action.accept( "text", text() );
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)