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 final boolean required) {
45 this.id = id;
46 this.required = required;
47 }
48
49 public boolean canProcess(final WriteableCommandLine commandLine,
50 final ListIterator arguments) {
51 if (arguments.hasNext()) {
52 final String argument = (String) arguments.next();
53 arguments.previous();
54
55 return canProcess(commandLine, argument);
56 } else {
57 return false;
58 }
59 }
60
61 public String toString() {
62 final StringBuffer buffer = new StringBuffer();
63 appendUsage(buffer, DisplaySetting.ALL, null);
64
65 return buffer.toString();
66 }
67
68 public int getId() {
69 return id;
70 }
71
72 public boolean equals(final Object thatObj) {
73 if (thatObj instanceof OptionImpl) {
74 final OptionImpl that = (OptionImpl) thatObj;
75
76 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 return false;
83 }
84 }
85
86 private boolean equals(Object left,
87 Object right) {
88 if ((left == null) && (right == null)) {
89 return true;
90 } else if ((left == null) || (right == null)) {
91 return false;
92 } else {
93 return left.equals(right);
94 }
95 }
96
97 public int hashCode() {
98 int hashCode = getId();
99 if (getPreferredName() != null) {
100 hashCode = (hashCode * 37) + getPreferredName().hashCode();
101 }
102
103 if (getDescription() != null) {
104 hashCode = (hashCode * 37) + getDescription().hashCode();
105 }
106
107 hashCode = (hashCode * 37) + getPrefixes().hashCode();
108 hashCode = (hashCode * 37) + getTriggers().hashCode();
109
110 return hashCode;
111 }
112
113 public Option findOption(String trigger) {
114 if (getTriggers().contains(trigger)) {
115 return this;
116 } else {
117 return null;
118 }
119 }
120
121 public boolean isRequired() {
122 return required;
123 }
124
125 public void defaults(final WriteableCommandLine commandLine) {
126
127 }
128
129 public Option getParent() {
130 return parent;
131 }
132
133 public void setParent(Option parent) {
134 this.parent = parent;
135 }
136
137 protected void checkPrefixes(final Set prefixes) {
138
139 if (prefixes.isEmpty()) {
140 return;
141 }
142
143
144 checkPrefix(prefixes, getPreferredName());
145
146
147 this.getTriggers();
148
149 for (final Iterator i = getTriggers().iterator(); i.hasNext();) {
150 checkPrefix(prefixes, (String) i.next());
151 }
152 }
153
154 private void checkPrefix(final Set prefixes,
155 final String trigger) {
156 for (final Iterator i = prefixes.iterator(); i.hasNext();) {
157 String prefix = (String) i.next();
158
159 if (trigger.startsWith(prefix)) {
160 return;
161 }
162 }
163
164 final ResourceHelper helper = ResourceHelper.getResourceHelper();
165 final String message =
166 helper.getMessage(ResourceConstants.OPTION_TRIGGER_NEEDS_PREFIX, trigger,
167 prefixes.toString());
168 throw new IllegalArgumentException(message);
169 }
170 }