Reduce branching depth

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-12-02 14:19:54 -05:00
parent 6b2a25674d
commit 0cef23e17b

View File

@ -194,15 +194,9 @@ public final class ConnectingConsoleInput implements ConsoleInput {
prompting = true;
}
while (true) {
synchronized (promptLock) {
if (!prompting) {
if (interrupting) {
interrupting = false;
throw new InterruptedIOException("Prompt was interrupted");
}
if (!checkPrompt()) {
return null;
}
}
getConnection(0);
boolean connect;
ConsoleInput actualConnected;
@ -210,7 +204,9 @@ public final class ConnectingConsoleInput implements ConsoleInput {
connect = connected != null;
actualConnected = connected;
}
if (connect) {
if (!connect) {
continue;
}
try {
final String res = actualConnected.prompt(message);
synchronized (promptLock) {
@ -231,7 +227,6 @@ public final class ConnectingConsoleInput implements ConsoleInput {
}
}
}
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String, long) */
@ -245,15 +240,9 @@ public final class ConnectingConsoleInput implements ConsoleInput {
}
final long end = System.currentTimeMillis() + timeout;
do {
synchronized (promptLock) {
if (!prompting) {
if (interrupting) {
interrupting = false;
throw new InterruptedIOException("Prompt was interrupted");
}
if (!checkPrompt()) {
break;
}
}
getConnection(timeout);
boolean connect;
ConsoleInput actualConnected;
@ -261,7 +250,9 @@ public final class ConnectingConsoleInput implements ConsoleInput {
connect = connected != null;
actualConnected = connected;
}
if (connect) {
if (!connect) {
continue;
}
try {
final String res = actualConnected.prompt(message,
end - System.currentTimeMillis());
@ -281,11 +272,26 @@ public final class ConnectingConsoleInput implements ConsoleInput {
throw e;
}
}
}
} while (System.currentTimeMillis() < end);
return null;
}
/** Test if we are in prompting state.
*
* @return if the process is currently in prompting state.
* @throws InterruptedIOException if the prompting state has been interrupted */
private boolean checkPrompt() throws InterruptedIOException {
synchronized (promptLock) {
if (!prompting) {
if (interrupting) {
interrupting = false;
throw new InterruptedIOException("Prompt was interrupted");
}
}
return prompting;
}
}
private void getConnection(final long timeout) throws InterruptedIOException {
boolean connect;
synchronized (connectionLock) {