View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A base implementation of Option providing limited ground work for further
31   * Option implementations.
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       * Creates an OptionImpl with the specified id
40       * @param id the unique id of this Option
41       * @param required true iff this Option must be present
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         // nothing to do normally
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         // nothing to do if empty prefix list
139         if (prefixes.isEmpty()) {
140             return;
141         }
142 
143         // check preferred name
144         checkPrefix(prefixes, getPreferredName());
145 
146         // check triggers
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 }