View Javadoc

1   /*
2    BooleanOptionArgumentImpl.java
3    Creation date : 5/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.argument.impl.bool;
26  
27  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.PIPE_CHAR;
28  import static net.sourceforge.plantumldependency.common.utils.bool.YesNoBoolean.NO;
29  import static net.sourceforge.plantumldependency.common.utils.bool.YesNoBoolean.YES;
30  import static net.sourceforge.plantumldependency.common.utils.bool.YesNoBoolean.valueOf;
31  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
32  import static net.sourceforge.plantumldependency.common.utils.string.StringUtils.isNotEmpty;
33  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.EMPTY_OPTION_ARGUMENT_ERROR;
34  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.NOT_BOOLEAN_ARGUMENT_ERROR;
35  import net.sourceforge.plantumldependency.common.utils.bool.YesNoBoolean;
36  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
37  import net.sourceforge.plantumldependency.commoncli.option.argument.AbstractOptionArgument;
38  
39  /**
40   * The boolean implementation of the
41   * {@link net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument} interface,
42   * specifying a {@link YesNoBoolean} argument.
43   *
44   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
45   * @since 1.3.0
46   * @version 1.3.0
47   */
48  public class BooleanOptionArgumentImpl extends AbstractOptionArgument < YesNoBoolean > {
49  
50      /** Serial version UID. */
51      private static final long serialVersionUID = -4728168833242872815L;
52  
53      /** The argument main usage constant. */
54      private static final String MAIN_USAGE = YES.toString() + PIPE_CHAR + NO.toString();
55  
56      /** The argument main usage description constant. */
57      private static final String USAGE_DESCRIPTION = MAIN_USAGE
58              + " specifies a valid file path, not a directory. It can be absolute or relative.";
59  
60      /**
61       * Default constructor.
62       *
63       * @param optionArgumentIsMandatory
64       *            <code>true</code> if the argument is mandatory, <code>false</code> otherwise.
65       * @since 1.3.0
66       */
67      public BooleanOptionArgumentImpl(final boolean optionArgumentIsMandatory) {
68          super(optionArgumentIsMandatory, new StringBuilder(USAGE_DESCRIPTION));
69      }
70  
71      /**
72       * {@inheritDoc}
73       *
74       * @since 1.3.0
75       */
76      @Override
77      protected String getMainUsageDescription() {
78          return MAIN_USAGE;
79      }
80  
81      /**
82       * {@inheritDoc}
83       *
84       * @since 1.3.0
85       */
86      @Override
87      public YesNoBoolean parseArgument(final String argument) throws CommandLineException {
88          YesNoBoolean bool = null;
89  
90          if (isNotEmpty(argument)) {
91              try {
92                  bool = valueOf(argument.toUpperCase());
93              } catch (final IllegalArgumentException e) {
94                  throw new CommandLineException(buildLogString(NOT_BOOLEAN_ARGUMENT_ERROR, argument), e);
95              }
96          } else {
97              throw new CommandLineException(EMPTY_OPTION_ARGUMENT_ERROR);
98  
99          }
100 
101         return bool;
102     }
103 }