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,14 +194,8 @@ public final class ConnectingConsoleInput implements ConsoleInput {
prompting = true; prompting = true;
} }
while (true) { while (true) {
synchronized (promptLock) { if (!checkPrompt()) {
if (!prompting) { return null;
if (interrupting) {
interrupting = false;
throw new InterruptedIOException("Prompt was interrupted");
}
return null;
}
} }
getConnection(0); getConnection(0);
boolean connect; boolean connect;
@ -210,25 +204,26 @@ public final class ConnectingConsoleInput implements ConsoleInput {
connect = connected != null; connect = connected != null;
actualConnected = connected; actualConnected = connected;
} }
if (connect) { if (!connect) {
try { continue;
final String res = actualConnected.prompt(message); }
synchronized (promptLock) { try {
if (prompting) { final String res = actualConnected.prompt(message);
prompting = false; synchronized (promptLock) {
return res; if (prompting) {
} prompting = false;
} return res;
} catch (final InterruptedIOException e) {
// The inner console was interrupted. This can mean we are
// disconnecting or actually interrupted.
if (disconnection) {
disconnection = false;
} else {
interrupting = false;
throw e;
} }
} }
} catch (final InterruptedIOException e) {
// The inner console was interrupted. This can mean we are
// disconnecting or actually interrupted.
if (disconnection) {
disconnection = false;
} else {
interrupting = false;
throw e;
}
} }
} }
} }
@ -245,14 +240,8 @@ public final class ConnectingConsoleInput implements ConsoleInput {
} }
final long end = System.currentTimeMillis() + timeout; final long end = System.currentTimeMillis() + timeout;
do { do {
synchronized (promptLock) { if (!checkPrompt()) {
if (!prompting) { break;
if (interrupting) {
interrupting = false;
throw new InterruptedIOException("Prompt was interrupted");
}
break;
}
} }
getConnection(timeout); getConnection(timeout);
boolean connect; boolean connect;
@ -261,31 +250,48 @@ public final class ConnectingConsoleInput implements ConsoleInput {
connect = connected != null; connect = connected != null;
actualConnected = connected; actualConnected = connected;
} }
if (connect) { if (!connect) {
try { continue;
final String res = actualConnected.prompt(message, }
end - System.currentTimeMillis()); try {
synchronized (promptLock) { final String res = actualConnected.prompt(message,
if (prompting) { end - System.currentTimeMillis());
prompting = false; synchronized (promptLock) {
return res; if (prompting) {
} prompting = false;
} return res;
} catch (final InterruptedIOException e) {
// The inner console was interrupted. This can mean we are
// disconnecting or actually interrupted.
if (disconnection) {
disconnection = false;
} else {
interrupting = false;
throw e;
} }
} }
} catch (final InterruptedIOException e) {
// The inner console was interrupted. This can mean we are
// disconnecting or actually interrupted.
if (disconnection) {
disconnection = false;
} else {
interrupting = false;
throw e;
}
} }
} while (System.currentTimeMillis() < end); } while (System.currentTimeMillis() < end);
return null; 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 { private void getConnection(final long timeout) throws InterruptedIOException {
boolean connect; boolean connect;
synchronized (connectionLock) { synchronized (connectionLock) {