Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid redundant Map.containsKey call #781

Merged
merged 2 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")) {
turbanoff marked this conversation as resolved.
Show resolved Hide resolved
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){
turbanoff marked this conversation as resolved.
Show resolved Hide resolved
candidates.add(new Candidate(AttributedString.stripAnsi(v)
, v, null, null, null, null, true));
}
Expand Down
39 changes: 20 additions & 19 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,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) {
turbanoff marked this conversation as resolved.
Show resolved Hide resolved
options.remove(key);
}
}
}
Expand Down Expand Up @@ -512,15 +511,17 @@ 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 == null) {
return out;
}
if (option instanceof String) {
out.addAll(Arrays.asList(((String)option).split(",")));
} else if (option instanceof Collection) {
out.addAll((Collection<String>)option);
} else {
throw new IllegalArgumentException("Unsupported option list: {key: " + key
+ ", type: " + option.getClass() + "}");
}
turbanoff marked this conversation as resolved.
Show resolved Hide resolved
return out;
}
Expand Down Expand Up @@ -559,9 +560,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())) {
gnodet marked this conversation as resolved.
Show resolved Hide resolved
return (Map<String,Object>)engine.execute(toMap.get(obj.getClass()), obj);
} else if (objectToMap.containsKey(obj.getClass())) {
Expand Down Expand Up @@ -641,8 +641,9 @@ private AttributedString highlightValue(Map<String, Object> options, String colu
if (hv.containsKey("*")) {
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);
}
turbanoff marked this conversation as resolved.
Show resolved Hide resolved
}
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