Skip to content

Commit b3dd961

Browse files
committed
Refactor common code
1 parent 4f35142 commit b3dd961

5 files changed

Lines changed: 15 additions & 6 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ private void handleProperties(final Properties properties) throws ParseException
449449
final String value = properties.getProperty(option);
450450

451451
if (opt.hasArg()) {
452-
if (opt.getValues() == null || opt.getValues().length == 0) {
452+
if (Util.isEmpty(opt.getValues())) {
453453
opt.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
454454
}
455455
} else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ public void printHelp(final PrintWriter pw, final int width, final String cmdLin
580580
*/
581581
public void printHelp(final PrintWriter pw, final int width, final String cmdLineSyntax, final String header, final Options options, final int leftPad,
582582
final int descPad, final String footer, final boolean autoUsage) {
583-
if (cmdLineSyntax == null || cmdLineSyntax.isEmpty()) {
583+
if (Util.isEmpty(cmdLineSyntax)) {
584584
throw new IllegalArgumentException("cmdLineSyntax not provided");
585585
}
586586
if (autoUsage) {
@@ -891,7 +891,7 @@ private Appendable renderWrappedTextBlock(final StringBuffer sb, final int width
891891
* @return The String of without the trailing padding
892892
*/
893893
protected String rtrim(final String s) {
894-
if (s == null || s.isEmpty()) {
894+
if (Util.isEmpty(s)) {
895895
return s;
896896
}
897897
int pos = s.length();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,6 @@ public String getValue(final int index) throws IndexOutOfBoundsException {
684684
*/
685685
public String getValue(final String defaultValue) {
686686
final String value = getValue();
687-
688687
return value != null ? value : defaultValue;
689688
}
690689

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ protected void processProperties(final Properties properties) throws ParseExcept
278278
// get the value from the properties instance
279279
final String value = properties.getProperty(option);
280280
if (opt.hasArg()) {
281-
if (opt.getValues() == null || opt.getValues().length == 0) {
281+
if (Util.isEmpty(opt.getValues())) {
282282
try {
283283
opt.processValue(value);
284284
} catch (final RuntimeException exp) { // NOPMD

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,23 @@ final class Util {
2727
*/
2828
static final String[] EMPTY_STRING_ARRAY = {};
2929

30+
/**
31+
* Tests whether the given array is null or empty.
32+
*
33+
* @param array the array to test.
34+
* @return the given array is null or empty.
35+
*/
36+
static boolean isEmpty(final Object[] array) {
37+
return array == null || array.length == 0;
38+
}
39+
3040
/**
3141
* Tests whether the given string is null or empty.
3242
*
3343
* @param str The string to test.
3444
* @return Whether the given string is null or empty.
3545
*/
36-
private static boolean isEmpty(final String str) {
46+
static boolean isEmpty(final String str) {
3747
return str == null || str.isEmpty();
3848
}
3949

0 commit comments

Comments
 (0)