|
| 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