This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-cli.git
The following commit(s) were added to refs/heads/master by this push: new 9b101bf Better use of toArray() 9b101bf is described below commit 9b101bf36c477fb2e802d0860afdc093aef1307f Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Oct 3 08:50:56 2022 -0400 Better use of toArray() --- src/main/java/org/apache/commons/cli/CommandLine.java | 19 ++++--------------- src/main/java/org/apache/commons/cli/Option.java | 7 +++++-- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java b/src/main/java/org/apache/commons/cli/CommandLine.java index 05d6a93..ad416cb 100644 --- a/src/main/java/org/apache/commons/cli/CommandLine.java +++ b/src/main/java/org/apache/commons/cli/CommandLine.java @@ -17,16 +17,15 @@ package org.apache.commons.cli; +import static org.apache.commons.cli.Util.EMPTY_STRING_ARRAY; + import java.io.Serializable; import java.util.ArrayList; -import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Properties; -import static org.apache.commons.cli.Util.EMPTY_STRING_ARRAY; - /** * Represents list of arguments parsed against a {@link Options} descriptor. * <p> @@ -125,11 +124,7 @@ public class CommandLine implements Serializable { * @return remaining items passed in but not parsed as an array. */ public String[] getArgs() { - final String[] answer = new String[args.size()]; - - args.toArray(answer); - - return answer; + return args.toArray(Util.EMPTY_STRING_ARRAY); } /** @@ -225,13 +220,7 @@ public class CommandLine implements Serializable { * @return an array of the processed {@link Option}s. */ public Option[] getOptions() { - final Collection<Option> processed = options; - - // reinitialize array - final Option[] optionsArray = new Option[processed.size()]; - - // return the array - return processed.toArray(optionsArray); + return options.toArray(Option.EMPTY_ARRAY); } /** diff --git a/src/main/java/org/apache/commons/cli/Option.java b/src/main/java/org/apache/commons/cli/Option.java index d101c50..9c88310 100644 --- a/src/main/java/org/apache/commons/cli/Option.java +++ b/src/main/java/org/apache/commons/cli/Option.java @@ -17,13 +17,13 @@ package org.apache.commons.cli; +import static org.apache.commons.cli.Util.EMPTY_STRING_ARRAY; + import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Objects; -import static org.apache.commons.cli.Util.EMPTY_STRING_ARRAY; - /** * Describes a single command-line option. It maintains information regarding the short-name of the option, the * 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 class Option implements Cloneable, Serializable { /** The serial version UID. */ private static final long serialVersionUID = 1L; + /** Empty array. */ + static final Option[] EMPTY_ARRAY = {}; + /** * Returns a {@link Builder} to create an {@link Option} using descriptive methods. *