-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.java
37 lines (30 loc) · 1.1 KB
/
Utils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Scanner;
public class Utils {
/**
* Prompts the user for a port number using the given Scanner.
*
* @param inputScanner Scanner to use to prompt the user
* @param defaultPort a port number the user can default to
*/
public static int getPort(Scanner inputScanner, int defaultPort) {
int input;
do {
System.out.print("\nPlease select a port by entering an integer value between 1 and 65535 or\n");
System.out.print("insert \"0\" in order to continue with the default setting (" + defaultPort + "): ");
input = inputScanner.nextInt();
} while (input != 0 && !isValidPort(input));
return input == 0 ? defaultPort : input;
}
/**
* @param port number to check
* @return true if the given number is a valid port number, else false
*/
public static boolean isValidPort(int port) {
return port >= 1 && port <= 65535;
}
static void assertTrue(boolean condition, String msg) {
if (!condition) {
throw new RuntimeException(msg);
}
}
}