]> git.basschouten.com Git - openhab-addons.git/blob
8df35ecb57aef15f5f72d1785bde632376bdcc71
[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;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
19  * A {@link ChannelConfig} is required for the {@link ChannelState} object.
20  * For easily creating a configuration, use this builder.
21  *
22  * @author David Graeff - Initial contribution
23  */
24 @NonNullByDefault
25 public class ChannelConfigBuilder {
26     private final ChannelConfig config = new ChannelConfig();
27
28     private ChannelConfigBuilder() {
29     }
30
31     public static ChannelConfigBuilder create() {
32         return new ChannelConfigBuilder();
33     }
34
35     public static ChannelConfigBuilder create(@Nullable String stateTopic, @Nullable String commandTopic) {
36         return new ChannelConfigBuilder().withStateTopic(stateTopic).withCommandTopic(commandTopic);
37     }
38
39     public ChannelConfig build() {
40         return config;
41     }
42
43     public ChannelConfigBuilder withFormatter(String formatter) {
44         config.formatBeforePublish = formatter;
45         return this;
46     }
47
48     public ChannelConfigBuilder withStateTopic(@Nullable String topic) {
49         if (topic != null) {
50             config.stateTopic = topic;
51         }
52         return this;
53     }
54
55     public ChannelConfigBuilder withCommandTopic(@Nullable String topic) {
56         if (topic != null) {
57             config.commandTopic = topic;
58         }
59         return this;
60     }
61
62     public ChannelConfigBuilder withStopCommandTopic(@Nullable String topic) {
63         if (topic != null) {
64             config.stopCommandTopic = topic;
65         }
66         return this;
67     }
68
69     public ChannelConfigBuilder withRetain(boolean retain) {
70         config.retained = retain;
71         return this;
72     }
73
74     public ChannelConfigBuilder withQos(@Nullable Integer qos) {
75         config.qos = qos;
76         return this;
77     }
78
79     public ChannelConfigBuilder makeTrigger(boolean trigger) {
80         config.trigger = trigger;
81         return this;
82     }
83 }