[refactor] Use mapping computeIfPresent/Absent for conditional branch

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2021-11-07 11:55:21 +01:00
parent d726312341
commit 8643b69054
3 changed files with 14 additions and 16 deletions

View File

@ -103,7 +103,7 @@ public final class CommandParameters {
* @param key the key * @param key the key
* @return if the key was specified */ * @return if the key was specified */
public boolean isActive(final String key) { public boolean isActive(final String key) {
Boolean val = booleanArguments.get(key); final Boolean val = booleanArguments.get(key);
return val != null && val.booleanValue(); return val != null && val.booleanValue();
} }
@ -198,9 +198,8 @@ public final class CommandParameters {
* @param string the key * @param string the key
* @param value the value */ * @param value the value */
public void set(final String string, final boolean value) { public void set(final String string, final boolean value) {
if (booleanArguments.containsKey(string)) { booleanArguments.computeIfPresent(string,
booleanArguments.put(string, Boolean.valueOf(value)); (final String k, final Boolean v) -> Boolean.valueOf(value));
}
} }
/** Set a string parameter value. /** Set a string parameter value.
@ -208,8 +207,7 @@ public final class CommandParameters {
* @param string the key * @param string the key
* @param value the value */ * @param value the value */
public void set(final String string, final String value) { public void set(final String string, final String value) {
if (stringArguments.containsKey(string)) { stringArguments.computeIfPresent(string,
stringArguments.put(string, value); (final String k, final String v) -> value);
}
} }
} }

View File

@ -149,14 +149,16 @@ public final class ParametrizedCommandData {
* @param needed if the parameter is needed * @param needed if the parameter is needed
* @throws InvalidParameterException if the new definition is invalid */ * @throws InvalidParameterException if the new definition is invalid */
private void checkParam(final String param, final boolean needed) { private void checkParam(final String param, final boolean needed) {
final Boolean val = stringParams.get(param); final Boolean val = stringParams.computeIfPresent(param,
if (val != null) { (final String k, final Boolean v) -> {
final Boolean need = Boolean.valueOf(needed || val.booleanValue()); final Boolean need = Boolean.valueOf(needed || v.booleanValue());
stringParams.put(param, need);
params.put(param, need); params.put(param, need);
return; return need;
});
if (val == null) {
throw new InvalidParameterException(
"Parameter is already defined as boolean"); //$NON-NLS-1$
} }
throw new InvalidParameterException("Parameter is already defined as boolean"); //$NON-NLS-1$
} }
/** Retrieve the boolean parameters (aka flags). /** Retrieve the boolean parameters (aka flags).

View File

@ -202,9 +202,7 @@ public final class ReadingRunnable implements Runnable {
public void waitForDelivery(final String message) throws InterruptedException { public void waitForDelivery(final String message) throws InterruptedException {
Object mLock; Object mLock;
synchronized (messageBlockerLock) { synchronized (messageBlockerLock) {
if (!messageBlocker.containsKey(message)) { messageBlocker.computeIfAbsent(message, (final String k) -> new Object());
messageBlocker.put(message, new Object());
}
mLock = messageBlocker.get(message); mLock = messageBlocker.get(message);
} }
synchronized (mLock) { synchronized (mLock) {