1:
17: package ;
18:
19: import ;
20: import ;
21: import ;
22:
23: import ;
24: import ;
25: import ;
26: import ;
27: import ;
28:
29:
35: public abstract class OptionImpl implements Option
36: {
37: private final int m_id;
38: private final boolean m_required;
39:
40:
45: public OptionImpl( final int id, final boolean required )
46: {
47: m_id = id;
48: m_required = required;
49: }
50:
51:
61: public boolean canProcess(
62: final WriteableCommandLine commandLine, final ListIterator arguments )
63: {
64: if( arguments.hasNext() )
65: {
66: final String argument = (String) arguments.next();
67: arguments.previous();
68: return canProcess( commandLine, argument );
69: }
70: else
71: {
72: return false;
73: }
74: }
75:
76:
80: public String toString()
81: {
82: final StringBuffer buffer = new StringBuffer();
83: appendUsage( buffer, DisplaySetting.ALL, null );
84: return buffer.toString();
85: }
86:
87:
104: public int getId()
105: {
106: return m_id;
107: }
108:
109:
114: public boolean equals( final Object thatObj )
115: {
116: if( thatObj instanceof OptionImpl )
117: {
118: final OptionImpl that = (OptionImpl) thatObj;
119: return ( getId() == that.getId() )
120: && equals( getPreferredName(), that.getPreferredName() )
121: && equals( getDescription(), that.getDescription() )
122: && equals( getPrefixes(), that.getPrefixes() )
123: && equals( getTriggers(), that.getTriggers() );
124: }
125: else
126: {
127: return false;
128: }
129: }
130:
131: private boolean equals( Object left, Object right )
132: {
133: if( ( left == null ) && ( right == null ) )
134: {
135: return true;
136: }
137: else if( ( left == null ) || ( right == null ) )
138: {
139: return false;
140: }
141: else
142: {
143: return left.equals( right );
144: }
145: }
146:
147:
151: public int hashCode()
152: {
153: int hashCode = getId();
154: hashCode = ( hashCode * 37 ) + getPreferredName().hashCode();
155: if( getDescription() != null )
156: {
157: hashCode = ( hashCode * 37 ) + getDescription().hashCode();
158: }
159: hashCode = ( hashCode * 37 ) + getPrefixes().hashCode();
160: hashCode = ( hashCode * 37 ) + getTriggers().hashCode();
161: return hashCode;
162: }
163:
164:
170: public Option findOption( String trigger )
171: {
172: if( getTriggers().contains( trigger ) )
173: {
174: return this;
175: }
176: else
177: {
178: return null;
179: }
180: }
181:
182:
186: public boolean isRequired()
187: {
188: return m_required;
189: }
190:
191:
199: public void defaults( final WriteableCommandLine commandLine )
200: {
201:
202: }
203:
204:
208: protected void checkPrefixes( final Set prefixes )
209: {
210:
211: if( prefixes.isEmpty() )
212: {
213: return;
214: }
215:
216:
217: checkPrefix( prefixes, getPreferredName() );
218:
219:
220: getTriggers();
221:
222: for( final Iterator i = getTriggers().iterator(); i.hasNext();)
223: {
224: checkPrefix( prefixes, (String) i.next() );
225: }
226: }
227:
228:
233: private void checkPrefix( final Set prefixes, final String trigger )
234: {
235: for( final Iterator i = prefixes.iterator(); i.hasNext();)
236: {
237: String prefix = (String) i.next();
238: if( trigger.startsWith( prefix ) )
239: {
240: return;
241: }
242: }
243:
244: final ResourceHelper helper = ResourceHelper.getResourceHelper();
245: final String message =
246: helper.getMessage(
247: ResourceConstants.OPTION_TRIGGER_NEEDS_PREFIX,
248: trigger,
249: prefixes.toString() );
250: throw new IllegalArgumentException( message );
251: }
252: }