public Set<String> getMatchingKeys(String regex) { return getMatchingKeys(key -> key.matches(regex)); }
/** * A convenience method that calls {@link #executeWithPropertiesConfigurator(Consumer, Consumer)} * with a configurator that takes all terminal properties with the given prefix * and applies them after stripping the prefix from their keys. * <br>For example, if {@code textio.properties contains}: * <pre> * textio.prompt.color = green * textio.error.prompt.color = red * </pre> * then the following statement: * <pre> * textTerminal.executeWithPropertiesPrefix("error", t -> t.println("Connection failed.")); * </pre> * will display the message in red. * @param prefix the prefix of the terminal properties whose values will be temporarily used. * @param action the action to be performed on the TextTerminal. */ default void executeWithPropertiesPrefix(String prefix, Consumer<TextTerminal<T>> action) { executeWithPropertiesConfigurator(t -> { Set<String> keys = t.getMatchingKeys(key -> key.startsWith(prefix + ".")); int len = prefix.length() + 1; keys.forEach(key -> { String baseKey = key.substring(len); t.put(baseKey, t.getString(key)); }); }, action); } }
/** Convenience method that calls {@link #withPropertiesConfigurator(Consumer)} * with a configurator that takes all terminal properties with the given prefix * and applies them after stripping the prefix from their keys. * <br>For example, if {@code textio.properties} contains: * <pre> * textio.prompt.color = green * textio.input.color = yellow * textio.warn.prompt.color = red * textio.warn.input.color = orange * </pre> * then the following statement: * <pre> * textIO.newBooleanInputReader() * .withPropertiesPrefix("warn") * .read("Erase all data?"); * </pre> * will display the question in red and the user input in orange. **/ @SuppressWarnings("unchecked") public B withPropertiesPrefix(String prefix) { return withPropertiesConfigurator(t -> { Set<String> keys = t.getMatchingKeys(key -> key.startsWith(prefix + ".")); int len = prefix.length() + 1; keys.forEach(key -> { String baseKey = key.substring(len); t.put(baseKey, t.getString(key)); }); }); }