Skip to content

Commit

Permalink
Using .forEach instead of for (:)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Dec 23, 2024
1 parent 95acea1 commit 767d13a
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<ProgramName, JCommander> entry : commands.entrySet()) {
entry.getValue().initializeDefaultValues();
}
commands.entrySet().forEach(entry ->
entry.getValue().initializeDefaultValues());
}
}

Expand All @@ -398,9 +393,9 @@ private void validateOptions() {

if (!requiredFields.isEmpty()) {
List<String> 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: ")
Expand Down Expand Up @@ -432,13 +427,13 @@ private void validateOptions() {
}

Map<String, Object> 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) {
Expand Down Expand Up @@ -467,7 +462,7 @@ private String[] expandArgs(String[] originalArgv) {
// Expand separators
//
List<String> vResult2 = Lists.newArrayList();
for (String arg : vResult1) {
vResult1.forEach(arg -> {
if (isOption(arg)) {
String sep = getSeparatorFor(arg);
if (!" ".equals(sep)) {
Expand All @@ -481,7 +476,7 @@ private String[] expandArgs(String[] originalArgv) {
} else {
vResult2.add(arg);
}
}
});

return vResult2.toArray(new String[vResult2.size()]);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -878,7 +871,7 @@ else if (commands.isEmpty()) {
if (parameterDescription.getDefault() != null && !parameterDescription.getParameterized().getType().isPrimitive()) {
requiredFields.remove(parameterDescription.getParameterized());
}
}
});

}

Expand Down Expand Up @@ -1532,18 +1525,18 @@ public void addCommand(String name, Object object, String... aliases) {
public Map<String, JCommander> getCommands() {
Map<String, JCommander> res = Maps.newLinkedHashMap();

for (Map.Entry<ProgramName, JCommander> 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<ProgramName, JCommander> getRawCommands() {
Map<ProgramName, JCommander> res = Maps.newLinkedHashMap();

for (Map.Entry<ProgramName, JCommander> entry : commands.entrySet()) {
res.put(entry.getKey(), entry.getValue());
}
commands.entrySet().forEach(entry ->
res.put(entry.getKey(), entry.getValue())
);
return res;
}

Expand Down

0 comments on commit 767d13a

Please sign in to comment.