Created
August 28, 2022 00:46
-
-
Save mattnicee7/fdbcbf8d8be98f004cb2ee010ffc837b to your computer and use it in GitHub Desktop.
Customize your `java.util.Scanner` and fix buffering issues.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream; | |
import java.math.BigDecimal; | |
import java.math.BigInteger; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Objects; | |
import java.util.Scanner; | |
import java.util.function.Supplier; | |
/** | |
* Author of the original idea of this class: https://github.com/matheusmv | |
**/ | |
public class ConsoleReader implements AutoCloseable { | |
private final Scanner inputReader; | |
private final Map<Class<?>, Supplier<?>> parsers = new HashMap<>(); | |
public ConsoleReader(InputStream inputStream) { | |
Objects.requireNonNull(inputStream, "The inputStream cannot be null."); | |
this.inputReader = new Scanner(inputStream); | |
createNewParser(Boolean.class, () -> Boolean.parseBoolean(nextLine())); | |
createNewParser(Integer.class, () -> Integer.parseInt(nextLine())); | |
createNewParser(Float.class, () -> Float.parseFloat(nextLine())); | |
createNewParser(Double.class, () -> Double.parseDouble(nextLine())); | |
createNewParser(Byte.class, () -> Byte.parseByte(nextLine())); | |
createNewParser(Short.class, () -> Short.parseShort(nextLine())); | |
createNewParser(Long.class, () -> Long.parseLong(nextLine())); | |
createNewParser(Character.class, () -> { | |
final Character character = inputReader.next().charAt(0); | |
nextLine(); | |
return character; | |
}); | |
createNewParser(BigDecimal.class, () -> new BigDecimal(nextLine())); | |
createNewParser(BigInteger.class, () -> new BigInteger(nextLine())); | |
createNewParser(String.class, inputReader::nextLine); | |
} | |
public void createNewParser(Class<?> clazz, Supplier<?> supplier) { | |
Objects.requireNonNull(clazz, "The class cannot be null"); | |
Objects.requireNonNull(supplier, "The supplier cannot be null"); | |
if (this.parsers.containsKey(clazz)) | |
throw new IllegalArgumentException("This parser already exists"); | |
this.parsers.put(clazz, supplier); | |
} | |
public <T> T read(Class<T> clazz) { | |
Objects.requireNonNull(clazz, "The target class cannot be null"); | |
if (!this.parsers.containsKey(clazz)) | |
throw new IllegalArgumentException("This class does not have a parser."); | |
return (T) this.parsers.get(clazz).get(); | |
} | |
public String nextLine() { | |
return inputReader.nextLine(); | |
} | |
@Override | |
public void close() { | |
try { | |
this.inputReader.close(); | |
} catch (IllegalStateException exception) { | |
exception.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Pattern; | |
public class ExampleUsage { | |
public static void main(String[] args) { | |
try (final ConsoleReader consoleReader = new ConsoleReader(System.in)) { | |
final Double exampleDoubleValue = consoleReader.read(Double.class); | |
final Integer exampleIntegerValue = consoleReader.read(Integer.class); | |
System.out.println(exampleDoubleValue); | |
System.out.println(exampleIntegerValue); | |
consoleReader.createNewParser(Pattern.class, () -> Pattern.compile(consoleReader.nextLine())); | |
final Pattern pattern = consoleReader.read(Pattern.class); | |
final String checkIfMatches = consoleReader.read(String.class); | |
System.out.println(pattern.matcher(checkIfMatches).matches()); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment