File tree Expand file tree Collapse file tree
src/main/java/org/scijava/optional Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .scijava .optional ;
2+
3+ import java .util .LinkedHashMap ;
4+ import java .util .Map ;
5+
6+ public abstract class AbstractOptions < T extends AbstractOptions < T > > implements Options < T >
7+ {
8+ final LinkedHashMap < String , Object > theOptions = new LinkedHashMap <>();
9+
10+ protected AbstractOptions ( T that )
11+ {
12+ that .theOptions .forEach ( theOptions ::put );
13+ }
14+
15+ public AbstractOptions ()
16+ {
17+ }
18+
19+ protected T copyOrThis ()
20+ {
21+ return ( T ) this ;
22+ }
23+
24+ protected T append ( final T additionalOptions )
25+ {
26+ T concat = copyOrThis ();
27+ additionalOptions .theOptions .forEach ( concat .theOptions ::put );
28+ return concat ;
29+ }
30+
31+ @ Override
32+ public T add ( final String key , final Object value )
33+ {
34+ final T copy = copyOrThis ();
35+ copy .theOptions .remove ( key );
36+ copy .theOptions .put ( key , value );
37+ return copy ;
38+ }
39+
40+ @ Override
41+ public String toString ()
42+ {
43+ final StringBuilder sb = new StringBuilder ();
44+ sb .append ( "{" );
45+ int numLeft = theOptions .size ();
46+ for ( Map .Entry < String , Object > option : theOptions .entrySet () )
47+ {
48+ sb .append ( option .getKey () );
49+ sb .append ( " = " );
50+ sb .append ( option .getValue () );
51+ if ( --numLeft > 0 )
52+ sb .append ( ", " );
53+ }
54+ sb .append ( "}" );
55+ return sb .toString ();
56+ }
57+ }
Original file line number Diff line number Diff line change 1+ package org .scijava .optional ;
2+
3+ import java .util .LinkedHashMap ;
4+
5+ public abstract class AbstractValues implements Values
6+ {
7+ private final LinkedHashMap < String , Object > theOptions ;
8+
9+ public AbstractValues ( AbstractOptions < ? > options )
10+ {
11+ this .theOptions = options .theOptions ;
12+ }
13+
14+ public void buildToString ( final ValuesToString sb )
15+ {
16+ throw new UnsupportedOperationException ( "internal, not supposed to be called" );
17+ }
18+
19+ @ Override
20+ public < T > T value ( final String key , final T defaultValue )
21+ {
22+ @ SuppressWarnings ( "unchecked" )
23+ final T value = ( T ) theOptions .get ( key );
24+ return value == null ? defaultValue : value ;
25+ }
26+
27+ public class ValuesToString
28+ {
29+ private final StringBuilder sb ;
30+
31+ private boolean first ;
32+
33+ public ValuesToString ()
34+ {
35+ sb = new StringBuilder ().append ( "{" );
36+ first = true ;
37+ }
38+
39+ public < T > void append ( final String key , final T value )
40+ {
41+ if ( first )
42+ first = false ;
43+ else
44+ sb .append ( ", " );
45+ sb .append ( key );
46+ sb .append ( " = " );
47+ sb .append ( value );
48+ if ( !theOptions .containsKey ( key ) )
49+ sb .append ( " [default]" );
50+ }
51+
52+ @ Override
53+ public String toString ()
54+ {
55+ sb .append ( "}" );
56+ return sb .toString ();
57+ }
58+ }
59+ }
Original file line number Diff line number Diff line change 1+ package org .scijava .optional ;
2+
3+ public interface Options < T extends Options < T > >
4+ {
5+ T add ( String key , Object value );
6+ }
Original file line number Diff line number Diff line change 1+ package org .scijava .optional ;
2+
3+ public interface Values
4+ {
5+ < T > T value ( String key , T defaultValue );
6+ }
You can’t perform that action at this time.
0 commit comments