]> git.basschouten.com Git - openhab-addons.git/blob
08e958c63a77372c777f6f213be4717a1a0ded6f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.generic.values;
14
15 import static java.util.function.Predicate.not;
16
17 import java.util.List;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
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.StateDescriptionFragmentBuilder;
30 import org.openhab.core.types.StateOption;
31
32 /**
33  * Implements a text/string value. Allows to restrict the incoming value to a set of states.
34  *
35  * @author David Graeff - Initial contribution
36  */
37 @NonNullByDefault
38 public class TextValue extends Value {
39     private final @Nullable Set<String> states;
40
41     /**
42      * Create a string value with a limited number of allowed states.
43      *
44      * @param states Allowed states. Empty states are filtered out. If the resulting set is empty, all string values
45      *            will be allowed.
46      */
47     public TextValue(String[] states) {
48         super(CoreItemFactory.STRING, List.of(StringType.class));
49         Set<String> s = Stream.of(states).filter(not(String::isBlank)).collect(Collectors.toSet());
50         if (!s.isEmpty()) {
51             this.states = s;
52         } else {
53             this.states = null;
54         }
55     }
56
57     public TextValue() {
58         super(CoreItemFactory.STRING, List.of(StringType.class));
59         this.states = null;
60     }
61
62     @Override
63     public StringType parseCommand(Command command) throws IllegalArgumentException {
64         final Set<String> states = this.states;
65         String valueStr = command.toString();
66         if (states != null && !states.contains(valueStr)) {
67             throw new IllegalArgumentException("Value " + valueStr + " not within range");
68         }
69         return new StringType(valueStr);
70     }
71
72     /**
73      * @return valid states. Can be null.
74      */
75     public @Nullable Set<String> getStates() {
76         return states;
77     }
78
79     @Override
80     public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
81         StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
82         final Set<String> states = this.states;
83         if (states != null) {
84             for (String state : states) {
85                 builder = builder.withOption(new StateOption(state, state));
86             }
87         }
88         return builder;
89     }
90
91     @Override
92     public CommandDescriptionBuilder createCommandDescription() {
93         CommandDescriptionBuilder builder = super.createCommandDescription();
94         final Set<String> commands = this.states;
95         if (states != null) {
96             for (String command : commands) {
97                 builder = builder.withCommandOption(new CommandOption(command, command));
98             }
99         }
100         return builder;
101     }
102 }