22
22
import java .math .BigInteger ;
23
23
import java .util .List ;
24
24
import java .util .ArrayList ;
25
-
26
25
import com .github .sttk .exception .ReasonedException ;
27
26
28
27
/**
@@ -145,11 +144,11 @@ public <T> OptCfg(
145
144
) {
146
145
var init = new Init <T >();
147
146
init .storeKey = storeKey ;
148
- init .names = unmodifiableList ( names ) ;
147
+ init .names = names ;
149
148
init .hasArg = hasArg ;
150
149
init .isArray = isArray ;
151
150
init .type = type ;
152
- init .defaults = unmodifiableList ( defaults ) ;
151
+ init .defaults = defaults ;
153
152
init .desc = desc ;
154
153
init .argInHelp = argInHelp ;
155
154
init .converter = converter ;
@@ -162,7 +161,8 @@ public <T> OptCfg(
162
161
this .hasArg = init .hasArg ;
163
162
this .isArray = init .isArray ;
164
163
this .type = init .type ;
165
- this .defaults = unmodifiableList (init .defaults );
164
+ this .defaults = (init .defaults == null ) ? null :
165
+ unmodifiableList (init .defaults );
166
166
this .desc = init .desc ;
167
167
this .argInHelp = init .argInHelp ;
168
168
this .converter = init .converter ;
@@ -201,14 +201,14 @@ public <T> OptCfg(NamedParam<T> ...params) {
201
201
this .hasArg = init .hasArg ;
202
202
this .isArray = init .isArray ;
203
203
this .type = init .type ;
204
- this .defaults = unmodifiableList (init .defaults );
204
+ this .defaults = (init .defaults == null ) ? null :
205
+ unmodifiableList (init .defaults );
205
206
this .desc = init .desc ;
206
207
this .argInHelp = init .argInHelp ;
207
208
this .converter = init .converter ;
208
209
this .postparser = init .postparser ;
209
210
}
210
211
211
- @ SuppressWarnings ("unchecked" )
212
212
private void fillmissing (Init <?> init ) {
213
213
if (isEmpty (init .storeKey )) {
214
214
if (! isEmpty (init .names )) {
@@ -224,38 +224,65 @@ private void fillmissing(Init<?> init) {
224
224
}
225
225
}
226
226
227
+ if (init .names == null ) {
228
+ init .names = emptyList ();
229
+ }
230
+
227
231
if (init .type != null && ! init .hasArg ) {
228
- init .hasArg = true ;
232
+ if (init .type != boolean .class && init .type != Boolean .class ) {
233
+ init .hasArg = true ;
234
+ }
229
235
}
230
236
231
237
if (init .type != null && init .converter == null ) {
232
- var type = init .type ;
233
- if (type .equals (int .class ) || type .equals (Integer .class )) {
234
- init .converter = (Converter )new IntConverter ();
235
- } else if (type .equals (double .class ) || type .equals (Double .class )) {
236
- init .converter = (Converter )new DoubleConverter ();
237
- } else if (type .equals (long .class ) || type .equals (Long .class )) {
238
- init .converter = (Converter )new LongConverter ();
239
- } else if (type .equals (BigDecimal .class )) {
240
- init .converter = (Converter )new BigDecimalConverter ();
241
- } else if (type .equals (BigInteger .class )) {
242
- init .converter = (Converter )new BigIntConverter ();
243
- } else if (type .equals (float .class ) || type .equals (Float .class )) {
244
- init .converter = (Converter )new FloatConverter ();
245
- } else if (type .equals (short .class ) || type .equals (Short .class )) {
246
- init .converter = (Converter )new ShortConverter ();
247
- } else if (type .equals (byte .class ) || type .equals (Byte .class )) {
248
- init .converter = (Converter )new ByteConverter ();
238
+ var converter = findConverter (init .type );
239
+ if (converter != null ) {
240
+ setInitConverter (init , converter );
249
241
}
250
242
}
251
243
}
252
244
245
+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
246
+ private void setInitConverter (Init init , Converter <?> converter ) {
247
+ init .converter = (Converter ) converter ;
248
+ }
249
+
250
+ @ SuppressWarnings ("unchecked" )
251
+ static <T > Converter <T > findConverter (Class <T > type ) {
252
+ if (type .equals (int .class ) || type .equals (Integer .class )) {
253
+ var c = (Converter <T >) new IntConverter ();
254
+ return c ;
255
+ } else if (type .equals (double .class ) || type .equals (Double .class )) {
256
+ var c = (Converter <T >) new DoubleConverter ();
257
+ return c ;
258
+ } else if (type .equals (long .class ) || type .equals (Long .class )) {
259
+ var c = (Converter <T >) new LongConverter ();
260
+ return c ;
261
+ } else if (type .equals (BigDecimal .class )) {
262
+ var c = (Converter <T >) new BigDecimalConverter ();
263
+ return c ;
264
+ } else if (type .equals (BigInteger .class )) {
265
+ var c = (Converter <T >) new BigIntConverter ();
266
+ return c ;
267
+ } else if (type .equals (float .class ) || type .equals (Float .class )) {
268
+ var c = (Converter <T >) new FloatConverter ();
269
+ return c ;
270
+ } else if (type .equals (short .class ) || type .equals (Short .class )) {
271
+ var c = (Converter <T >) new ShortConverter ();
272
+ return c ;
273
+ } else if (type .equals (byte .class ) || type .equals (Byte .class )) {
274
+ var c = (Converter <T >) new ByteConverter ();
275
+ return c ;
276
+ }
277
+ return null ;
278
+ }
279
+
253
280
private static class Init <T > {
254
281
String storeKey ;
255
- List <String > names = emptyList () ;
282
+ List <String > names ;
256
283
boolean hasArg ;
257
284
boolean isArray ;
258
- List <T > defaults = emptyList () ;
285
+ List <T > defaults ;
259
286
Class <T > type ;
260
287
String desc ;
261
288
String argInHelp ;
0 commit comments