1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.cli2.option; |
18 | |
|
19 | |
import java.util.Iterator; |
20 | |
import java.util.ListIterator; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
import org.apache.commons.cli2.DisplaySetting; |
24 | |
import org.apache.commons.cli2.Option; |
25 | |
import org.apache.commons.cli2.WriteableCommandLine; |
26 | |
import org.apache.commons.cli2.resource.ResourceConstants; |
27 | |
import org.apache.commons.cli2.resource.ResourceHelper; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public abstract class OptionImpl implements Option { |
34 | |
private final int id; |
35 | |
private final boolean required; |
36 | |
private Option parent; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public OptionImpl(final int id, |
44 | 2153 | final boolean required) { |
45 | 2153 | this.id = id; |
46 | 2153 | this.required = required; |
47 | 2153 | } |
48 | |
|
49 | |
public boolean canProcess(final WriteableCommandLine commandLine, |
50 | |
final ListIterator arguments) { |
51 | 174 | if (arguments.hasNext()) { |
52 | 103 | final String argument = (String) arguments.next(); |
53 | 103 | arguments.previous(); |
54 | |
|
55 | 103 | return canProcess(commandLine, argument); |
56 | |
} else { |
57 | 71 | return false; |
58 | |
} |
59 | |
} |
60 | |
|
61 | |
public String toString() { |
62 | 2 | final StringBuffer buffer = new StringBuffer(); |
63 | 2 | appendUsage(buffer, DisplaySetting.ALL, null); |
64 | |
|
65 | 2 | return buffer.toString(); |
66 | |
} |
67 | |
|
68 | |
public int getId() { |
69 | 7272 | return id; |
70 | |
} |
71 | |
|
72 | |
public boolean equals(final Object thatObj) { |
73 | 2221 | if (thatObj instanceof OptionImpl) { |
74 | 2221 | final OptionImpl that = (OptionImpl) thatObj; |
75 | |
|
76 | 2221 | return (getId() == that.getId()) && |
77 | |
equals(getPreferredName(), that.getPreferredName()) && |
78 | |
equals(getDescription(), that.getDescription()) && |
79 | |
equals(getPrefixes(), that.getPrefixes()) && |
80 | |
equals(getTriggers(), that.getTriggers()); |
81 | |
} else { |
82 | 0 | return false; |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
private boolean equals(Object left, |
87 | |
Object right) { |
88 | 4294 | if ((left == null) && (right == null)) { |
89 | 693 | return true; |
90 | 3601 | } else if ((left == null) || (right == null)) { |
91 | 423 | return false; |
92 | |
} else { |
93 | 3178 | return left.equals(right); |
94 | |
} |
95 | |
} |
96 | |
|
97 | |
public int hashCode() { |
98 | 2823 | int hashCode = getId(); |
99 | 2823 | if (getPreferredName() != null) { |
100 | 2823 | hashCode = (hashCode * 37) + getPreferredName().hashCode(); |
101 | |
} |
102 | |
|
103 | 2823 | if (getDescription() != null) { |
104 | 1078 | hashCode = (hashCode * 37) + getDescription().hashCode(); |
105 | |
} |
106 | |
|
107 | 2823 | hashCode = (hashCode * 37) + getPrefixes().hashCode(); |
108 | 2823 | hashCode = (hashCode * 37) + getTriggers().hashCode(); |
109 | |
|
110 | 2823 | return hashCode; |
111 | |
} |
112 | |
|
113 | |
public Option findOption(String trigger) { |
114 | 430 | if (getTriggers().contains(trigger)) { |
115 | 138 | return this; |
116 | |
} else { |
117 | 292 | return null; |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
public boolean isRequired() { |
122 | 637 | return required; |
123 | |
} |
124 | |
|
125 | |
public void defaults(final WriteableCommandLine commandLine) { |
126 | |
|
127 | 396 | } |
128 | |
|
129 | |
public Option getParent() { |
130 | 1034 | return parent; |
131 | |
} |
132 | |
|
133 | |
public void setParent(Option parent) { |
134 | 1235 | this.parent = parent; |
135 | 1235 | } |
136 | |
|
137 | |
protected void checkPrefixes(final Set prefixes) { |
138 | |
|
139 | 1191 | if (prefixes.isEmpty()) { |
140 | 0 | return; |
141 | |
} |
142 | |
|
143 | |
|
144 | 1191 | checkPrefix(prefixes, getPreferredName()); |
145 | |
|
146 | |
|
147 | 1191 | this.getTriggers(); |
148 | |
|
149 | 1191 | for (final Iterator i = getTriggers().iterator(); i.hasNext();) { |
150 | 1784 | checkPrefix(prefixes, (String) i.next()); |
151 | |
} |
152 | 1191 | } |
153 | |
|
154 | |
private void checkPrefix(final Set prefixes, |
155 | |
final String trigger) { |
156 | 2975 | for (final Iterator i = prefixes.iterator(); i.hasNext();) { |
157 | 3721 | String prefix = (String) i.next(); |
158 | |
|
159 | 3721 | if (trigger.startsWith(prefix)) { |
160 | 2975 | return; |
161 | |
} |
162 | 746 | } |
163 | |
|
164 | 0 | final ResourceHelper helper = ResourceHelper.getResourceHelper(); |
165 | 0 | final String message = |
166 | |
helper.getMessage(ResourceConstants.OPTION_TRIGGER_NEEDS_PREFIX, trigger, |
167 | |
prefixes.toString()); |
168 | 0 | throw new IllegalArgumentException(message); |
169 | |
} |
170 | |
} |