From 22a190d843b4d3c3fdf7040c64db9a5813addaff Mon Sep 17 00:00:00 2001 From: Azamat M Date: Fri, 20 Oct 2023 10:50:31 +0500 Subject: [PATCH] SetInputReader same as Enum but dynamic, to create dynamic menu --- .../java/org/beryx/textio/SetInputReader.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 text-io/src/main/java/org/beryx/textio/SetInputReader.java diff --git a/text-io/src/main/java/org/beryx/textio/SetInputReader.java b/text-io/src/main/java/org/beryx/textio/SetInputReader.java new file mode 100644 index 00000000..519ddf92 --- /dev/null +++ b/text-io/src/main/java/org/beryx/textio/SetInputReader.java @@ -0,0 +1,56 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.beryx.textio; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.function.Supplier; + +/** + * + * @author qo0p + */ +public class SetInputReader extends InputReader> { + + private final Map enumValues = new LinkedHashMap(); + + public SetInputReader(Supplier> textTerminalSupplier, Set list) { + super(textTerminalSupplier); + for (T value : list) { + enumValues.put(value.toString(), value); + } + + this.possibleValues = new ArrayList(enumValues.values()); + this.numberedPossibleValues = true; + } + + public SetInputReader withAllValues() { + return withPossibleValues(new ArrayList(enumValues.values())); + } + + public SetInputReader withAllValuesNumbered() { + return withNumberedPossibleValues(new ArrayList(enumValues.values())); + } + + public SetInputReader withAllValuesInline() { + return withInlinePossibleValues(new ArrayList(enumValues.values())); + } + + @Override + protected InputReader.ParseResult parse(String s) { + T value = enumValues.get(s); + if (value != null) { + return new ParseResult<>(value); + } + return new ParseResult(null, getErrorMessages(s)); + } + + public static SetInputReader newInstance(TextTerminal terminal, Set list) { + return new SetInputReader(() -> terminal, list); + } +}