Skip to content

Commit a8d7c89

Browse files
committed
Removed verifier management and rebased to master
1 parent 2ec9d4d commit a8d7c89

11 files changed

Lines changed: 53 additions & 119 deletions

File tree

src/main/java/org/apache/commons/cli/CommandLine.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public <T> T getParsedOptionValue(final String opt) throws ParseException {
398398
* @since 1.7.0
399399
* @param <T> The return type for the method.
400400
*/
401-
public <T> T getParsedOptionValue(final char opt, T defaultValue) throws ParseException {
401+
public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws ParseException {
402402
return getParsedOptionValue(String.valueOf(opt), defaultValue);
403403
}
404404

@@ -414,7 +414,7 @@ public <T> T getParsedOptionValue(final char opt, T defaultValue) throws ParseEx
414414
* @param <T> The return type for the method.
415415
*/
416416
@SuppressWarnings("unchecked")
417-
public <T> T getParsedOptionValue(final Option option, T defaultValue) throws ParseException {
417+
public <T> T getParsedOptionValue(final Option option, final T defaultValue) throws ParseException {
418418
if (option == null) {
419419
return null;
420420
}
@@ -438,7 +438,7 @@ public <T> T getParsedOptionValue(final Option option, T defaultValue) throws Pa
438438
* @since 1.7.0
439439
* @param <T> The return type for the method.
440440
*/
441-
public <T> T getParsedOptionValue(final String opt, T defaultValue) throws ParseException {
441+
public <T> T getParsedOptionValue(final String opt, final T defaultValue) throws ParseException {
442442
return getParsedOptionValue(resolveOption(opt), defaultValue);
443443
}
444444

src/main/java/org/apache/commons/cli/Option.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public Builder valueSeparator(final char valueSeparator) {
285285
* @return this builder, to allow method chaining.
286286
* @since 1.7.0
287287
*/
288-
public Builder converter(Converter<?> converter) {
288+
public Builder converter(final Converter<?> converter) {
289289
this.converter = converter;
290290
return this;
291291
}
@@ -297,7 +297,7 @@ public Builder converter(Converter<?> converter) {
297297
* @return this builder, to allow method chaining.
298298
* @since 1.7.0
299299
*/
300-
public Builder verifier(Predicate<String> verifier) {
300+
public Builder verifier(final Predicate<String> verifier) {
301301
this.verifier = verifier;
302302
return this;
303303
}
@@ -925,7 +925,7 @@ public Converter<?> getConverter() {
925925
* @param converter The converter to convert the string value to the type.
926926
* @since 1.7.0
927927
*/
928-
public void setConverter(Converter<?> converter) {
928+
public void setConverter(final Converter<?> converter) {
929929
this.converter = converter;
930930
}
931931

@@ -935,15 +935,15 @@ public void setConverter(Converter<?> converter) {
935935
* @since 1.7.0
936936
*/
937937
public Predicate<String> getVerifier() {
938-
return verifier == null ? TypeHandler.getVerifier(type) : verifier;
938+
return verifier == null ? Verifier.DEFAULT : verifier;
939939
}
940940

941941
/**
942942
* Sets the value verifier to verify that a string value is the proper form to be converted to a string.
943943
* @param verifier the Verifier to use.
944944
* @since 1.7.0
945945
*/
946-
public void setVerifier(Predicate<String> verifier) {
946+
public void setVerifier(final Predicate<String> verifier) {
947947
this.verifier = verifier;
948948
}
949949

src/main/java/org/apache/commons/cli/OptionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static Option create(final String opt) throws IllegalArgumentException {
110110
option.setArgs(argCount);
111111
option.setType(type);
112112
option.setConverter(TypeHandler.getConverter(type));
113-
option.setVerifier(TypeHandler.getVerifier(type));
113+
option.setVerifier(Verifier.DEFAULT);
114114
option.setValueSeparator(valueSeparator);
115115
option.setArgName(argName);
116116
} finally {

src/main/java/org/apache/commons/cli/PatternOptionBuilder.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ public class PatternOptionBuilder {
110110
};
111111

112112
static {
113-
TypeHandler.register(PatternOptionBuilder.FILES_VALUE, NOT_IMPLEMENTED, null);
113+
registerTypes();
114+
}
115+
116+
/**
117+
* Registers custom {@code Converter}s with the {@code TypeHandler}.
118+
*/
119+
public static void registerTypes() {
120+
TypeHandler.register(PatternOptionBuilder.FILES_VALUE, NOT_IMPLEMENTED);
114121
}
115122

116123
/**
@@ -204,7 +211,6 @@ public static Options parsePattern(final String pattern) {
204211
} else {
205212
type = getValueType(ch);
206213
converter = TypeHandler.getConverter(getValueType(ch));
207-
verifier = TypeHandler.getVerifier(getValueType(ch));
208214
}
209215
}
210216

src/main/java/org/apache/commons/cli/TypeHandler.java

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2525
import java.util.Date;
2626
import java.util.HashMap;
2727
import java.util.Map;
28-
import java.util.function.Predicate;
2928

3029
/**
3130
* TypeHandler will handle the pluggable conversion and verification of
@@ -46,13 +45,9 @@ public class TypeHandler {
4645

4746
/** Map of classes to converters. */
4847
private static Map<Class<?>, Converter<?>> converterMap = new HashMap<>();
49-
50-
/** Map of classes to verifiers. */
51-
private static Map<Class<?>, Predicate<String>> verifierMap = new HashMap<>();
5248

5349
static {
5450
resetConverters();
55-
resetVerifiers();
5651
}
5752

5853
/**
@@ -93,56 +88,19 @@ public static void noConverters() {
9388
}
9489

9590
/**
96-
* Resets the registered Verifiers to the default state.
97-
* @since 1.7.0
98-
*/
99-
public static void resetVerifiers() {
100-
verifierMap.clear();
101-
verifierMap.put(Object.class, Verifier.CLASS);
102-
verifierMap.put(Class.class, Verifier.CLASS);
103-
verifierMap.put(Number.class, Verifier.NUMBER);
104-
105-
verifierMap.put(Long.class, Verifier.INTEGER);
106-
verifierMap.put(Integer.class, Verifier.INTEGER);
107-
verifierMap.put(Short.class, Verifier.INTEGER);
108-
verifierMap.put(Byte.class, Verifier.INTEGER);
109-
110-
verifierMap.put(Double.class, Verifier.NUMBER);
111-
verifierMap.put(Float.class, Verifier.NUMBER);
112-
verifierMap.put(BigInteger.class, Verifier.INTEGER);
113-
verifierMap.put(BigDecimal.class, Verifier.NUMBER);
114-
}
115-
116-
/**
117-
* Unregisters all Verifiers.
118-
* @since 1.7.0
119-
*/
120-
public static void noVerifiers() {
121-
verifierMap.clear();
122-
}
123-
124-
/**
125-
* Registers a Converter and Verifier for a Class. If @code converter} or
126-
* {@code verifier} are null their respective registrations are cleared for {@code clazz}, and
127-
* defaults will be used in processing.
91+
* Registers a Converter for a Class. If @code converter} is null registration is cleared for {@code clazz}, and
92+
* no converter will be used in processing.
12893
*
12994
* @param clazz the Class to register the Converter and Verifier to.
13095
* @param converter The Converter to associate with Class. May be null.
131-
* @param verifier The Verifier to associate with Class. May be null.
13296
* @since 1.7.0
13397
*/
134-
public static void register(Class<?> clazz, Converter<?> converter, Predicate<String> verifier) {
98+
public static void register(final Class<?> clazz, final Converter<?> converter) {
13599
if (converter == null) {
136100
converterMap.remove(clazz);
137101
} else {
138102
converterMap.put(clazz, converter);
139103
}
140-
141-
if (verifier == null) {
142-
verifierMap.remove(clazz);
143-
} else {
144-
verifierMap.put(clazz, verifier);
145-
}
146104
}
147105

148106
/**
@@ -151,22 +109,11 @@ public static void register(Class<?> clazz, Converter<?> converter, Predicate<St
151109
* @return the registered converter if any, {@link Converter#DEFAULT} otherwise.
152110
* @since 1.7.0
153111
*/
154-
public static Converter<?> getConverter(Class<?> clazz) {
112+
public static Converter<?> getConverter(final Class<?> clazz) {
155113
Converter<?> converter = converterMap.get(clazz);
156114
return converter == null ? Converter.DEFAULT : converter;
157115
}
158116

159-
/**
160-
* Gets the verifier for the Class. Never null.
161-
* @param clazz the Class to get the Verifier for.
162-
* @return the registered verifier if any, {@link Verifier#DEFAULT} otherwise.
163-
* @since 1.7.0
164-
*/
165-
public static Predicate<String> getVerifier(Class<?> clazz) {
166-
Predicate<String> verifier = verifierMap.get(clazz);
167-
return verifier == null ? Verifier.DEFAULT : verifier;
168-
}
169-
170117
/**
171118
* Returns the class whose name is {@code className}.
172119
*

src/main/java/org/apache/commons/cli/Verifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ private Verifier() {
7878
* @param exemplar One of the values from the accepted Enum.
7979
* @return A {@code Predicate<String>} that matches the Enum names.
8080
*/
81-
public static Predicate<String> enumVerifier(Enum<?> exemplar) {
81+
public static Predicate<String> enumVerifier(final Enum<?> exemplar) {
8282
return new Predicate<String>() {
8383
/** The list of valid names */
8484
private final List<String> names = Arrays.stream(exemplar.getDeclaringClass().getEnumConstants())
8585
.map(t -> ((Enum<?>) t).name()).collect(Collectors.toList());
8686
@Override
87-
public boolean test(String str) throws RuntimeException {
87+
public boolean test(final String str) throws RuntimeException {
8888
return names.contains(str);
8989
}
9090
};

src/test/java/org/apache/commons/cli/ConverterTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ConverterTests {
3838

3939
@ParameterizedTest
4040
@MethodSource("numberTestParameters")
41-
public void numberTests(String str, Number expected) throws Exception {
41+
public void numberTests(final String str, final Number expected) throws Exception {
4242
if (expected != null) {
4343
assertEquals(expected, Converter.NUMBER.apply(str));
4444
} else {
@@ -72,7 +72,7 @@ public void classTests() throws Exception {
7272
assertNotNull(Converter.CLASS.apply(this.getClass().getTypeName()), this.getClass().getTypeName());
7373

7474
assertThrows(ClassNotFoundException.class, () -> Converter.CLASS.apply("foo.bar"));
75-
assertNotNull(Converter.CLASS.apply(TestClass.class.getName()));
75+
assertNotNull(Converter.CLASS.apply(AClassWithoutADefaultConstructor.class.getName()));
7676
}
7777

7878
@Test
@@ -84,7 +84,7 @@ public void objectTests() throws Exception {
8484
assertNotNull(Converter.OBJECT.apply(this.getClass().getTypeName()), this.getClass().getTypeName());
8585

8686
assertThrows(ClassNotFoundException.class, () -> Converter.OBJECT.apply("foo.bar"));
87-
assertThrows(NoSuchMethodException.class, () -> Converter.OBJECT.apply(TestClass.class.getName()));
87+
assertThrows(NoSuchMethodException.class, () -> Converter.OBJECT.apply(AClassWithoutADefaultConstructor.class.getName()));
8888
}
8989

9090
@Test
@@ -111,8 +111,8 @@ public void fileTests() throws Exception {
111111
}
112112

113113
// A class without a default constructor.
114-
public class TestClass {
115-
public TestClass(int i) {
114+
public class AClassWithoutADefaultConstructor {
115+
public AClassWithoutADefaultConstructor(final int i) {
116116
}
117117
}
118118
}

src/test/java/org/apache/commons/cli/OptionTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void testSubclass() {
239239
}
240240

241241

242-
private Option roundTrip(Option o) throws IOException, ClassNotFoundException {
242+
private Option roundTrip(final Option o) throws IOException, ClassNotFoundException {
243243
ByteArrayOutputStream baos = new ByteArrayOutputStream();
244244
ObjectOutputStream oos = new ObjectOutputStream(baos);
245245
oos.writeObject(o);
@@ -267,16 +267,16 @@ public void testSerialization() throws IOException, ClassNotFoundException {
267267

268268
// verify registered class converters and verifiers do not get reset to default.
269269
try {
270-
TypeHandler.register(TypeHandlerTest.Instantiable.class, Converter.URL, Verifier.CLASS);
270+
TypeHandler.register(TypeHandlerTest.Instantiable.class, Converter.URL);
271271
// verify earlier values still set.
272272
assertEquals(Converter.DATE, o.getConverter());
273273
assertEquals(Verifier.NUMBER, o.getVerifier());
274274
o2 = roundTrip(o);
275-
// verify set to registered values
275+
// verify set to registered value
276276
assertEquals(Converter.URL, o2.getConverter());
277-
assertEquals(Verifier.CLASS, o2.getVerifier());
277+
assertEquals(Verifier.DEFAULT, o2.getVerifier());
278278
} finally {
279-
TypeHandler.register(TypeHandlerTest.Instantiable.class, null, null);
279+
TypeHandler.register(TypeHandlerTest.Instantiable.class, null);
280280
}
281281
}
282282
}

src/test/java/org/apache/commons/cli/PatternOptionBuilderTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2323
import static org.junit.Assert.assertNull;
2424
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
26-
import static org.junit.jupiter.api.Assertions.assertThrows;
2726

2827
import java.io.File;
2928
import java.io.FileInputStream;
@@ -32,13 +31,19 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3231
import java.util.Date;
3332
import java.util.Vector;
3433

34+
import org.junit.BeforeClass;
3535
import org.junit.Test;
3636

3737
/**
3838
* Test case for the PatternOptionBuilder class.
3939
*/
4040
@SuppressWarnings("deprecation") // tests some deprecated classes
4141
public class PatternOptionBuilderTest {
42+
@BeforeClass
43+
public static void setup() {
44+
PatternOptionBuilder.registerTypes();
45+
}
46+
4247
@Test
4348
public void testClassPattern() throws Exception {
4449
final Options options = PatternOptionBuilder.parsePattern("c+d+");
@@ -81,16 +86,16 @@ public void testNumberPattern() throws Exception {
8186
final Options options = PatternOptionBuilder.parsePattern("n%d%x%");
8287
final CommandLineParser parser = new PosixParser();
8388
// 3,5 fails validation.
84-
assertThrows(ParseException.class, () -> parser.parse(options, new String[] {"-n", "1", "-d", "2.1", "-x", "3,5"}));
89+
//assertThrows(ParseException.class, () -> parser.parse(options, new String[] {"-n", "1", "-d", "2.1", "-x", "3,5"}));
8590

86-
CommandLine line = parser.parse(options, new String[] {"-n", "1", "-d", "2.1", "-x", "3.5"});
91+
CommandLine line = parser.parse(options, new String[] {"-n", "1", "-d", "2.1", "-x", "3,5"});
8792
assertEquals("n object class", Long.class, line.getOptionObject("n").getClass());
8893
assertEquals("n value", Long.valueOf(1), line.getOptionObject("n"));
8994

9095
assertEquals("d object class", Double.class, line.getOptionObject("d").getClass());
9196
assertEquals("d value", Double.valueOf(2.1), line.getOptionObject("d"));
9297

93-
assertEquals("x object", Double.valueOf(3.5), line.getOptionObject("x"));
98+
assertNull("x object", line.getOptionObject("x"));
9499
}
95100

96101
@Test

0 commit comments

Comments
 (0)