Skip to content

Commit c56d473

Browse files
committed
Remove trailing whitespace
- Use final - Reduce nesting - Use method references
1 parent 6a64704 commit c56d473

10 files changed

Lines changed: 57 additions & 58 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public String getOptionValue(final Option option, final Supplier<String> default
309309
final String answer = getOptionValue(option);
310310
return answer != null ? answer : defaultValue.get();
311311
}
312-
312+
313313
/**
314314
* Gets the first argument, if any, of this option.
315315
*
@@ -343,7 +343,7 @@ public String getOptionValue(final String opt, final Supplier<String> defaultVal
343343
return getOptionValue(resolveOption(opt), defaultValue);
344344
}
345345

346-
346+
347347
/**
348348
* Gets the array of values, if any, of an option.
349349
*
@@ -425,7 +425,7 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P
425425
public <T> T getParsedOptionValue(final Option option) throws ParseException {
426426
return getParsedOptionValue(option, null);
427427
}
428-
428+
429429
/**
430430
* Gets a version of this {@code Option} converted to a particular type.
431431
*
@@ -446,7 +446,7 @@ public <T> T getParsedOptionValue(final Option option, final T defaultValue) thr
446446

447447
try {
448448
return res == null ? defaultValue : (T) option.getConverter().apply(res);
449-
} catch (Exception e) {
449+
} catch (final Exception e) {
450450
throw ParseException.wrap(e);
451451
}
452452
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public interface Converter<T> {
3636
Converter<?> DEFAULT = s -> s;
3737

3838
/** Class name converter. Calls {@code Class.forName}. */
39-
Converter<Class<?>> CLASS = s -> Class.forName(s);
39+
Converter<Class<?>> CLASS = Class::forName;
4040

4141
/** File name converter. Calls @{code new File(s)} */
42-
Converter<File> FILE = s -> new File(s);
42+
Converter<File> FILE = File::new;
4343

