From 767d13af30f4c9b2934c16d930b172a7d7b82524 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Mon, 23 Dec 2024 16:44:12 +0000 Subject: [PATCH] Using .forEach instead of for (:) --- .../java/com/beust/jcommander/JCommander.java | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/beust/jcommander/JCommander.java b/src/main/java/com/beust/jcommander/JCommander.java index e3912197..714b6647 100644 --- a/src/main/java/com/beust/jcommander/JCommander.java +++ b/src/main/java/com/beust/jcommander/JCommander.java @@ -321,9 +321,7 @@ public synchronized Console getConsole() { public final void addObject(Object object) { if (object instanceof Iterable i) { // Iterable - for (Object o : i) { - objects.add(o); - } + i.forEach(objects::add); } else if (object.getClass().isArray()) { // Array for (Object o : (Object[]) object) { @@ -376,13 +374,10 @@ private void parse(boolean validate, String... args) { private void initializeDefaultValues() { if (options.defaultProvider != null) { - for (ParameterDescription pd : descriptions.values()) { - initializeDefaultValue(pd); - } + descriptions.values().forEach(this::initializeDefaultValue); - for (Map.Entry entry : commands.entrySet()) { - entry.getValue().initializeDefaultValues(); - } + commands.entrySet().forEach(entry -> + entry.getValue().initializeDefaultValues()); } } @@ -398,9 +393,9 @@ private void validateOptions() { if (!requiredFields.isEmpty()) { List missingFields = new ArrayList<>(); - for (ParameterDescription pd : requiredFields.values()) { - missingFields.add("[" + Strings.join(" | ", pd.getParameter().names()) + "]"); - } + requiredFields.values().forEach(pd -> + missingFields.add("[" + Strings.join(" | ", pd.getParameter().names()) + "]") + ); String message = Strings.join(", ", missingFields); throw new ParameterException("The following " + pluralize(requiredFields.size(), "option is required: ", "options are required: ") @@ -432,13 +427,13 @@ private void validateOptions() { } Map nameValuePairs = Maps.newHashMap(); - for (ParameterDescription pd : fields.values()) { - nameValuePairs.put(pd.getLongestName(), pd.getValue()); - } + fields.values().forEach(pd -> + nameValuePairs.put(pd.getLongestName(), pd.getValue()) + ); - for (IParametersValidator parametersValidator : parametersValidators) { - parametersValidator.validate(nameValuePairs); - } + parametersValidators.forEach(parametersValidator -> + parametersValidator.validate(nameValuePairs) + ); } private static String pluralize(int quantity, String singular, String plural) { @@ -467,7 +462,7 @@ private String[] expandArgs(String[] originalArgv) { // Expand separators // List vResult2 = Lists.newArrayList(); - for (String arg : vResult1) { + vResult1.forEach(arg -> { if (isOption(arg)) { String sep = getSeparatorFor(arg); if (!" ".equals(sep)) { @@ -481,7 +476,7 @@ private String[] expandArgs(String[] originalArgv) { } else { vResult2.add(arg); } - } + }); return vResult2.toArray(new String[vResult2.size()]); } @@ -617,9 +612,7 @@ private static String trim(String string) { public void createDescriptions() { descriptions = Maps.newHashMap(); - for (Object object : objects) { - addDescription(object); - } + objects.forEach(this::addDescription); } private void addDescription(Object object) { @@ -867,7 +860,7 @@ else if (commands.isEmpty()) { } // Mark the parameter descriptions held in fields as assigned - for (ParameterDescription parameterDescription : descriptions.values()) { + descriptions.values().forEach(parameterDescription -> { if (parameterDescription.isAssigned()) { fields.get(parameterDescription.getParameterized()).setAssigned(true); } @@ -878,7 +871,7 @@ else if (commands.isEmpty()) { if (parameterDescription.getDefault() != null && !parameterDescription.getParameterized().getType().isPrimitive()) { requiredFields.remove(parameterDescription.getParameterized()); } - } + }); } @@ -1532,18 +1525,18 @@ public void addCommand(String name, Object object, String... aliases) { public Map getCommands() { Map res = Maps.newLinkedHashMap(); - for (Map.Entry entry : commands.entrySet()) { - res.put(entry.getKey().name, entry.getValue()); - } + commands.entrySet().forEach(entry -> + res.put(entry.getKey().name, entry.getValue()) + ); return res; } public Map getRawCommands() { Map res = Maps.newLinkedHashMap(); - for (Map.Entry entry : commands.entrySet()) { - res.put(entry.getKey(), entry.getValue()); - } + commands.entrySet().forEach(entry -> + res.put(entry.getKey(), entry.getValue()) + ); return res; }