1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
37
38
39
40
41
42
43
44 public class IntegerOptionArgumentImpl extends AbstractOptionArgument < Integer > {
45
46
47 private static final long serialVersionUID = -3189420770554605677L;
48
49
50 private static final String MAIN_USAGE = "INTEGER";
51
52
53 private static final String USAGE_DESCRIPTION = MAIN_USAGE + " specifies an integer, positive or negative.";
54
55
56
57
58
59
60
61
62 public IntegerOptionArgumentImpl(final boolean optionArgumentIsMandatory) {
63 super(optionArgumentIsMandatory, new StringBuilder(USAGE_DESCRIPTION));
64 }
65
66
67
68
69
70
71 @Override
72 protected String getMainUsageDescription() {
73 return MAIN_USAGE;
74 }
75
76
77
78
79
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 }