Skip to content

Commit 29f82ac

Browse files
refactor: Add util.Input
1 parent 0d8552f commit 29f82ac

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package net.marcellperger.mathexpr.interactive;
22

33
import java.util.Arrays;
4-
import java.util.Scanner;
4+
5+
import static net.marcellperger.mathexpr.util.Input.input;
56

67
public class Shell {
78
public static void main(String[] args) {
8-
Scanner sc = new Scanner(System.in);
99
System.out.println("args = " + Arrays.toString(args));
1010
System.out.print("Enter something: ");
11-
String s = sc.nextLine();
12-
System.out.println("You entered " + s);
11+
System.out.println("You entered " + input());
1312
}
1413
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package net.marcellperger.mathexpr.util;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.io.PrintStream;
9+
import java.util.Objects;
10+
import java.util.Scanner;
11+
12+
public class Input {
13+
@NotNull Scanner sc;
14+
@Nullable PrintStream ps;
15+
16+
public Input() {
17+
this(System.in, System.out);
18+
}
19+
public Input(@NotNull InputStream iStream) {
20+
this(new Scanner(iStream));
21+
}
22+
public Input(Readable readable) {
23+
this(new Scanner(readable));
24+
}
25+
public Input(String s) {
26+
this(new Scanner(s));
27+
}
28+
public Input(@NotNull Scanner scanner) {
29+
this(scanner, null);
30+
}
31+
public Input(InputStream iStream, @Nullable OutputStream oStream) {
32+
this(new Scanner(iStream), oStream);
33+
}
34+
public Input(@Nullable OutputStream oStream) {
35+
this(System.in, oStream);
36+
}
37+
public Input(@NotNull Scanner scanner, @Nullable OutputStream oStream) {
38+
this(scanner, oStream == null ? null : new PrintStream(oStream));
39+
}
40+
public Input(@NotNull Scanner scanner, @Nullable PrintStream pStream) {
41+
sc = scanner;
42+
ps = pStream;
43+
}
44+
45+
public String getInput() {
46+
return sc.nextLine();
47+
}
48+
public String getInput(String prompt) {
49+
Objects.requireNonNull(ps, "An OutputStream is required to use Input.getInput(prompt)").println(prompt);
50+
return sc.nextLine();
51+
}
52+
53+
public static String input() {
54+
// Don't cache `new Input()` - System.in could be changed
55+
return new Input().getInput();
56+
}
57+
public static String input(@NotNull InputStream iStream) {
58+
return new Input(iStream).getInput();
59+
}
60+
public static String input(String prompt) {
61+
return new Input().getInput(prompt);
62+
}
63+
public static String input(String prompt, @NotNull OutputStream oStream) {
64+
return new Input(oStream).getInput(prompt);
65+
}
66+
public static String input(String prompt, @NotNull InputStream iStream, @NotNull OutputStream oStream) {
67+
return new Input(iStream, oStream).getInput(prompt);
68+
}
69+
}

0 commit comments

Comments
 (0)