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.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
39
40
41
42
43
44
45
46 public class IntegerIntervalOptionArgumentImpl extends AbstractOptionArgument < Integer > {
47
48
49 private static final long serialVersionUID = -1321717914491985645L;
50
51
52 private static final String MAIN_USAGE = "INTEGER";
53
54
55 private static final String USAGE_DESCRIPTION = MAIN_USAGE + " specifies an integer within an interval, between ";
56
57
58 protected static final String AND_STRING = " and ";
59
60
61
62
63
64 private Integer maximum;
65
66
67
68
69
70 private Integer minimum;
71
72
73
74
75
76
77
78
79
80
81
82
83
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
93
94
95
96
97
98
99
100
101
102
103
104
105
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
116
117
118
119 @Override
120 protected String getMainUsageDescription() {
121 return MAIN_USAGE;
122 }
123
124
125
126
127
128
129
130
131 private Integer getMaximum() {
132 return maximum;
133 }
134
135
136
137
138
139
140
141
142 private Integer getMinimum() {
143 return minimum;
144 }
145
146
147
148
149
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
177
178
179
180
181
182
183 private void setMaximum(final Integer value) {
184 maximum = value;
185 }
186
187
188
189
190
191
192
193
194
195 private void setMinimum(final Integer value) {
196 minimum = value;
197 }
198 }