View Javadoc

1   /*
2    OutputOption.java
3    Creation date : 2/07/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.impl.output;
26  
27  import static java.util.Arrays.asList;
28  import static java.util.Collections.unmodifiableSet;
29  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.SPACE_CHAR;
30  import static net.sourceforge.plantumldependency.common.constants.CommonFileConstants.TXT_EXTENSION;
31  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
32  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.COMMAND_LINE_NULL_ERROR;
33  import static net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus.ACTIVE_MANDATORY_OPTION_STATUS;
34  
35  import java.io.File;
36  import java.util.Date;
37  import java.util.Set;
38  import java.util.TreeSet;
39  
40  import net.sourceforge.plantumldependency.commoncli.command.CommandLine;
41  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
42  import net.sourceforge.plantumldependency.commoncli.option.AbstractOptionWithArgument;
43  import net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument;
44  import net.sourceforge.plantumldependency.commoncli.option.argument.impl.file.ExistingOrNotFileOptionArgumentImpl;
45  import net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus;
46  
47  /**
48   * A default implementation managing the "-o" option, allowing to write the program's result in an
49   * output file which is mandatory. <i>Note : no option should have the same main or secondary
50   * names</i>.
51   *
52   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
53   * @since 1.3.0
54   * @version 1.3.0
55   */
56  public class OutputOption extends AbstractOptionWithArgument < File > {
57  
58      /** Serial version UID. */
59      private static final long serialVersionUID = 313893193517910913L;
60  
61      /** Option main synopsis. */
62      public static final String OPTION_MAIN_SYNOPSIS = "-o";
63  
64      /** Option synopsis alias. */
65      public static final Set < String > OPTION_SYNOPSIS = unmodifiableSet(new TreeSet < String >(
66              asList(new String[] {"--output"})));
67  
68      /**
69       * Default constructor.
70       *
71       * @since 1.3.0
72       */
73      public OutputOption() {
74          this(new ExistingOrNotFileOptionArgumentImpl(true), new StringBuilder("To output file path"), SPACE_CHAR,
75                  ACTIVE_MANDATORY_OPTION_STATUS);
76      }
77  
78      /**
79       * Full constructor.
80       *
81       * @param argument
82       *            the option argument, mustn't be <code>null</code>.
83       * @param fullOptionDescription
84       *            the full option usage description, explaining what the option does (used for
85       *            helping message). <i>Note : a new {@link StringBuilder} is created.</i>
86       * @param valSepararator
87       *            the option - argument separator. Most of the time, it is a single space " " but it
88       *            may be "=" or "-", mustn't be <code>null</code>.
89       * @param optionStatus
90       *            the option status, telling if the option is active, inactive or hidden, mustn't be
91       *            <code>null</code>.
92       * @since 1.3.0
93       */
94      public OutputOption(final OptionArgument < File > argument, final StringBuilder fullOptionDescription,
95              final String valSepararator, final OptionStatus optionStatus) {
96          super(OPTION_MAIN_SYNOPSIS, OPTION_SYNOPSIS, argument, fullOptionDescription, valSepararator, optionStatus);
97      }
98  
99      /**
100      * {@inheritDoc}
101      *
102      * @since 1.3.0
103      */
104     @Override
105     public String getDefaultArgumentAsStringIfOptionNotSpecified(final CommandLine commandLine)
106             throws CommandLineException {
107         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
108 
109         return new Date().getTime() + TXT_EXTENSION;
110     }
111 
112     /**
113      * {@inheritDoc}
114      *
115      * @since 1.3.0
116      */
117     @Override
118     public String getDefaultArgumentAsStringIfOptionSpecified(final CommandLine commandLine)
119             throws CommandLineException {
120         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
121 
122         return new Date().getTime() + TXT_EXTENSION;
123     }
124 }