4444
/** Path converter. Calls @{code new Path(s)} */
4545
Converter<Path> PATH = s -> new File(s).toPath();
@@ -63,7 +63,7 @@ public interface Converter<T> {
6363
Converter<Object> OBJECT = s -> CLASS.apply(s).getConstructor().newInstance();
6464

6565
/** Creates a URL. Calls {@code new URL(s)}. */
66-
Converter<URL> URL = s -> new URL(s);
66+
Converter<URL> URL = java.net.URL::new;
6767

6868
/** Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy". */
6969
Converter<Date> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static final class Builder {
8282

8383
/** The character that is the value separator */
8484
private char valueSeparator;
85-
85+
8686
/** The converter to convert to type **/
8787
private Converter<?> converter;
8888

@@ -285,7 +285,7 @@ public Builder valueSeparator(final char valueSeparator) {
285285
this.valueSeparator = valueSeparator;
286286
return this;
287287
}
288-
288+
289289
}
290290

291291
/** Specifies the number of argument values has not been specified */
@@ -351,7 +351,7 @@ public static Builder builder(final String option) {
351351

352352
/** The character that is the value separator. */
353353
private char valuesep;
354-
354+
355355
/** The explicit converter for this option. May be null */
356356
private transient Converter<?> converter;
357357

@@ -724,7 +724,7 @@ private boolean hasNoValues() {
724724

725725
/**
726726
* Returns whether this Option can have an optional argument.
727-
*
727+
*
728728
* @return whether this Option can have an optional argument
729729
*/
730730
public boolean hasOptionalArg() {
@@ -880,7 +880,7 @@ public void setRequired(final boolean required) {
880880
public void setType(final Class<?> type) {
881881
this.type = type;
882882
}
883-
883+
884884
/**
885885
* Sets the type of this Option.
886886
* <p>

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public class ParseException extends Exception {
2929

3030
/**
3131
* Converts any exception except {@code UnsupportedOperationException} to a {@code ParseException}.
32-
* if {@code e} is an instance of {@code ParseException} it is returned, otherwise a {@code ParseException} is
32+
* if {@code e} is an instance of {@code ParseException} it is returned, otherwise a {@code ParseException} is
3333
* created that wraps it.
3434
* <p>
35-
* Note: {@code UnsupportedOperationException} are not wrapped. This is to solve a legacy expected exception problem and will be
36-
* removed in the future.</p>
35+
* Note: {@code UnsupportedOperationException} are not wrapped. This is to solve a legacy expected exception problem and will be
36+
* removed in the future.</p>
3737
* @param e the exception to convert.
3838
* @return the ParseException.
3939
* @throws UnsupportedOperationException due to legacy expectations. Will be removed in the future.
@@ -50,13 +50,13 @@ public static ParseException wrap(final Exception e) throws UnsupportedOperation
5050
return new ParseException(e);
5151
}
5252
/**
53-
* Constructs a new {@code ParseException} wrapping the specified exception.
53+
* Constructs a new {@code ParseException} wrapping the specified exception.
5454
* @param e the Exception to wrap.
5555
*/
5656
public ParseException(final Exception e) {
5757
super(e);
5858
}
59-
59+
6060
/**
6161
* Constructs a new {@code ParseException} with the specified detail message.
6262
*

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ public class PatternOptionBuilder {
102102

103103
/** URL class */
104104
public static final Class<URL> URL_VALUE = URL.class;
105-
105+
106106
/** The converter to use for Unimplemented data types */
107-
static final Converter<?> NOT_IMPLEMENTED = s -> {
107+
static final Converter<?> NOT_IMPLEMENTED = s -> {
108108
throw new UnsupportedOperationException("Not yet implemented");
109109
};
110-
110+
111111
static {
112112
registerTypes();
113113
}
@@ -155,7 +155,7 @@ public static Class<?> getValueType(final char ch) {
155155

156156
return null;
157157
}
158-
158+
159159
/**
160160
* Returns whether {@code ch} is a value code, i.e. whether it represents a class in a pattern.
161161
*
@@ -217,7 +217,7 @@ public static Options parsePattern(final String pattern) {
217217
}
218218

219219
/**
220-
* Registers custom {@code Converter}s with the {@code TypeHandler}.
220+
* Registers custom {@code Converter}s with the {@code TypeHandler}.
221221
* @since 1.7.0
222222
*/
223223
public static void registerTypes() {

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2828
import java.util.Map;
2929

3030
/**
31-
* TypeHandler will handle the pluggable conversion and verification of
31+
* TypeHandler will handle the pluggable conversion and verification of
3232
* Option types. It handles the mapping of classes to bot converters and verifiers.
3333
* It provides the default conversion and verification methods when converters and verifiers
3434
* are not explicitly set.
3535
* <p>
36-
* If Options are serialized and deserialized their converters and verifiers will revert to the
36+
* If Options are serialized and deserialized their converters and verifiers will revert to the
3737
* defaults defined in this class. To correctly de-serialize Options with custom converters and/or
3838
* verifiers, using the default serialization methods, this class should be properly configured with the custom
3939
* converters and verifiers for the specific class.
4040
* </p>
4141
*/
4242
public class TypeHandler {
43-
43+
4444
/** Value of hex conversion of strings */
4545
private static final int HEX_RADIX = 16;
4646

@@ -50,7 +50,7 @@ public class TypeHandler {
5050
static {
5151
resetConverters();
5252
}
53-
53+
5454
/**
5555
* Returns the class whose name is {@code className}.
5656
*
@@ -63,7 +63,7 @@ public class TypeHandler {
6363
public static Class<?> createClass(final String className) throws ParseException {
6464
return createValue(className, Class.class);
6565
}
66-
66+
6767
/**
6868
* Returns the date represented by {@code str}.
6969
* <p>
@@ -77,7 +77,7 @@ public static Class<?> createClass(final String className) throws ParseException
7777
public static Date createDate(final String str) {
7878
try {
7979
return createValue(str, Date.class);
80-
} catch (ParseException e) {
80+
} catch (final ParseException e) {
8181
throw new RuntimeException(e);
8282
}
8383
}
@@ -93,7 +93,7 @@ public static Date createDate(final String str) {
9393
public static File createFile(final String str) {
9494
try {
9595
return createValue(str, File.class);
96-
} catch (ParseException e) {
96+
} catch (final ParseException e) {
9797
throw new RuntimeException(e);
9898
}
9999
}
@@ -166,7 +166,7 @@ public static URL createURL(final String str) throws ParseException {
166166
public static <T> T createValue(final String str, final Class<T> clazz) throws ParseException {
167167
try {
168168
return (T) getConverter(clazz).apply(str);
169-
} catch (Exception e) {
169+
} catch (final Exception e) {
170170
throw ParseException.wrap(e);
171171
}
172172
}
@@ -192,7 +192,7 @@ public static Object createValue(final String str, final Object obj) throws Pars
192192
* @since 1.7.0
193193
*/
194194
public static Converter<?> getConverter(final Class<?> clazz) {
195-
Converter<?> converter = converterMap.get(clazz);
195+
final Converter<?> converter = converterMap.get(clazz);
196196
return converter == null ? Converter.DEFAULT : converter;
197197
}
198198

@@ -218,9 +218,9 @@ public static FileInputStream openFile(final String str) throws ParseException {
218218
}
219219

220220
/**
221-
* Registers a Converter for a Class. If @code converter} is null registration is cleared for {@code clazz}, and
221+
* Registers a Converter for a Class. If @code converter} is null registration is cleared for {@code clazz}, and
222222
* no converter will be used in processing.
223-
*
223+
*
224224
* @param clazz the Class to register the Converter and Verifier to.
225225
* @param converter The Converter to associate with Class. May be null.
226226
* @since 1.7.0
@@ -246,20 +246,19 @@ public static void resetConverters() {
246246
converterMap.put(Path.class, Converter.PATH);
247247
converterMap.put(Number.class, Converter.NUMBER);
248248
converterMap.put(URL.class, Converter.URL);
249-
converterMap.put(FileInputStream.class, s -> new FileInputStream(s));
249+
converterMap.put(FileInputStream.class, FileInputStream::new);
250250
converterMap.put(Long.class, Long::parseLong);
251251
converterMap.put(Integer.class, Integer::parseInt);
252252
converterMap.put(Short.class, Short::parseShort);
253253
converterMap.put(Byte.class, Byte::parseByte);
254254
converterMap.put(Character.class, s -> {
255255
if (s.startsWith("\\u")) {
256256
return Character.toChars(Integer.parseInt(s.substring(2), HEX_RADIX))[0];
257-
} else {
258-
return s.charAt(0);
259-
} });
257+
}
258+
return s.charAt(0); });
260259
converterMap.put(Double.class, Double::parseDouble);
261260
converterMap.put(Float.class, Float::parseFloat);
262-
converterMap.put(BigInteger.class, s -> new BigInteger(s));
263-
converterMap.put(BigDecimal.class, s -> new BigDecimal(s));
261+
converterMap.put(BigInteger.class, BigInteger::new);
262+
converterMap.put(BigDecimal.class, BigDecimal::new);
264263
}
265264
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public AClassWithoutADefaultConstructor(final int i) {
4343
}
4444

4545
private static Stream<Arguments> numberTestParameters() {
46-
List<Arguments> lst = new ArrayList<>();
46+
final List<Arguments> lst = new ArrayList<>();
4747

4848
lst.add(Arguments.of("123", Long.valueOf("123")));
4949
lst.add(Arguments.of("12.3", Double.valueOf("12.3")));
@@ -75,16 +75,16 @@ public void classTests() throws Exception {
7575
public void dateTests() throws Exception {
7676
assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("whatever"));
7777

78-
Date d = new Date(1023400137000L);
78+
final Date d = new Date(1023400137000L);
7979
assertEquals(d, Converter.DATE.apply("Thu Jun 06 17:48:57 EDT 2002"));
8080

8181
assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("Jun 06 17:48:57 EDT 2002"));
8282
}
8383

8484
@Test
8585
public void fileTests() throws Exception {
86-
URL url = this.getClass().getClassLoader().getResource("./org/apache/commons/cli/existing-readable.file");
87-
String fileName = url.toString().substring("file:".length());
86+
final URL url = this.getClass().getClassLoader().getResource("./org/apache/commons/cli/existing-readable.file");
87+
final String fileName = url.toString().substring("file:".length());
8888
assertNotNull(Converter.FILE.apply(fileName));
8989
}
9090

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ private static void checkOption(final Option option, final String opt, final Str
8080
}
8181

8282
private Option roundTrip(final Option o) throws IOException, ClassNotFoundException {
83-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
84-
ObjectOutputStream oos = new ObjectOutputStream(baos);
83+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
84+
final ObjectOutputStream oos = new ObjectOutputStream(baos);
8585
oos.writeObject(o);
86-
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
87-
ObjectInputStream ois = new ObjectInputStream(bais);
86+
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
87+
final ObjectInputStream ois = new ObjectInputStream(bais);
8888
return (Option) ois.readObject();
8989
}
9090

@@ -243,7 +243,7 @@ public void testHashCode() {
243243
@Test
244244
public void testSerialization() throws IOException, ClassNotFoundException {
245245

246-
Option o = Option.builder("o").type(TypeHandlerTest.Instantiable.class).build();
246+
final Option o = Option.builder("o").type(TypeHandlerTest.Instantiable.class).build();
247247
assertEquals(Converter.DEFAULT, o.getConverter());
248248
Option o2 = roundTrip(o);
249249
assertEquals(Converter.DEFAULT, o2.getConverter());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void testNumberPattern() throws Exception {
8888
// 3,5 fails validation.
8989
//assertThrows(ParseException.class, () -> parser.parse(options, new String[] {"-n", "1", "-d", "2.1", "-x", "3,5"}));
9090

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

0 commit comments

Comments
 (0)