Fix test and wait

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2018-10-14 18:14:51 -04:00
parent 2a05366e31
commit 4301f2a15e
12 changed files with 213 additions and 154 deletions

View File

@@ -102,7 +102,7 @@ public final class CommandParameters {
*
* @param key the key
* @return if the key was specified */
public boolean getBool(final String key) {
public boolean isActive(final String key) {
Boolean val = booleanArguments.get(key);
return val != null && val.booleanValue();
}

View File

@@ -85,7 +85,7 @@ import java.util.concurrent.ConcurrentHashMap;
import net.bigeon.gclc.exception.CommandParsingException;
import net.bigeon.gclc.exception.InvalidParameterException;
import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.manager.EmptyInput;
import net.bigeon.gclc.utils.EmptyInput;
/** An object to handle standardized command parameters.
*

View File

@@ -70,6 +70,7 @@ package net.bigeon.gclc.prompt;
* #L%
*/
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -162,33 +163,27 @@ public final class CLIPrompter {
* @throws IOException if the manager was closed */
public static boolean promptBoolean(final ConsoleOutput manager,
final ConsoleInput input, final String message) throws IOException {
String result = input
.prompt(message + CLIPrompterMessages.getString(BOOL_CHOICES));
String booleanChoices = message + CLIPrompterMessages.getString(BOOL_CHOICES);
String result = input.prompt(booleanChoices);
boolean first = true;
final String choices = CLIPrompterMessages.getString(PROMPTBOOL_CHOICES_YES1)
+ ", " + //$NON-NLS-1$
CLIPrompterMessages.getString(PROMPTBOOL_CHOICES_NO1);
while (!(result
.equalsIgnoreCase(CLIPrompterMessages.getString(PROMPTBOOL_CHOICES_YES1))
|| CLIPrompterMessages.getString(PROMPTBOOL_CHOICES_NO1)
.equalsIgnoreCase(result)
|| CLIPrompterMessages.getString("promptbool.choices.no2") //$NON-NLS-1$
.equalsIgnoreCase(result)
|| CLIPrompterMessages.getString("promptbool.choices.yes2") //$NON-NLS-1$
.equalsIgnoreCase(result))) {
String booleanYesChoice = CLIPrompterMessages.getString(PROMPTBOOL_CHOICES_YES1);
String booleanNoChoice = CLIPrompterMessages.getString(PROMPTBOOL_CHOICES_NO1);
final String choices = MessageFormat.format("{0}, {1}", booleanYesChoice,
booleanNoChoice);
String yes2 = CLIPrompterMessages.getString("promptbool.choices.yes2");
String no2 = CLIPrompterMessages.getString("promptbool.choices.no2");
while (!(result.equalsIgnoreCase(booleanYesChoice)
|| booleanNoChoice.equalsIgnoreCase(result)
|| no2.equalsIgnoreCase(result) || yes2.equalsIgnoreCase(result))) {
if (!first) {
manager.println(CLIPrompterMessages
.getString("promptbool.choices.invalid", choices)); //$NON-NLS-1$
result = input
.prompt(message + CLIPrompterMessages.getString(BOOL_CHOICES));
result = input.prompt(booleanChoices);
}
first = false;
}
return result
.equalsIgnoreCase(CLIPrompterMessages.getString(PROMPTBOOL_CHOICES_YES1))
|| result.equalsIgnoreCase(
CLIPrompterMessages.getString("promptbool.choices.yes2")); //$NON-NLS-1$
return result.equalsIgnoreCase(booleanYesChoice) || result.equalsIgnoreCase(yes2); // $NON-NLS-1$
}
/** Prompt for a choice.

View File

@@ -36,7 +36,9 @@
* gclc:net.bigeon.gclc.manager.EmptyInput.java
* Created on: Nov 13, 2017
*/
package net.bigeon.gclc.manager;
package net.bigeon.gclc.utils;
import net.bigeon.gclc.manager.ConsoleInput;
/*-
* #%L
@@ -80,6 +82,7 @@ import net.bigeon.gclc.tools.StringProvider;
* @author Emmanuel Bigeon */
public final class EmptyInput implements ConsoleInput {
private static final ConstantString CONSTANT_STRING = new ConstantString("");
/** The empty prompter. */
public static final ConsoleInput INSTANCE = new EmptyInput();
@@ -99,7 +102,7 @@ public final class EmptyInput implements ConsoleInput {
* @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */
@Override
public StringProvider getPrompt() {
return new ConstantString(""); //$NON-NLS-1$
return CONSTANT_STRING; //$NON-NLS-1$
}
/* (non-Javadoc)

View File

@@ -143,10 +143,10 @@ public final class PipedConsoleOutput implements ConsoleOutput {
* may not</em> be the next line of data.
*
* @param message the message
* @return the thread to join to wait for message delivery
* @see net.bigeon.gclc.utils.ReadingRunnable#getWaitForDelivery(java.lang.String) */
public Thread getWaitForDelivery(final String message) {
return reading.getWaitForDelivery(message);
* @throws InterruptedException if the wait on the message was interrupted
* @see net.bigeon.gclc.utils.ReadingRunnable#waitForDelivery(String) */
public void waitForDelivery(final String message) throws InterruptedException {
reading.waitForDelivery(message);
}
/* (non-Javadoc)

View File

@@ -86,64 +86,6 @@ import java.util.logging.Logger;
* @author Emmanuel Bigeon */
public final class ReadingRunnable implements Runnable {
/** The runnable to wait for arrival of a message in the queue.
*
* @author Emmanuel Bigeon */
private final class ToWaitRunnable implements Runnable {
/** The Object. */
private final Object obj;
/** The locking object. */
private final Object start;
/** The message. */
private final String message;
/** The started status. */
private boolean started;
/** Create the waiting runnable.
*
* @param obj the object to lock on
* @param start the object to notify when ready to wait
* @param message the message to wait for */
public ToWaitRunnable(final Object obj, final Object start,
final String message) {
this.obj = obj;
this.start = start;
this.message = message;
}
/** Test if the waiting runnable is started.
*
* @return the started */
public boolean isStarted() {
synchronized (start) {
return started;
}
}
/* (non-Javadoc)
* @see java.lang.Runnable#run() */
@Override
public void run() {
synchronized (obj) {
synchronized (start) {
started = true;
start.notifyAll();
}
while (isRunning()) {
try {
obj.wait();
if (delivering.equals(message)) {
return;
}
} catch (final InterruptedException e) {
LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION, e);
Thread.currentThread().interrupt();
}
}
}
}
}
/** The thread intteruption logging message. */
private static final String THREAD_INTERRUPTION_EXCEPTION = "Thread interruption exception."; //$NON-NLS-1$
/** The closed pipe message. */
@@ -169,9 +111,6 @@ public final class ReadingRunnable implements Runnable {
private final Map<String, Object> messageBlocker = new ConcurrentHashMap<>();
/** The lock. */
private final Object messageBlockerLock = new Object();
/** The message being delivered. */
private String delivering;
/** Create a reading runnable.
*
* @param reader the input to read from */
@@ -237,37 +176,20 @@ public final class ReadingRunnable implements Runnable {
return messages.poll();
}
}
/** Get a waiting thread for a specific message delivery.
*
* @param message the message
* @return the thread to join to wait for message delivery */
public Thread getWaitForDelivery(final String message) {
public void waitForDelivery(String message) throws InterruptedException {
Object mLock;
synchronized (messageBlockerLock) {
if (!messageBlocker.containsKey(message)) {
messageBlocker.put(message, new Object());
}
final Object obj = messageBlocker.get(message);
final Object start = new Object();
final ToWaitRunnable waitRunn = new ToWaitRunnable(obj, start, message);
final Thread th = new Thread(waitRunn);
// Wait for the thread to actually start before unlocking the message queue.
synchronized (start) {
th.start();
while (!waitRunn.isStarted()) {
try {
start.wait(TIMEOUT);
} catch (final InterruptedException e) {
LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION, e);
Thread.currentThread().interrupt();
}
}
}
return th;
mLock = messageBlocker.get(message);
}
synchronized (mLock) {
mLock.wait();
}
}
/** Test if some data is available.
*
* @return if a message is waiting
@@ -305,7 +227,6 @@ public final class ReadingRunnable implements Runnable {
* @param message the message */
private void notifyMessage(final String message) {
synchronized (messageBlockerLock) {
delivering = message;
final Object mLock = messageBlocker.get(message);
if (mLock!=null) {
synchronized (mLock) {