2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.generic.values;
15 import static java.util.function.Predicate.not;
17 import java.util.List;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.library.CoreItemFactory;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.CommandDescriptionBuilder;
28 import org.openhab.core.types.CommandOption;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.StateDescriptionFragmentBuilder;
31 import org.openhab.core.types.StateOption;
34 * Implements a text/string value. Allows to restrict the incoming value to a set of states.
36 * @author David Graeff - Initial contribution
39 public class TextValue extends Value {
40 private final @Nullable Set<String> states;
41 private final @Nullable Set<String> commands;
44 * Create a string value with a limited number of allowed states and commands.
46 * @param states Allowed states. Empty states are filtered out. If the resulting set is empty, all string values
48 * @param commands Allowed commands. Empty commands are filtered out. If the resulting set is empty, all string
49 * values will be allowed.
51 public TextValue(String[] states, String[] commands) {
52 super(CoreItemFactory.STRING, List.of(StringType.class));
53 Set<String> s = Stream.of(states).filter(not(String::isBlank)).collect(Collectors.toSet());
59 Set<String> c = Stream.of(commands).filter(not(String::isBlank)).collect(Collectors.toSet());
68 * Create a string value with a limited number of allowed states.
70 * @param states Allowed states. Empty states are filtered out. If the resulting set is empty, all string values
71 * will be allowed. This same array is also used for allowed commands.
73 public TextValue(String[] states) {
78 super(CoreItemFactory.STRING, List.of(StringType.class));
84 public StringType parseCommand(Command command) throws IllegalArgumentException {
85 final Set<String> commands = this.commands;
86 String valueStr = command.toString();
87 if (commands != null && !commands.contains(valueStr)) {
88 throw new IllegalArgumentException("Value " + valueStr + " not within range");
90 return new StringType(valueStr);
94 public State parseMessage(Command command) throws IllegalArgumentException {
95 final Set<String> states = this.states;
96 String valueStr = command.toString();
97 if (states != null && !states.contains(valueStr)) {
98 throw new IllegalArgumentException("Value " + valueStr + " not within range");
100 return new StringType(valueStr);
104 * @return valid states. Can be null.
106 public @Nullable Set<String> getStates() {
111 public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
112 StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
113 final Set<String> states = this.states;
114 if (states != null) {
115 for (String state : states) {
116 builder = builder.withOption(new StateOption(state, state));
123 public CommandDescriptionBuilder createCommandDescription() {
124 CommandDescriptionBuilder builder = super.createCommandDescription();
125 final Set<String> commands = this.commands;
126 if (commands != null) {
127 for (String command : commands) {
128 builder = builder.withCommandOption(new CommandOption(command, command));