View Javadoc

1   /*
2    LocaleOptionArgumentImpl.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.locale;
26  
27  import static java.util.Locale.ENGLISH;
28  import static java.util.Locale.FRENCH;
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  
32  import java.util.Locale;
33  
34  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
35  import net.sourceforge.plantumldependency.commoncli.option.argument.AbstractOptionArgument;
36  
37  /**
38   * The locale implementation of the
39   * {@link net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument} interface,
40   * specifying a directory {@link Locale} argument.
41   *
42   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
43   * @since 1.3.0
44   * @version 1.3.0
45   */
46  public class LocaleOptionArgumentImpl extends AbstractOptionArgument < Locale > {
47  
48      /** Serial version UID. */
49      private static final long serialVersionUID = 5730167187613883418L;
50  
51      /** The argument main usage constant. */
52      private static final String MAIN_USAGE = "LOCALE";
53  
54      /** The argument main usage description constant. */
55      private static final String USAGE_DESCRIPTION = MAIN_USAGE + " specifies a locale, with the form \""
56              + FRENCH.getLanguage() + "\", \"" + ENGLISH.getLanguage() + "\"";
57  
58      /**
59       * Default constructor.
60       *
61       * @param optionArgumentIsMandatory
62       *            <code>true</code> if the argument is mandatory, <code>false</code> otherwise.
63       * @since 1.3.0
64       */
65      public LocaleOptionArgumentImpl(final boolean optionArgumentIsMandatory) {
66          super(optionArgumentIsMandatory, new StringBuilder(USAGE_DESCRIPTION));
67      }
68  
69      /**
70       * {@inheritDoc}
71       *
72       * @since 1.3.0
73       */
74      @Override
75      protected String getMainUsageDescription() {
76          return MAIN_USAGE;
77      }
78  
79      /**
80       * {@inheritDoc}
81       *
82       * @since 1.3.0
83       */
84      @Override
85      public Locale parseArgument(final String argument) throws CommandLineException {
86          Locale locale = null;
87  
88          if (isNotEmpty(argument)) {
89              locale = new Locale(argument);
90          } else {
91              throw new CommandLineException(EMPTY_OPTION_ARGUMENT_ERROR);
92          }
93  
94          return locale;
95      }
96  }