1 /*
2 IntegerIntervalOptionArgumentImpl.java
3 Creation date : 20/10/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.constants.CharacterConstants.DOT_CHAR;
29 import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
30 import static net.sourceforge.plantumldependency.common.utils.string.StringUtils.isNotEmpty;
31 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.EMPTY_OPTION_ARGUMENT_ERROR;
32 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.INTEGER_OUT_OF_RANGE_ARGUMENT_ERROR;
33 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.NOT_INTEGER_ARGUMENT_ERROR;
34 import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
35 import net.sourceforge.plantumldependency.commoncli.option.argument.AbstractOptionArgument;
36
37 /**
38 * The integer interval implementation of the
39 * {@link net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument} interface,
40 * specifying an {@link Integer} argument within a interval.
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 IntegerIntervalOptionArgumentImpl extends AbstractOptionArgument < Integer > {
47
48 /** Serial version UID. */
49 private static final long serialVersionUID = -1321717914491985645L;
50
51 /** The argument main usage constant. */
52 private static final String MAIN_USAGE = "INTEGER";
53
54 /** The argument main usage description constant. */
55 private static final String USAGE_DESCRIPTION = MAIN_USAGE + " specifies an integer within an interval, between ";
56
57 /** The and string constant. */
58 protected static final String AND_STRING = " and ";
59
60 /**
61 * The maximum {@link Integer} to accept, including it, must be higher than <code>minimum</code>
62 * .
63 */
64 private Integer maximum;
65
66 /**
67 * The minimum {@link Integer} to accept, including it, must be lower than <code>maximum</code>
68 * .
69 */
70 private Integer minimum;
71
72 /**
73 * Default constructor.
74 *
75 * @param min
76 * the minimum {@link Integer} to accept, including it, must be lower than
77 * <code>maximum</code>.
78 * @param max
79 * the maximum {@link Integer} to accept, including it, must be higher than
80 * <code>minimum</code>.
81 * @param optionArgumentIsMandatory
82 * <code>true</code> if the argument is mandatory, <code>false</code> otherwise.
83 * @since 1.3.0
84 */
85 public IntegerIntervalOptionArgumentImpl(final Integer min, final Integer max,
86 final boolean optionArgumentIsMandatory) {
87 this(min, max, optionArgumentIsMandatory, new StringBuilder(USAGE_DESCRIPTION + min + AND_STRING + max
88 + DOT_CHAR));
89 }
90
91 /**
92 * Default constructor.
93 *
94 * @param min
95 * the minimum {@link Integer} to accept, including it, must be lower than
96 * <code>maximum</code>.
97 * @param max
98 * the maximum {@link Integer} to accept, including it, must be higher than
99 * <code>minimum</code>.
100 * @param optionArgumentIsMandatory
101 * <code>true</code> if the argument is mandatory, <code>false</code> otherwise.
102 * @param fullArgumentDescription
103 * the full argument usage description, explaining what the argument does (used for
104 * helping message). <i>Note : a new {@link StringBuilder} is created.</i>
105 * @since 1.3.0
106 */
107 public IntegerIntervalOptionArgumentImpl(final Integer min, final Integer max,
108 final boolean optionArgumentIsMandatory, final StringBuilder fullArgumentDescription) {
109 super(optionArgumentIsMandatory, fullArgumentDescription);
110 setMaximum(max);
111 setMinimum(min);
112 }
113
114 /**
115 * {@inheritDoc}
116 *
117 * @since 1.3.0
118 */
119 @Override
120 protected String getMainUsageDescription() {
121 return MAIN_USAGE;
122 }
123
124 /**
125 * Gets the value of <code>maximum</code>.
126 *
127 * @return the value of <code>maximum</code>.
128 * @see #setMaximum(Integer)
129 * @since 1.3.0
130 */
131 private Integer getMaximum() {
132 return maximum;
133 }
134
135 /**
136 * Gets the value of <code>minimum</code>.
137 *
138 * @return the value of <code>minimum</code>.
139 * @see #setMinimum(Integer)
140 * @since 1.3.0
141 */
142 private Integer getMinimum() {
143 return minimum;
144 }
145
146 /**
147 * {@inheritDoc}
148 *
149 * @since 1.3.0
150 */
151 @Override
152 public Integer parseArgument(final String argument) throws CommandLineException {
153 Integer integer = null;
154
155 if (isNotEmpty(argument)) {
156 try {
157 integer = valueOf(argument);
158 if (integer.compareTo(getMinimum()) < 0) {
159 throw new CommandLineException(buildLogString(INTEGER_OUT_OF_RANGE_ARGUMENT_ERROR, new Object[] {
160 argument, getMinimum(), getMaximum()}));
161 } else if (integer.compareTo(getMaximum()) > 0) {
162 throw new CommandLineException(buildLogString(INTEGER_OUT_OF_RANGE_ARGUMENT_ERROR, new Object[] {
163 argument, getMinimum(), getMaximum()}));
164 }
165 } catch (final NumberFormatException e) {
166 throw new CommandLineException(buildLogString(NOT_INTEGER_ARGUMENT_ERROR, argument), e);
167 }
168 } else {
169 throw new CommandLineException(EMPTY_OPTION_ARGUMENT_ERROR);
170 }
171
172 return integer;
173 }
174
175 /**
176 * Sets the value of <code>maximum</code>.
177 *
178 * @param value
179 * the <code>maximum</code> to set, can be <code>null</code>.
180 * @see #getMaximum()
181 * @since 1.3.0
182 */
183 private void setMaximum(final Integer value) {
184 maximum = value;
185 }
186
187 /**
188 * Sets the value of <code>minimum</code>.
189 *
190 * @param value
191 * the <code>minimum</code> to set, can be <code>null</code>.
192 * @see #getMinimum()
193 * @since 1.3.0
194 */
195 private void setMinimum(final Integer value) {
196 minimum = value;
197 }
198 }