]> git.basschouten.com Git - openhab-addons.git/blob
9b5f3aab601fc88ee2cc2ef2d53861e12c6428b5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 java.util.Collections;
16 import java.util.Set;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.library.CoreItemFactory;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.CommandDescriptionBuilder;
27 import org.openhab.core.types.CommandOption;
28 import org.openhab.core.types.StateDescriptionFragmentBuilder;
29 import org.openhab.core.types.StateOption;
30
31 /**
32  * Implements a text/string value. Allows to restrict the incoming value to a set of states.
33  *
34  * @author David Graeff - Initial contribution
35  */
36 @NonNullByDefault
37 public class TextValue extends Value {
38     private final @Nullable Set<String> states;
39
40     /**
41      * Create a string value with a limited number of allowed states.
42      *
43      * @param states Allowed states. Empty states are filtered out. If the resulting set is empty, all string values
44      *            will be allowed.
45      */
46     public TextValue(String[] states) {
47         super(CoreItemFactory.STRING, Collections.singletonList(StringType.class));
48         Set<String> s = Stream.of(states).filter(e -> StringUtils.isNotBlank(e)).collect(Collectors.toSet());
49         if (!s.isEmpty()) {
50             this.states = s;
51         } else {
52             this.states = null;
53         }
54     }
55
56     public TextValue() {
57         super(CoreItemFactory.STRING, Collections.singletonList(StringType.class));
58         this.states = null;
59     }
60
61     @Override
62     public void update(Command command) throws IllegalArgumentException {
63         final Set<String> states = this.states;
64         String valueStr = command.toString();
65         if (states != null && !states.contains(valueStr)) {
66             throw new IllegalArgumentException("Value " + valueStr + " not within range");
67         }
68         state = new StringType(valueStr);
69     }
70
71     /**
72      * @return valid states. Can be null.
73      */
74     public @Nullable Set<String> getStates() {
75         return states;
76     }
77
78     @Override
79     public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
80         StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
81         final Set<String> states = this.states;
82         if (states != null) {
83             for (String state : states) {
84                 builder = builder.withOption(new StateOption(state, state));
85             }
86         }
87         return builder;
88     }
89
90     @Override
91     public CommandDescriptionBuilder createCommandDescription() {
92         CommandDescriptionBuilder builder = super.createCommandDescription();
93         final Set<String> commands = this.states;
94         if (states != null) {
95             for (String command : commands) {
96                 builder = builder.withCommandOption(new CommandOption(command, command));
97             }
98         }
99         return builder;
100     }
101 }