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;
32 import org.openhab.core.types.UnDefType;
35 * Implements a text/string value. Allows to restrict the incoming value to a set of states.
37 * @author David Graeff - Initial contribution
40 public class TextValue extends Value {
41 private final @Nullable Set<String> states;
42 private final @Nullable Set<String> commands;
44 protected @Nullable String nullValue = null;
47 * Create a string value with a limited number of allowed states and commands.
49 * @param states Allowed states. Empty states are filtered out. If the resulting set is empty, all string values
51 * @param commands Allowed commands. Empty commands are filtered out. If the resulting set is empty, all string
52 * values will be allowed.
54 public TextValue(String[] states, String[] commands) {
55 super(CoreItemFactory.STRING, List.of(StringType.class));
56 Set<String> s = Stream.of(states).filter(not(String::isBlank)).collect(Collectors.toSet());
62 Set<String> c = Stream.of(commands).filter(not(String::isBlank)).collect(Collectors.toSet());
71 * Create a string value with a limited number of allowed states.
73 * @param states Allowed states. Empty states are filtered out. If the resulting set is empty, all string values
74 * will be allowed. This same array is also used for allowed commands.
76 public TextValue(String[] states) {
81 super(CoreItemFactory.STRING, List.of(StringType.class));
86 public void setNullValue(@Nullable String nullValue) {
87 this.nullValue = nullValue;
91 public StringType parseCommand(Command command) throws IllegalArgumentException {
92 final Set<String> commands = this.commands;
93 String valueStr = command.toString();
94 if (commands != null && !commands.contains(valueStr)) {
95 throw new IllegalArgumentException("Value " + valueStr + " not within range");
97 return new StringType(valueStr);
101 public State parseMessage(Command command) throws IllegalArgumentException {
102 if (command instanceof StringType string && string.toString().equals(nullValue)) {
103 return UnDefType.NULL;
106 final Set<String> states = this.states;
107 String valueStr = command.toString();
108 if (states != null && !states.contains(valueStr)) {
109 throw new IllegalArgumentException("Value " + valueStr + " not within range");
111 return new StringType(valueStr);
115 * @return valid states. Can be null.
117 public @Nullable Set<String> getStates() {
122 public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
123 StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
124 final Set<String> states = this.states;
125 if (states != null) {
126 for (String state : states) {
127 builder = builder.withOption(new StateOption(state, state));
134 public CommandDescriptionBuilder createCommandDescription() {
135 CommandDescriptionBuilder builder = super.createCommandDescription();
136 final Set<String> commands = this.commands;
137 if (commands != null) {
138 for (String command : commands) {
139 builder = builder.withCommandOption(new CommandOption(command, command));