Skip to content

Commit 107acd4

Browse files
committed
Use forEach()
1 parent b65ef1c commit 107acd4

File tree

6 files changed

+18
-25
lines changed

6 files changed

+18
-25
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ public Object getOptionObject(final String optionName) {
306306
*/
307307
public Properties getOptionProperties(final Option option) {
308308
final Properties props = new Properties();
309-
for (final Option processedOption : options) {
309+
options.forEach(processedOption -> {
310310
if (processedOption.equals(option)) {
311311
processPropertiesFromValues(props, processedOption.getValuesList());
312312
}
313-
}
313+
});
314314
return props;
315315
}
316316

@@ -326,11 +326,11 @@ public Properties getOptionProperties(final Option option) {
326326
*/
327327
public Properties getOptionProperties(final String optionName) {
328328
final Properties props = new Properties();
329-
for (final Option option : options) {
329+
options.forEach(option -> {
330330
if (optionName.equals(option.getOpt()) || optionName.equals(option.getLongOpt())) {
331331
processPropertiesFromValues(props, option.getValuesList());
332332
}
333-
}
333+
});
334334
return props;
335335
}
336336

@@ -503,14 +503,14 @@ public String[] getOptionValues(final Option option) {
503503
return null;
504504
}
505505
final List<String> values = new ArrayList<>();
506-
for (final Option processedOption : options) {
506+
options.forEach(processedOption -> {
507507
if (processedOption.equals(option)) {
508508
if (option.isDeprecated()) {
509509
handleDeprecated(option);
510510
}
511511
values.addAll(processedOption.getValuesList());
512512
}
513-
}
513+
});
514514
return values.isEmpty() ? null : values.toArray(Util.EMPTY_STRING_ARRAY);
515515
}
516516

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ private void appendOptionGroup(final StringBuilder buff, final OptionGroup optio
393393
for (final Iterator<Option> it = optList.iterator(); it.hasNext();) {
394394
// whether the option is required or not is handled at group level
395395
appendOption(buff, it.next(), true);
396-
397396
if (it.hasNext()) {
398397
buff.append(" | ");
399398
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Options addOptionGroup(final OptionGroup optionGroup) {
150150
if (optionGroup.isRequired()) {
151151
requiredOpts.add(optionGroup);
152152
}
153-
for (final Option option : optionGroup.getOptions()) {
153+
optionGroup.getOptions().forEach(option -> {
154154
// an Option cannot be required if it is in an
155155
// OptionGroup, either the group is required or
156156
// nothing is required
@@ -159,7 +159,7 @@ public Options addOptionGroup(final OptionGroup optionGroup) {
159159
requiredOpts.remove(key);
160160
addOption(option);
161161
optionGroups.put(key, optionGroup);
162-
}
162+
});
163163
return this;
164164
}
165165

@@ -172,12 +172,12 @@ public Options addOptionGroup(final OptionGroup optionGroup) {
172172
* @since 1.7.0
173173
*/
174174
public Options addOptions(final Options options) {
175-
for (final Option opt : options.getOptions()) {
175+
options.getOptions().forEach(opt -> {
176176
if (hasOption(opt.getKey())) {
177177
throw new IllegalArgumentException("Duplicate key: " + opt.getKey());
178178
}
179179
addOption(opt);
180-
}
180+
});
181181
options.getOptionGroups().forEach(this::addOptionGroup);
182182
return this;
183183
}
@@ -223,11 +223,11 @@ public List<String> getMatchingOptions(final String opt) {
223223
if (longOpts.containsKey(clean)) {
224224
return Collections.singletonList(clean);
225225
}
226-
for (final String longOpt : longOpts.keySet()) {
226+
longOpts.keySet().forEach(longOpt -> {
227227
if (longOpt.startsWith(clean)) {
228228
matchingOpts.add(longOpt);
229229
}
230-
}
230+
});
231231
return matchingOpts;
232232
}
233233

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ public CommandLine parse(final Options options, final String[] arguments, final
148148
public CommandLine parse(final Options options, final String[] arguments, final Properties properties, final boolean stopAtNonOption)
149149
throws ParseException {
150150
// clear out the data in options in case it's been used before (CLI-71)
151-
for (final Option opt : options.helpOptions()) {
152-
opt.clearValues();
153-
}
151+
options.helpOptions().forEach(Option::clearValues);
154152
// clear the data from the groups
155153
for (final OptionGroup optionGroup : options.getOptionGroups()) {
156154
optionGroup.setSelected(null);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public TableDefinition getTableDefinition(final Iterable<Option> options) {
163163
// setup the rows for the table.
164164
final List<List<String>> rows = new ArrayList<>();
165165
final StringBuilder sb = new StringBuilder();
166-
for (final Option option : options) {
166+
options.forEach(option -> {
167167
final List<String> row = new ArrayList<>();
168168
// create an option formatter to correctly format the parts of the option
169169
final OptionFormatter formatter = getOptionFormatBuilder().build(option);
@@ -182,7 +182,7 @@ public TableDefinition getTableDefinition(final Iterable<Option> options) {
182182
// add the option description
183183
row.add(formatter.getDescription());
184184
rows.add(row);
185-
}
185+
});
186186
// return the TableDefinition with the proper column headers.
187187
return TableDefinition.from("", styles, showSince ? Arrays.asList("Options", "Since", "Description") : Arrays.asList("Options", "Description"), rows);
188188
}

src/main/java/org/apache/commons/cli/help/TextHelpAppendable.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,8 @@ protected TableDefinition adjustTableFormat(final TableDefinition table) {
191191
}
192192
// regenerate the styles
193193
final List<TextStyle> styles = new ArrayList<>();
194-
for (final TextStyle.Builder builder : styleBuilders) {
195-
// adjust by removing the padding as it was not accounted for above.
196-
styles.add(builder.get());
197-
}
194+
// adjust by removing the padding as it was not accounted for above.
195+
styleBuilders.forEach(builder -> styles.add(builder.get()));
198196
return TableDefinition.from(table.caption(), styles, table.headers(), table.rows());
199197
}
200198

@@ -244,9 +242,7 @@ public void appendTable(final TableDefinition rawTable) throws IOException {
244242
// write the table
245243
appendParagraph(table.caption());
246244
final List<TextStyle> headerStyles = new ArrayList<>();
247-
for (final TextStyle style : table.columnTextStyles()) {
248-
headerStyles.add(TextStyle.builder().setTextStyle(style).setAlignment(TextStyle.Alignment.CENTER).get());
249-
}
245+
table.columnTextStyles().forEach(style -> headerStyles.add(TextStyle.builder().setTextStyle(style).setAlignment(TextStyle.Alignment.CENTER).get()));
250246
writeColumnQueues(makeColumnQueues(table.headers(), headerStyles), headerStyles);
251247
for (final List<String> row : table.rows()) {
252248
writeColumnQueues(makeColumnQueues(row, table.columnTextStyles()), table.columnTextStyles());

0 commit comments

Comments
 (0)