From d5a7d4a16fc2515975b47bd70991036de077d01b Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Sun, 14 Oct 2018 09:59:46 -0400 Subject: [PATCH] Avoid contains and get calls on same key in map Signed-off-by: Emmanuel Bigeon --- .../gclc/command/CommandParameters.java | 4 ++-- .../gclc/command/ParametrizedCommandData.java | 20 +++++++++---------- .../bigeon/gclc/utils/ReadingRunnable.java | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/gclc/src/main/java/net/bigeon/gclc/command/CommandParameters.java b/gclc/src/main/java/net/bigeon/gclc/command/CommandParameters.java index 92e3849..23a01b1 100644 --- a/gclc/src/main/java/net/bigeon/gclc/command/CommandParameters.java +++ b/gclc/src/main/java/net/bigeon/gclc/command/CommandParameters.java @@ -103,8 +103,8 @@ public final class CommandParameters { * @param key the key * @return if the key was specified */ public boolean getBool(final String key) { - return booleanArguments.containsKey(key) - && booleanArguments.get(key).booleanValue(); + Boolean val = booleanArguments.get(key); + return val != null && val.booleanValue(); } /** Get the boolean arguments. diff --git a/gclc/src/main/java/net/bigeon/gclc/command/ParametrizedCommandData.java b/gclc/src/main/java/net/bigeon/gclc/command/ParametrizedCommandData.java index 11b56f9..61b1421 100644 --- a/gclc/src/main/java/net/bigeon/gclc/command/ParametrizedCommandData.java +++ b/gclc/src/main/java/net/bigeon/gclc/command/ParametrizedCommandData.java @@ -93,7 +93,7 @@ import net.bigeon.gclc.manager.EmptyInput; public final class ParametrizedCommandData { /** The boolean parameters mandatory status. */ - private final Set boolParams = new HashSet<>(); + private final Set boolParams = new HashSet<>(); /** The string parameters mandatory status. */ private final Map stringParams = new ConcurrentHashMap<>(); @@ -101,7 +101,7 @@ public final class ParametrizedCommandData { private final Map params = new ConcurrentHashMap<>(); /** The restriction of provided parameters on execution to declared paramters in * the status maps. */ - private final boolean strict; + private final boolean strict; /** The data for the parametrized command. */ public ParametrizedCommandData() { @@ -120,8 +120,7 @@ public final class ParametrizedCommandData { * @param flag the boolean flag * @throws InvalidParameterException if the parameter is already defined as a * string parameter */ - public void addBooleanParameter(final String flag) - throws InvalidParameterException { + public void addBooleanParameter(final String flag) throws InvalidParameterException { if (params.containsKey(flag) && stringParams.containsKey(flag)) { throw new InvalidParameterException("Parameter is already defined as string"); //$NON-NLS-1$ } @@ -152,9 +151,9 @@ public final class ParametrizedCommandData { * @throws InvalidParameterException if the new definition is invalid */ private void checkParam(final String param, final boolean needed) throws InvalidParameterException { - if (stringParams.containsKey(param)) { - final Boolean need = Boolean - .valueOf(needed || stringParams.get(param).booleanValue()); + Boolean val = stringParams.get(param); + if (val != null) { + final Boolean need = Boolean.valueOf(needed || val.booleanValue()); stringParams.put(param, need); params.put(param, need); return; @@ -182,8 +181,8 @@ public final class ParametrizedCommandData { * @param args the command arguments * @return the command object * @throws IOException if the command could not be filled. */ - public CommandParameters getParameters(final ConsoleInput input, - final String... args) throws IOException { + public CommandParameters getParameters(final ConsoleInput input, final String... args) + throws IOException { final CommandParameters parameters = new CommandParameters(boolParams, stringParams.keySet(), strict); try { @@ -218,7 +217,8 @@ public final class ParametrizedCommandData { * @param param the parameter name * @return if the parameter is needed */ public boolean isNeeded(final String param) { - return params.containsKey(param) && params.get(param).booleanValue(); + Boolean val = params.get(param); + return val != null && val.booleanValue(); } /** If the command refuse unrecognized parameters. diff --git a/gclc/src/main/java/net/bigeon/gclc/utils/ReadingRunnable.java b/gclc/src/main/java/net/bigeon/gclc/utils/ReadingRunnable.java index 4366617..38dbe58 100644 --- a/gclc/src/main/java/net/bigeon/gclc/utils/ReadingRunnable.java +++ b/gclc/src/main/java/net/bigeon/gclc/utils/ReadingRunnable.java @@ -306,8 +306,8 @@ public final class ReadingRunnable implements Runnable { private void notifyMessage(final String message) { synchronized (messageBlockerLock) { delivering = message; - if (messageBlocker.containsKey(message)) { - final Object mLock = messageBlocker.get(message); + final Object mLock = messageBlocker.get(message); + if (mLock!=null) { synchronized (mLock) { mLock.notifyAll(); }