1- /**
2- * Copyright 2003-2004 The Apache Software Foundation
1+ /*
2+ * Copyright 2003-2005 The Apache Software Foundation
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import java .util .ListIterator ;
2222
2323/**
24- * A Validator instance that parses Numbers
24+ * The <code>NumberValidator</code> validates the string argument
25+ * values are numbers. If the value is a number, the string value in
26+ * the {@link java.util.List} of values is replaced with the
27+ * {@link java.lang.Number} instance.
28+ *
29+ * A maximum and minimum value can also be specified using
30+ * the {@link #setMaximum setMaximum}, and the
31+ * {@link #setMinimum setMinimum} methods.
32+ *
33+ * The following example shows how to limit the valid values
34+ * for the age attribute to integers less than 100.
35+ *
36+ * <pre>
37+ * ...
38+ * ArgumentBuilder builder = new ArgumentBuilder();
39+ * NumberValidator validator = NumberValidator.getIntegerInstance();
40+ * validator.setMaximum(new Integer(100));
41+ *
42+ * Argument age =
43+ * builder.withName("age");
44+ * .withValidator(validator);
45+ * </pre>
46+ *
47+ * @author Rob Oxspring
48+ * @author John Keyes
2549 */
2650public class NumberValidator implements Validator {
2751
2852 /**
29- * @return an instance using local currency format
53+ * Returns a <code>NumberValidator</code> for a currency format
54+ * for the current default locale.
55+ * @return a <code>NumberValidator</code> for a currency format
56+ * for the current default locale.
3057 */
3158 public static NumberValidator getCurrencyInstance () {
3259 return new NumberValidator (NumberFormat .getCurrencyInstance ());
3360 }
3461
3562 /**
36- * @return an instance using local integer format
63+ * Returns a <code>NumberValidator</code> for an integer number format
64+ * for the current default locale.
65+ * @return a <code>NumberValidator</code> for an integer number format
66+ * for the current default locale.
3767 */
3868 public static NumberValidator getIntegerInstance () {
3969 final NumberFormat format = NumberFormat .getNumberInstance ();
@@ -42,25 +72,36 @@ public static NumberValidator getIntegerInstance() {
4272 }
4373
4474 /**
45- * @return an instance using local percent format
75+ * Returns a <code>NumberValidator</code> for a percentage format
76+ * for the current default locale.
77+ * @return a <code>NumberValidator</code> for a percentage format
78+ * for the current default locale.
4679 */
4780 public static NumberValidator getPercentInstance () {
4881 return new NumberValidator (NumberFormat .getPercentInstance ());
4982 }
5083
5184 /**
52- * @return an instance using local number format
85+ * Returns a <code>NumberValidator</code> for a general-purpose
86+ * number format for the current default locale.
87+ * @returns a <code>NumberValidator</code> for a general-purpose
88+ * number format for the current default locale.
5389 */
5490 public static NumberValidator getNumberInstance () {
5591 return new NumberValidator (NumberFormat .getNumberInstance ());
5692 }
5793
94+ /** the <code>NumberFormat</code> being used. */
5895 private NumberFormat format ;
96+
97+ /** the lower bound for argument values. */
5998 private Number minimum = null ;
99+
100+ /** the upper bound for argument values */
60101 private Number maximum = null ;
61102
62103 /**
63- * Creates a new NumberValidator
104+ * Creates a new NumberValidator.
64105 */
65106 public NumberValidator () {
66107 this (NumberFormat .getInstance ());
@@ -74,6 +115,13 @@ public NumberValidator(final NumberFormat format) {
74115 this .format = format ;
75116 }
76117
118+ /**
119+ * Validate the list of values against the list of permitted values.
120+ * If a value is valid, replace the string in the <code>values</code>
121+ * {@link java.util.List} with the {@link java.lang.Number} instance.
122+ *
123+ * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
124+ */
77125 public void validate (final List values ) throws InvalidArgumentException {
78126 for (final ListIterator i = values .listIterator (); i .hasNext ();) {
79127 final String value = (String )i .next ();
@@ -96,44 +144,56 @@ public void validate(final List values) throws InvalidArgumentException {
96144 }
97145
98146 /**
99- * @return the format of a valid Number
147+ * Return the format being used to validate argument values against.
148+ *
149+ * @return the format being used to validate argument values against.
100150 */
101151 public NumberFormat getFormat () {
102152 return format ;
103153 }
104154
105155 /**
106- * @return the maximum value for a valid Number
156+ * Specify the format being used to validate argument values against.
157+ *
158+ * @param format the format being used to validate argument values against.
159+ */
160+ public void setFormat (NumberFormat format ) {
161+ this .format = format ;
162+ }
163+
164+ /**
165+ * Return the maximum value allowed for an argument value.
166+ *
167+ * @return the maximum value allowed for an argument value.
107168 */
108169 public Number getMaximum () {
109170 return maximum ;
110171 }
111172
112173 /**
113- * @param maximum the maximum value for a valid Number
174+ * Specify the maximum value allowed for an argument value.
175+ *
176+ * @param maximum the maximum value allowed for an argument value.
114177 */
115178 public void setMaximum (Number maximum ) {
116179 this .maximum = maximum ;
117180 }
118181
119182 /**
120- * @return the minimum value for a valid Number
183+ * Return the minimum value allowed for an argument value.
184+ *
185+ * @return the minimum value allowed for an argument value.
121186 */
122187 public Number getMinimum () {
123188 return minimum ;
124189 }
125190
126191 /**
127- * @param minimum the minimum value for a valid Number
192+ * Specify the minimum value allowed for an argument value.
193+ *
194+ * @param minimum the minimum value allowed for an argument value.
128195 */
129196 public void setMinimum (Number minimum ) {
130197 this .minimum = minimum ;
131198 }
132-
133- /**
134- * @param format The format to set.
135- */
136- public void setFormat (NumberFormat format ) {
137- this .format = format ;
138- }
139199}
0 commit comments