1:
17: package ;
18:
19: import ;
20: import ;
21:
22: import ;
23: import ;
24: import ;
25: import ;
26: import ;
27:
28:
34: public class ArgumentBuilder
35: {
36:
37: private static final ResourceHelper RESOURCES = ResourceHelper.getResourceHelper();
38:
39:
40: private String m_name;
41:
42:
43: private String m_description;
44:
45:
46: private int m_minimum;
47:
48:
49: private int m_maximum;
50:
51:
52: private char m_initialSeparator;
53:
54:
55: private char m_subsequentSeparator;
56:
57:
58: private Validator m_validator;
59:
60:
61: private String m_consumeRemaining;
62:
63:
64: private List m_defaultValues;
65:
66:
67: private int m_id;
68:
69:
72: public ArgumentBuilder()
73: {
74: reset();
75: }
76:
77:
84: public final Argument create()
85: {
86: final Argument argument =
87: new ArgumentImpl(
88: m_name,
89: m_description,
90: m_minimum,
91: m_maximum,
92: m_initialSeparator,
93: m_subsequentSeparator,
94: m_validator,
95: m_consumeRemaining,
96: m_defaultValues,
97: m_id );
98: reset();
99: return argument;
100: }
101:
102:
107: public final ArgumentBuilder reset()
108: {
109: m_name = "arg";
110: m_description = null;
111: m_minimum = 0;
112: m_maximum = Integer.MAX_VALUE;
113: m_initialSeparator = ArgumentImpl.DEFAULT_INITIAL_SEPARATOR;
114: m_subsequentSeparator = ArgumentImpl.DEFAULT_SUBSEQUENT_SEPARATOR;
115: m_validator = null;
116: m_consumeRemaining = "--";
117: m_defaultValues = null;
118: m_id = 0;
119: return this;
120: }
121:
122:
131: public final ArgumentBuilder withName( final String newName )
132: {
133: if( newName == null )
134: {
135: throw new IllegalArgumentException(
136: RESOURCES.getMessage(
137: ResourceConstants.ARGUMENT_BUILDER_NULL_NAME ) );
138: }
139: if( "".equals( newName ) )
140: {
141: throw new IllegalArgumentException(
142: RESOURCES.getMessage(
143: ResourceConstants.ARGUMENT_BUILDER_EMPTY_NAME ) );
144: }
145: m_name = newName;
146: return this;
147: }
148:
149:
157: public final ArgumentBuilder withDescription( final String newDescription )
158: {
159: m_description = newDescription;
160: return this;
161: }
162:
163:
169: public final ArgumentBuilder withMinimum( final int newMinimum )
170: {
171: if( newMinimum < 0 )
172: {
173: throw new IllegalArgumentException(
174: RESOURCES.getMessage(
175: ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MINIMUM ) );
176: }
177: m_minimum = newMinimum;
178: return this;
179: }
180:
181:
187: public final ArgumentBuilder withMaximum( final int newMaximum )
188: {
189: if( newMaximum < 0 )
190: {
191: throw new IllegalArgumentException(
192: RESOURCES.getMessage(
193: ResourceConstants.ARGUMENT_BUILDER_NEGATIVE_MAXIMUM ) );
194: }
195: m_maximum = newMaximum;
196: return this;
197: }
198:
199:
208: public final ArgumentBuilder withInitialSeparator(
209: final char newInitialSeparator )
210: {
211: m_initialSeparator = newInitialSeparator;
212: return this;
213: }
214:
215:
224: public final ArgumentBuilder withSubsequentSeparator(
225: final char newSubsequentSeparator )
226: {
227: m_subsequentSeparator = newSubsequentSeparator;
228: return this;
229: }
230:
231:
238: public final ArgumentBuilder withValidator( final Validator newValidator )
239: {
240: if( newValidator == null )
241: {
242: throw new IllegalArgumentException(
243: RESOURCES.getMessage(
244: ResourceConstants.ARGUMENT_BUILDER_NULL_VALIDATOR ) );
245: }
246: m_validator = newValidator;
247: return this;
248: }
249:
250:
258: public final ArgumentBuilder withConsumeRemaining( final String newConsumeRemaining )
259: {
260: if( newConsumeRemaining == null )
261: {
262: throw new IllegalArgumentException(
263: RESOURCES.getMessage(
264: ResourceConstants.ARGUMENT_BUILDER_NULL_CONSUME_REMAINING ) );
265: }
266: if( "".equals( newConsumeRemaining ) )
267: {
268: throw new IllegalArgumentException(
269: RESOURCES.getMessage(
270: ResourceConstants.ARGUMENT_BUILDER_EMPTY_CONSUME_REMAINING ) );
271: }
272: m_consumeRemaining = newConsumeRemaining;
273: return this;
274: }
275:
276:
282: public final ArgumentBuilder withDefault( final Object defaultValue )
283: {
284: if( defaultValue == null )
285: {
286: throw new IllegalArgumentException(
287: RESOURCES.getMessage(
288: ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULT ) );
289: }
290:
291: if( m_defaultValues == null )
292: {
293: m_defaultValues = new ArrayList( 1 );
294: }
295: m_defaultValues.add( defaultValue );
296: return this;
297: }
298:
299:
305: public final ArgumentBuilder withDefaults( final List newDefaultValues )
306: {
307: if( newDefaultValues == null )
308: {
309: throw new IllegalArgumentException(
310: RESOURCES.getMessage(
311: ResourceConstants.ARGUMENT_BUILDER_NULL_DEFAULTS ) );
312: }
313: m_defaultValues = newDefaultValues;
314: return this;
315: }
316:
317:
323: public final ArgumentBuilder withId( final int newId )
324: {
325: m_id = newId;
326: return this;
327: }
328: }