4848 */
4949public class PreferencesCommandLine extends CommandLineImpl {
5050
51- private static final char NUL = '\0' ;
52- private final Preferences preferences ;
53- private final Option root ;
54- private final char separator ;
51+ private static final char NUL = '\0' ;
52+ private final Preferences preferences ;
53+ private final Option root ;
54+ private final char separator ;
5555
56- /**
56+ /**
5757 * Creates a new PreferencesCommandLine using the specified root Option and
5858 * Preferences node. Argument values will be separated using the char 0.
5959 *
60- * @param root the CommandLine's root Option
61- * @param preferences the Preferences node to get values from
62- */
63- public PreferencesCommandLine (final Option root , final Preferences preferences ){
64- this (root ,preferences ,NUL );
65- }
60+ * @param root the CommandLine's root Option
61+ * @param preferences the Preferences node to get values from
62+ */
63+ public PreferencesCommandLine (final Option root , final Preferences preferences ){
64+ this (root ,preferences ,NUL );
65+ }
6666
6767 /**
6868 * Creates a new PreferencesCommandLine using the specified root Option,
@@ -72,99 +72,99 @@ public PreferencesCommandLine(final Option root, final Preferences preferences){
7272 * @param preferences the Preferences node to get values from
7373 * @param separator the character to split argument values
7474 */
75- public PreferencesCommandLine (final Option root , final Preferences preferences , final char separator ){
76- this .root = root ;
77- this .preferences = preferences ;
78- this .separator = separator ;
79- }
80-
81- public boolean hasOption (Option option ) {
82- if (option ==null ){
83- return false ;
84- }
85- else {
86- try {
87- return Arrays .asList (preferences .keys ()).contains (option .getPreferredName ());
88- } catch (BackingStoreException e ) {
89- return false ;
90- }
91- }
92- }
93-
94- public Option getOption (String trigger ) {
95- return root .findOption (trigger );
96- }
97-
98- public List getValues (final Option option , final List defaultValues ) {
99- final String value = preferences .get (option .getPreferredName (),null );
100-
101- if (value ==null ){
102- return defaultValues ;
103- }
104- else if (separator >NUL ){
105- final List values = new ArrayList ();
106- final StringTokenizer tokens = new StringTokenizer (value ,String .valueOf (separator ));
107-
108- while (tokens .hasMoreTokens ()){
109- values .add (tokens .nextToken ());
110- }
111-
112- return values ;
113- }
114- else {
115- return Collections .singletonList (value );
116- }
117- }
118-
119- public Boolean getSwitch (final Option option , final Boolean defaultValue ) {
120- final String value = preferences .get (option .getPreferredName (),null );
121- if ("true" .equals (value )){
122- return Boolean .TRUE ;
123- }
124- else if ("false" .equals (value )){
125- return Boolean .FALSE ;
126- }
127- else {
128- return defaultValue ;
129- }
130- }
131-
132- public String getProperty (final String property , final String defaultValue ) {
133- return preferences .get (property , defaultValue );
134- }
135-
136- public Set getProperties () {
137- try {
138- return new HashSet (Arrays .asList (preferences .keys ()));
139- } catch (BackingStoreException e ) {
140- return Collections .EMPTY_SET ;
141- }
142- }
143-
144- public List getOptions () {
145- try {
146- final List options = new ArrayList ();
147- final Iterator keys = Arrays .asList (preferences .keys ()).iterator ();
148- while (keys .hasNext ()) {
149- final String trigger = (String ) keys .next ();
150- final Option option = root .findOption (trigger );
151- if (option != null ) {
152- options .add (option );
153- }
154- }
155- return Collections .unmodifiableList (options );
156- } catch (BackingStoreException e ) {
157- return Collections .EMPTY_LIST ;
158- }
159- }
160-
161- public Set getOptionTriggers () {
162- final Set triggers = new HashSet ();
163- final Iterator options = getOptions ().iterator ();
164- while (options .hasNext ()){
165- final Option option = (Option )options .next ();
166- triggers .addAll (option .getTriggers ());
167- }
168- return Collections .unmodifiableSet (triggers );
169- }
75+ public PreferencesCommandLine (final Option root , final Preferences preferences , final char separator ){
76+ this .root = root ;
77+ this .preferences = preferences ;
78+ this .separator = separator ;
79+ }
80+
81+ public boolean hasOption (Option option ) {
82+ if (option ==null ){
83+ return false ;
84+ }
85+ else {
86+ try {
87+ return Arrays .asList (preferences .keys ()).contains (option .getPreferredName ());
88+ } catch (BackingStoreException e ) {
89+ return false ;
90+ }
91+ }
92+ }
93+
94+ public Option getOption (String trigger ) {
95+ return root .findOption (trigger );
96+ }
97+
98+ public List getValues (final Option option , final List defaultValues ) {
99+ final String value = preferences .get (option .getPreferredName (),null );
100+
101+ if (value ==null ){
102+ return defaultValues ;
103+ }
104+ else if (separator >NUL ){
105+ final List values = new ArrayList ();
106+ final StringTokenizer tokens = new StringTokenizer (value ,String .valueOf (separator ));
107+
108+ while (tokens .hasMoreTokens ()){
109+ values .add (tokens .nextToken ());
110+ }
111+
112+ return values ;
113+ }
114+ else {
115+ return Collections .singletonList (value );
116+ }
117+ }
118+
119+ public Boolean getSwitch (final Option option , final Boolean defaultValue ) {
120+ final String value = preferences .get (option .getPreferredName (),null );
121+ if ("true" .equals (value )){
122+ return Boolean .TRUE ;
123+ }
124+ else if ("false" .equals (value )){
125+ return Boolean .FALSE ;
126+ }
127+ else {
128+ return defaultValue ;
129+ }
130+ }
131+
132+ public String getProperty (final String property , final String defaultValue ) {
133+ return preferences .get (property , defaultValue );
134+ }
135+
136+ public Set getProperties () {
137+ try {
138+ return new HashSet (Arrays .asList (preferences .keys ()));
139+ } catch (BackingStoreException e ) {
140+ return Collections .EMPTY_SET ;
141+ }
142+ }
143+
144+ public List getOptions () {
145+ try {
146+ final List options = new ArrayList ();
147+ final Iterator keys = Arrays .asList (preferences .keys ()).iterator ();
148+ while (keys .hasNext ()) {
149+ final String trigger = (String ) keys .next ();
150+ final Option option = root .findOption (trigger );
151+ if (option != null ) {
152+ options .add (option );
153+ }
154+ }
155+ return Collections .unmodifiableList (options );
156+ } catch (BackingStoreException e ) {
157+ return Collections .EMPTY_LIST ;
158+ }
159+ }
160+
161+ public Set getOptionTriggers () {
162+ final Set triggers = new HashSet ();
163+ final Iterator options = getOptions ().iterator ();
164+ while (options .hasNext ()){
165+ final Option option = (Option )options .next ();
166+ triggers .addAll (option .getTriggers ());
167+ }
168+ return Collections .unmodifiableSet (triggers );
169+ }
170170}
0 commit comments