View Javadoc

1   /*
2    AboutOption.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.impl.about;
26  
27  import static java.util.Arrays.asList;
28  import static java.util.Collections.unmodifiableSet;
29  import static java.util.logging.Level.FINE;
30  import static java.util.logging.Logger.getLogger;
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.JAVA_PROGRAM_NULL_ERROR;
35  import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.OPTION_NOT_SPECIFIED_FINE;
36  import static net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus.ACTIVE_OPTIONAL_OPTION_STATUS;
37  
38  import java.util.Set;
39  import java.util.TreeSet;
40  import java.util.logging.Logger;
41  
42  import net.sourceforge.plantumldependency.commoncli.command.CommandLine;
43  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
44  import net.sourceforge.plantumldependency.commoncli.option.AbstractOption;
45  import net.sourceforge.plantumldependency.commoncli.option.execution.ExecutableOption;
46  import net.sourceforge.plantumldependency.commoncli.option.execution.OptionExecution;
47  import net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus;
48  import net.sourceforge.plantumldependency.commoncli.program.JavaProgram;
49  
50  /**
51   * A default implementation managing the "-about" option, displaying the program authors
52   * information. <i>Note : no option should have the same main or secondary names</i>.
53   *
54   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
55   * @since 1.3.0
56   * @version 1.3.0
57   */
58  public class AboutOption extends AbstractOption implements ExecutableOption {
59  
60      /** Serial version UID. */
61      private static final long serialVersionUID = -89198534939353471L;
62  
63      /** The class logger. */
64      private static final transient Logger LOGGER = getLogger(AboutOption.class.getName());
65  
66      /** Option main synopsis. */
67      public static final String OPTION_MAIN_SYNOPSIS = "-about";
68  
69      /** Option synopsis alias. */
70      public static final Set < String > OPTION_SYNOPSIS = unmodifiableSet(new TreeSet < String >(asList(new String[] {
71              "--author", "--authors"})));
72  
73      /** The java program to display authors information. */
74      private JavaProgram javaProgram;
75  
76      /** The option priority. */
77      private int priority;
78  
79      /**
80       * Default constructor.
81       *
82       * @param program
83       *            the java program to display authors information, mustn't be <code>null</code>.
84       * @param optionPriority
85       *            the option priority as an integer. <i>Note : the priority must be unique amongst
86       *            all options</i>.
87       * @since 1.3.0
88       */
89      public AboutOption(final JavaProgram program, final int optionPriority) {
90          this(new StringBuilder("To display information about " + program.getName() + ", its license and its authors."),
91                  program, optionPriority, ACTIVE_OPTIONAL_OPTION_STATUS);
92      }
93  
94      /**
95       * Full constructor.
96       *
97       * @param fullOptionDescription
98       *            the full option usage description, explaining what the option does (used for
99       *            helping message). <i>Note : a new {@link StringBuilder} is created.</i>
100      * @param program
101      *            the java program to display authors information, mustn't be <code>null</code>.
102      * @param optionPriority
103      *            the option priority as an integer. <i>Note : the priority must be unique amongst
104      *            all options</i>.
105      * @param optionStatus
106      *            the option status, telling if the option is active, inactive or hidden, mustn't be
107      *            <code>null</code>.
108      * @since 1.3.0
109      */
110     public AboutOption(final StringBuilder fullOptionDescription, final JavaProgram program, final int optionPriority,
111             final OptionStatus optionStatus) {
112         super(OPTION_MAIN_SYNOPSIS, OPTION_SYNOPSIS, fullOptionDescription, optionStatus);
113         setJavaProgram(program);
114         setPriority(optionPriority);
115     }
116 
117     /**
118      * {@inheritDoc}
119      *
120      * @since 1.3.0
121      */
122     @Override
123     public AboutOption deepClone() {
124         final AboutOption a = (AboutOption) super.deepClone();
125         a.javaProgram = getJavaProgram().deepClone();
126         return a;
127     }
128 
129     /**
130      * {@inheritDoc}
131      *
132      * @since 1.3.0
133      */
134     @Override
135     protected StringBuilder getFullUsageAdditions() {
136         return new StringBuilder();
137     }
138 
139     /**
140      * {@inheritDoc}
141      *
142      * @since 1.3.0
143      */
144     @Override
145     protected StringBuilder getFullUsageDescriptionAdditions() {
146         return new StringBuilder();
147     }
148 
149     /**
150      * Gets the value of <code>javaProgram</code>.
151      *
152      * @return the value of <code>javaProgram</code>.
153      * @see #setJavaProgram(JavaProgram)
154      * @since 1.3.0
155      */
156     private JavaProgram getJavaProgram() {
157         return javaProgram;
158     }
159 
160     /**
161      * {@inheritDoc}
162      *
163      * @since 1.3.0
164      */
165     @Override
166     protected StringBuilder getMainUsageAdditions() {
167         return new StringBuilder();
168     }
169 
170     /**
171      * {@inheritDoc}
172      *
173      * @since 1.3.0
174      */
175     @Override
176     public int getPriority() {
177         return priority;
178     }
179 
180     /**
181      * {@inheritDoc}
182      *
183      * @since 1.3.0
184      */
185     @Override
186     public OptionExecution parseCommandLine(final CommandLine commandLine) throws CommandLineException {
187         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
188 
189         OptionExecution optionExecution = null;
190 
191         if (commandLine.isOptionActiveAndSpecified(this)) {
192             optionExecution = new AboutOptionExecution(getJavaProgram(), getPriority());
193         } else {
194             LOGGER.log(FINE, buildLogString(OPTION_NOT_SPECIFIED_FINE, getAllNames()));
195         }
196 
197         return optionExecution;
198     }
199 
200     /**
201      * Sets the value of <code>javaProgram</code>.
202      *
203      * @param value
204      *            the <code>javaProgram</code> to set, can be <code>null</code>.
205      * @see #getJavaProgram()
206      * @since 1.3.0
207      */
208     private void setJavaProgram(final JavaProgram value) {
209         checkNull(value, JAVA_PROGRAM_NULL_ERROR);
210 
211         javaProgram = value;
212     }
213 
214     /**
215      * Sets the value of <code>priority</code>.
216      *
217      * @param value
218      *            the <code>priority</code> to set.
219      * @see #getPriority()
220      * @since 1.3.0
221      */
222     private void setPriority(final int value) {
223         priority = value;
224     }
225 
226     /**
227      * {@inheritDoc}
228      *
229      * @since 1.3.0
230      */
231     @Override
232     public String toString() {
233         return getClass().getSimpleName() + " [" + super.toString() + ", javaProgram=" + javaProgram + ", priority="
234                 + priority + "]";
235     }
236 }