View Javadoc

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