View Javadoc

1   /*
2    AbstractOptionWithArgument.java
3    Creation date : 2/06/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.commoncli.option;
26  
27  import static java.util.logging.Level.FINE;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.LINE_CHAR;
30  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.TAB_CHAR;
31  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
32  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
33  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.COMMAND_LINE_NULL_ERROR;
34  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.OPTION_ARGUMENT_NULL_ERROR;
35  import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.ARGUMENT_AS_STRING_ALREADY_FOUND_FINE;
36  
37  import java.util.Set;
38  import java.util.logging.Logger;
39  
40  import net.sourceforge.plantumldependency.commoncli.command.CommandLine;
41  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
42  import net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument;
43  import net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus;
44  
45  /**
46   * The abstract implementation of the {@link OptionWithArgument} interface, providing common
47   * behaviors.
48   *
49   * @param <A>
50   *            the argument type to parse.
51   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
52   * @since 1.3.0
53   * @version 1.4.0
54   */
55  public abstract class AbstractOptionWithArgument < A > extends AbstractOption implements OptionWithArgument < A > {
56  
57      /** Serial version UID. */
58      private static final long serialVersionUID = 6525955986370555309L;
59  
60      /** The class logger. */
61      private static final transient Logger LOGGER = getLogger(AbstractOptionWithArgument.class.getName());
62  
63      /** The option argument. */
64      private OptionArgument < A > optionArgument;
65  
66      /**
67       * The option - argument separator. Most of the time, it is a single space " " but it may be "="
68       * or "-".
69       */
70      private String valueSeparator;
71  
72      /**
73       * Default constructor.
74       *
75       * @param optionName
76       *            the option main name (for instance "-v" or "-o"), mustn't be <code>null</code>.
77       * @param optionSecondaryNames
78       *            the {@link Set} containing all option secondary names, mustn't be
79       *            <code>null</code> but may be empty. <i>Note : a new {@link Set} is created.</i>
80       * @param argument
81       *            the option argument, mustn't be <code>null</code>.
82       * @param fullOptionDescription
83       *            the full option usage description, explaining what the option does (used for
84       *            helping message). <i>Note : a new {@link StringBuilder} is created.</i>
85       * @param valSepararator
86       *            the option - argument separator. Most of the time, it is a single space " " but it
87       *            may be "=" or "-", mustn't be <code>null</code>.
88       * @param optionStatus
89       *            the option status, telling if the option is active, inactive or hidden, mustn't be
90       *            <code>null</code>.
91       * @since 1.3.0
92       */
93      protected AbstractOptionWithArgument(final String optionName, final Set < String > optionSecondaryNames,
94              final OptionArgument < A > argument, final StringBuilder fullOptionDescription,
95              final String valSepararator, final OptionStatus optionStatus) {
96          super(optionName, optionSecondaryNames, fullOptionDescription, optionStatus);
97          setValueSeparator(valSepararator);
98          setOptionArgument(argument);
99      }
100 
101     /**
102      * {@inheritDoc}
103      *
104      * @since 1.3.0
105      */
106     @Override
107     public Option deepClone() {
108         final AbstractOptionWithArgument < A > a = (AbstractOptionWithArgument < A >) super.deepClone();
109         a.optionArgument = getOptionArgument().deepClone();
110         return a;
111     }
112 
113     /**
114      * {@inheritDoc}
115      *
116      * @since 1.4.0
117      */
118     @Override
119     public String findAndParseArgumentAsStringOrGetDefaultArgument(final CommandLine commandLine)
120             throws CommandLineException {
121         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
122 
123         String argumentString = commandLine.findOptionArgument(this);
124 
125         if (argumentString == null) {
126             argumentString = getDefaultArgumentAsStringIfOptionNotSpecified(commandLine);
127         } else if (argumentString.length() == 0) {
128             argumentString = getDefaultArgumentAsStringIfOptionSpecified(commandLine);
129         } else {
130             LOGGER.log(FINE, buildLogString(ARGUMENT_AS_STRING_ALREADY_FOUND_FINE, argumentString));
131         }
132 
133         return argumentString;
134     }
135 
136     /**
137      * {@inheritDoc}
138      *
139      * @since 1.3.0
140      */
141     @Override
142     public A findAndParseArgumentOrGetDefaultArgument(final CommandLine commandLine) throws CommandLineException {
143         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
144 
145         final String argumentString = commandLine.findOptionArgument(this);
146         A argument = null;
147 
148         if (argumentString == null) {
149             argument = getDefaultArgumentIfOptionNotSpecified(commandLine);
150         } else if (argumentString.length() == 0) {
151             argument = getDefaultArgumentIfOptionSpecified(commandLine);
152         } else {
153             argument = getOptionArgument().parseArgument(argumentString);
154         }
155 
156         return argument;
157     }
158 
159     /**
160      * {@inheritDoc}
161      *
162      * @since 1.3.0
163      */
164     @Override
165     public A getDefaultArgumentIfOptionNotSpecified(final CommandLine commandLine) throws CommandLineException {
166         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
167 
168         return getOptionArgument().parseArgument(getDefaultArgumentAsStringIfOptionNotSpecified(commandLine));
169     }
170 
171     /**
172      * {@inheritDoc}
173      *
174      * @since 1.3.0
175      */
176     @Override
177     public A getDefaultArgumentIfOptionSpecified(final CommandLine commandLine) throws CommandLineException {
178         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
179 
180         return getOptionArgument().parseArgument(getDefaultArgumentAsStringIfOptionSpecified(commandLine));
181     }
182 
183     /**
184      * {@inheritDoc}
185      *
186      * @since 1.3.0
187      */
188     @Override
189     protected StringBuilder getFullUsageAdditions() {
190         final StringBuilder buffer = new StringBuilder(getValueSeparator());
191         buffer.append(getOptionArgument().getMainUsage());
192         return buffer;
193     }
194 
195     /**
196      * {@inheritDoc}
197      *
198      * @since 1.3.0
199      */
200     @Override
201     protected StringBuilder getFullUsageDescriptionAdditions() {
202         final StringBuilder buffer = new StringBuilder(LINE_CHAR);
203         buffer.append(TAB_CHAR);
204         buffer.append(TAB_CHAR);
205         buffer.append(getOptionArgument().getFullUsageDescription());
206         return buffer;
207     }
208 
209     /**
210      * {@inheritDoc}
211      *
212      * @since 1.3.0
213      */
214     @Override
215     protected StringBuilder getMainUsageAdditions() {
216         final StringBuilder buffer = new StringBuilder(getValueSeparator());
217         buffer.append(getOptionArgument().getMainUsage());
218         return buffer;
219     }
220 
221     /**
222      * {@inheritDoc}
223      *
224      * @since 1.3.0
225      */
226     @Override
227     public OptionArgument < A > getOptionArgument() {
228         return optionArgument;
229     }
230 
231     /**
232      * {@inheritDoc}
233      *
234      * @since 1.3.0
235      */
236     @Override
237     public String getValueSeparator() {
238         return valueSeparator;
239     }
240 
241     /**
242      * Sets the value of <code>optionArgument</code>.
243      *
244      * @param value
245      *            the <code>optionArgument</code> to set, can be <code>null</code>.
246      * @see #getOptionArgument()
247      * @since 1.3.0
248      */
249     private void setOptionArgument(final OptionArgument < A > value) {
250         checkNull(value, OPTION_ARGUMENT_NULL_ERROR);
251 
252         optionArgument = value;
253     }
254 
255     /**
256      * Sets the value of <code>valueSeparator</code>.
257      *
258      * @param value
259      *            the <code>valueSeparator</code> to set, can be <code>null</code>.
260      * @see #getValueSeparator()
261      * @since 1.3.0
262      */
263     private void setValueSeparator(final String value) {
264         checkNull(value, OPTION_ARGUMENT_NULL_ERROR);
265 
266         valueSeparator = value;
267     }
268 
269     /**
270      * {@inheritDoc}
271      *
272      * @since 1.3.0
273      */
274     @Override
275     public String toString() {
276         return getClass().getSimpleName() + " [optionArgument=" + optionArgument + ", valueSeparator=" + valueSeparator
277                 + "]";
278     }
279 }