Skip to content

Commit 9b101bf

Browse files
committed
Better use of toArray()
1 parent 6405687 commit 9b101bf

2 files changed

Lines changed: 9 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717

1818
package org.apache.commons.cli;
1919

20+
import static org.apache.commons.cli.Util.EMPTY_STRING_ARRAY;
21+
2022
import java.io.Serializable;
2123
import java.util.ArrayList;
22-
import java.util.Collection;
2324
import java.util.Iterator;
2425
import java.util.LinkedList;
2526
import java.util.List;
2627
import java.util.Properties;
2728

28-
import static org.apache.commons.cli.Util.EMPTY_STRING_ARRAY;
29-
3029
/**
3130
* Represents list of arguments parsed against a {@link Options} descriptor.
3231
* <p>
@@ -125,11 +124,7 @@ public List<String> getArgList() {
125124
* @return remaining items passed in but not parsed as an array.
126125
*/
127126
public String[] getArgs() {
128-
final String[] answer = new String[args.size()];
129-
130-
args.toArray(answer);
131-
132-
return answer;
127+
return args.toArray(Util.EMPTY_STRING_ARRAY);
133128
}
134129

135130
/**
@@ -225,13 +220,7 @@ public Properties getOptionProperties(final String opt) {
225220
* @return an array of the processed {@link Option}s.
226221
*/
227222
public Option[] getOptions() {
228-
final Collection<Option> processed = options;
229-
230-
// reinitialize array
231-
final Option[] optionsArray = new Option[processed.size()];
232-
233-
// return the array
234-
return processed.toArray(optionsArray);
223+
return options.toArray(Option.EMPTY_ARRAY);
235224
}
236225

237226
/**

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717

1818
package org.apache.commons.cli;
1919

20+
import static org.apache.commons.cli.Util.EMPTY_STRING_ARRAY;
21+
2022
import java.io.Serializable;
2123
import java.util.ArrayList;
2224
import java.util.List;
2325
import java.util.Objects;
2426

25-
import static org.apache.commons.cli.Util.EMPTY_STRING_ARRAY;
26-
2727
/**
2828
* Describes a single command-line option. It maintains information regarding the short-name of the option, the
2929
* long-name, if any exists, a flag indicating if an argument is required for this option, and a self-documenting
@@ -280,6 +280,9 @@ public Builder valueSeparator(final char sep) {
280280
/** The serial version UID. */
281281
private static final long serialVersionUID = 1L;
282282

283+
/** Empty array. */
284+
static final Option[] EMPTY_ARRAY = {};
285+
283286
/**
284287
* Returns a {@link Builder} to create an {@link Option} using descriptive methods.
285288
*

0 commit comments

Comments
 (0)