Skip to content

Commit

Permalink
Avoid redundant Map.containsKey call (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
turbanoff authored Jan 16, 2023
1 parent 77f1cea commit c2a0c9e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 66 deletions.
13 changes: 6 additions & 7 deletions builtins/src/main/java/org/jline/builtins/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ public Options setOptionsFirst(boolean optionsFirst) {
}

public boolean isSet(String name) {
if (!optSet.containsKey(name))
Boolean isSet = optSet.get(name);
if (isSet == null) {
throw new IllegalArgumentException("option not defined in spec: " + name);

return optSet.get(name);
}
return isSet;
}

public Object getObject(String name) {
Expand Down Expand Up @@ -311,9 +312,8 @@ private void parseSpec(Map<String, Boolean> myOptSet, Map<String, Object> myOptA
final String name = (opt != null) ? opt : m.group(GROUP_SHORT_OPT_1);

if (name != null) {
if (myOptSet.containsKey(name))
if (myOptSet.putIfAbsent(name, false) != null)
throw new IllegalArgumentException("duplicate option in spec: --" + name);
myOptSet.put(name, false);
}

String dflt = (m.group(GROUP_DEFAULT) != null) ? m.group(GROUP_DEFAULT) : "";
Expand All @@ -331,9 +331,8 @@ private void parseSpec(Map<String, Boolean> myOptSet, Map<String, Object> myOptA
for (int i = 0; i < 2; ++i) {
String sopt = m.group(i == 0 ? GROUP_SHORT_OPT_1 : GROUP_SHORT_OPT_2);
if (sopt != null) {
if (optName.containsKey(sopt))
if (optName.putIfAbsent(sopt, name) != null)
throw new IllegalArgumentException("duplicate option in spec: -" + sopt);
optName.put(sopt, name);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Styles.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ public StyleCompiler(Map<String,String> colors, boolean nanoStyle) {
}

public String getStyle(String reference) {
if (!colors.containsKey(reference)) {
return null;
}
String rawStyle = colors.get(reference);
if (rawStyle == null) {
return null;
Expand Down
15 changes: 9 additions & 6 deletions builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,9 @@ public void parse() throws IOException {
}
} else if (!addHighlightRule(parts, idx, TOKEN_NANORC) && parts.get(0).matches("\\+" + REGEX_TOKEN_NAME)) {
String key = themeKey(parts.get(0));
if (colorTheme.containsKey(key)) {
for (String l : colorTheme.get(key).split("\\\\n")) {
String theme = colorTheme.get(key);
if (theme != null) {
for (String l : theme.split("\\\\n")) {
idx++;
addHighlightRule(RuleSplitter.split(fixRegexes(l)), idx, TOKEN_NANORC);
}
Expand Down Expand Up @@ -584,18 +585,20 @@ private boolean addHighlightRule(List<String> parts, int idx, String tokenName)
addHighlightRule(syntaxName + idx, parts, true, tokenName);
} else if (parts.get(0).matches(REGEX_TOKEN_NAME + "[:]?")) {
String key = themeKey(parts.get(0));
if (colorTheme.containsKey(key)) {
String theme = colorTheme.get(key);
if (theme != null) {
parts.set(0, "color");
parts.add(1, colorTheme.get(key));
parts.add(1, theme);
addHighlightRule(syntaxName + idx, parts, false, tokenName);
} else {
Log.warn("Unknown token type: ", key);
}
} else if (parts.get(0).matches("~" + REGEX_TOKEN_NAME + "[:]?")) {
String key = themeKey(parts.get(0));
if (colorTheme.containsKey(key)) {
String theme = colorTheme.get(key);
if (theme != null) {
parts.set(0, "icolor");
parts.add(1, colorTheme.get(key));
parts.add(1, theme);
addHighlightRule(syntaxName + idx, parts, true, tokenName);
} else {
Log.warn("Unknown token type: ", key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,7 @@ public SystemCompleter compileCompleters() {

public T command(String name) {
T out;
if (!hasCommand(name)) {
throw new IllegalArgumentException("Command does not exists!");
}
if (aliasCommand.containsKey(name)) {
name = aliasCommand.get(name);
}
name = aliasCommand.getOrDefault(name, name);
if (nameCommand.containsKey(name)) {
out = nameCommand.get(name);
} else {
Expand Down Expand Up @@ -248,10 +243,8 @@ public SystemCompleter compileCompleters() {
public String command(String name) {
if (commandExecute.containsKey(name)) {
return name;
} else if (aliasCommand.containsKey(name)) {
return aliasCommand.get(name);
}
return null;
return aliasCommand.get(name);
}

public CommandMethods getCommandMethods(String command) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,8 +1283,8 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>
if (words.size() > 1) {
String h = words.get(words.size() - 2);
if (h != null && h.length() > 0) {
if(aliases.containsKey(h)){
String v = aliases.get(h);
String v = aliases.get(h);
if (v != null) {
candidates.add(new Candidate(AttributedString.stripAnsi(v)
, v, null, null, null, null, true));
}
Expand Down
42 changes: 20 additions & 22 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,10 @@ protected Terminal terminal() {
*/
protected void manageBooleanOptions(Map<String, Object> options) {
for (String key : Printer.BOOLEAN_KEYS) {
if (options.containsKey(key)) {
boolean value = options.get(key) instanceof Boolean && (boolean) options.get(key);
if (!value) {
options.remove(key);
}
Object option = options.get(key);
boolean value = option instanceof Boolean && (boolean) option;
if (!value) {
options.remove(key);
}
}
}
Expand Down Expand Up @@ -516,15 +515,14 @@ private Object mapValue(Map<String, Object> options, String key, Map<String,Obje
@SuppressWarnings("unchecked")
private List<String> optionList(String key, Map<String,Object> options) {
List<String> out = new ArrayList<>();
if (options.containsKey(key)) {
if (options.get(key) instanceof String) {
out.addAll(Arrays.asList(((String)options.get(key)).split(",")));
} else if (options.get(key) instanceof Collection) {
out.addAll((Collection<String>)options.get(key));
} else {
throw new IllegalArgumentException("Unsupported option list: {key: " + key
+ ", type: " + options.get(key).getClass() + "}");
}
Object option = options.get(key);
if (option instanceof String) {
out.addAll(Arrays.asList(((String)option).split(",")));
} else if (option instanceof Collection) {
out.addAll((Collection<String>)option);
} else if (option != null) {
throw new IllegalArgumentException("Unsupported option list: {key: " + key
+ ", type: " + option.getClass() + "}");
}
return out;
}
Expand Down Expand Up @@ -563,9 +561,8 @@ private String columnValue(String value) {
@SuppressWarnings("unchecked")
private Map<String,Object> objectToMap(Map<String, Object> options, Object obj) {
if (obj != null) {
Map<Class<?>, Object> toMap = options.containsKey(Printer.OBJECT_TO_MAP)
? (Map<Class<?>, Object>)options.get(Printer.OBJECT_TO_MAP)
: new HashMap<>();
Map<Class<?>, Object> toMap = (Map<Class<?>, Object>)
options.getOrDefault(Printer.OBJECT_TO_MAP, Collections.emptyMap());
if (toMap.containsKey(obj.getClass())) {
return (Map<String,Object>)engine.execute(toMap.get(obj.getClass()), obj);
} else if (objectToMap.containsKey(obj.getClass())) {
Expand Down Expand Up @@ -639,14 +636,15 @@ private AttributedString highlightValue(Map<String, Object> options, String colu
} else {
out = new AttributedString(columnValue(objectToString(options, raw)));
}
}
if ((simpleObject(raw) || raw == null) && (hv.containsKey("*") || highlightValue.containsKey("*"))
}
if ((simpleObject(raw) || raw == null) && (hv.containsKey("*") || highlightValue.containsKey("*"))
&& !isHighlighted(out)) {
if (hv.containsKey("*")) {
out = (AttributedString) engine.execute(hv.get("*"), out);
out = (AttributedString)engine.execute(hv.get("*"), out);
}
if (highlightValue.containsKey("*")) {
out = highlightValue.get("*").apply(out);
Function<Object, AttributedString> func = highlightValue.get("*");
if (func != null) {
out = func.apply(out);
}
}
if (options.containsKey(Printer.VALUE_STYLE) && !isHighlighted(out)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ public void register(String command, CommandRegistry subcommandRegistry) {

private List<String> localCommandInfo(String command) {
try {
if (subcommands.containsKey(command)) {
registryHelp(subcommands.get(command));
CommandRegistry subCommand = subcommands.get(command);
if (subCommand != null) {
registryHelp(subCommand);
} else {
localExecute(command, new String[] { "--help" });
}
Expand Down Expand Up @@ -275,8 +276,9 @@ private SystemCompleter _compileCompleters() {
SystemCompleter out = CommandRegistry.aggregateCompleters(commandRegistries);
SystemCompleter local = new SystemCompleter();
for (String command : commandExecute.keySet()) {
if (subcommands.containsKey(command)) {
for(Map.Entry<String,List<Completer>> entry : subcommands.get(command).compileCompleters().getCompleters().entrySet()) {
CommandRegistry subCommand = subcommands.get(command);
if (subCommand != null) {
for (Map.Entry<String,List<Completer>> entry : subCommand.compileCompleters().getCompleters().entrySet()) {
for (Completer cc : entry.getValue()) {
if (!(cc instanceof ArgumentCompleter)) {
throw new IllegalArgumentException();
Expand Down Expand Up @@ -369,18 +371,19 @@ public CmdDesc commandDescription(CmdLine line) {
case COMMAND:
if (isCommandOrScript(cmd) && !names.hasPipes(line.getArgs())) {
List<String> args = line.getArgs();
if (subcommands.containsKey(cmd)) {
CommandRegistry subCommand = subcommands.get(cmd);
if (subCommand != null) {
String c = args.size() > 1 ? args.get(1) : null;
if (c == null || subcommands.get(cmd).hasCommand(c)) {
if (c == null || subCommand.hasCommand(c)) {
if (c != null && c.equals("help")) {
out = null;
} else if (c != null) {
out = subcommands.get(cmd).commandDescription(Collections.singletonList(c));
out = subCommand.commandDescription(Collections.singletonList(c));
} else {
out = commandDescription(subcommands.get(cmd));
out = commandDescription(subCommand);
}
} else {
out = commandDescription(subcommands.get(cmd));
out = commandDescription(subCommand);
}
if (out != null) {
out.setSubcommand(true);
Expand Down
7 changes: 3 additions & 4 deletions console/src/main/java/org/jline/widget/TailTipWidgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,14 +744,13 @@ private Pair<String,Boolean> evaluateCommandLine(String line, List<String> args,
}

public CmdDesc getDescription(String command) {
CmdDesc out = null;
CmdDesc out;
if (descriptions.containsKey(command)) {
out = descriptions.get(command);
} else if (temporaryDescs.containsKey(command)) {
out = temporaryDescs.get(command);
} else if (volatileDescs.containsKey(command)) {
out = volatileDescs.get(command);
volatileDescs.remove(command);
} else {
out = volatileDescs.remove(command);
}
return out;
}
Expand Down
4 changes: 1 addition & 3 deletions console/src/test/java/org/jline/example/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,8 @@ public boolean hasCommand(String command) {
private String command(String name) {
if (commandExecute.containsKey(name)) {
return name;
} else if (aliasCommand.containsKey(name)) {
return aliasCommand.get(name);
}
return null;
return aliasCommand.get(name);
}

public SystemCompleter compileCompleters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private String command(String cmd) {
if (cmd != null) {
if (completers.containsKey(cmd)) {
out = cmd;
} else if (aliasCommand.containsKey(cmd)) {
} else {
out = aliasCommand.get(cmd);
}
}
Expand Down

0 comments on commit c2a0c9e

Please sign in to comment.