Use Collections.emptyMap and Collections.singletonMap in pattern matching code

This commit is contained in:
Riccardo Azzolini 2019-02-12 11:40:50 +01:00
parent 04d01565da
commit 788f9663e2
5 changed files with 5 additions and 7 deletions

View File

@ -21,7 +21,7 @@ public class ConstantPattern extends VisitorPattern {
public Optional<Map<String, Function>> visit(final Variable variable) {
if (variable.getType().equals(Variable.V_TYPE.CONSTANT)
&& variable.getChar() == symbol) {
return Optional.of(new HashMap<>());
return Optional.of(Collections.emptyMap());
} else {
return Optional.empty();
}

View File

@ -25,7 +25,7 @@ public class EquationsSystemPattern extends VisitorPattern {
return Optional.empty();
}
Optional<Map<String, Function>> subFunctions = Optional.of(new HashMap<>());
Optional<Map<String, Function>> subFunctions = Optional.of(Collections.emptyMap());
for (int i = 0; i < patterns.length && subFunctions.isPresent(); i++) {
final Pattern curPattern = patterns[i];
final Function curFunction = equationsSystem.getParameter(i);

View File

@ -21,7 +21,7 @@ public class NumberPattern extends VisitorPattern {
@Override
public Optional<Map<String, Function>> visit(final Number number) {
if (number.getTerm().compareTo(value) == 0) {
return Optional.of(new HashMap<>());
return Optional.of(Collections.emptyMap());
} else {
return Optional.empty();
}

View File

@ -18,9 +18,7 @@ public class SubFunctionPattern implements Pattern {
@Override
public Optional<Map<String, Function>> match(final Function function) {
final HashMap<String, Function> subFunctions = new HashMap<>();
subFunctions.put(name, function);
return Optional.of(subFunctions);
return Optional.of(Collections.singletonMap(name, function));
}
@Override

View File

@ -13,7 +13,7 @@ import java.util.*;
public class UndefinedPattern extends VisitorPattern {
@Override
public Optional<Map<String, Function>> visit(Undefined undefined) {
return Optional.of(new HashMap<>());
return Optional.of(Collections.emptyMap());
}
@Override