Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetInputReader same as Enum but dynamic, to create dynamic menu #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions text-io/src/main/java/org/beryx/textio/SetInputReader.java
Original file line number Diff line number Diff line change
@@ -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<T> extends InputReader<T, SetInputReader<T>> {

private final Map<String, T> enumValues = new LinkedHashMap();

public SetInputReader(Supplier<TextTerminal<?>> textTerminalSupplier, Set<T> list) {
super(textTerminalSupplier);
for (T value : list) {
enumValues.put(value.toString(), value);
}

this.possibleValues = new ArrayList(enumValues.values());
this.numberedPossibleValues = true;
}

public SetInputReader<T> withAllValues() {
return withPossibleValues(new ArrayList(enumValues.values()));
}

public SetInputReader<T> withAllValuesNumbered() {
return withNumberedPossibleValues(new ArrayList(enumValues.values()));
}

public SetInputReader<T> withAllValuesInline() {
return withInlinePossibleValues(new ArrayList(enumValues.values()));
}

@Override
protected InputReader.ParseResult<T> 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);
}
}