]> git.basschouten.com Git - openhab-addons.git/blob
ab36a7b3b738b02a95066eb348336a33c70d2463
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.onewire.internal.device;
14
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.BINDING_ID;
16
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.thing.type.ChannelTypeUID;
23
24 /**
25  * The {@link OwChannelConfig} class defines a map entry
26  *
27  * @author Jan N. Klug - Initial contribution
28  */
29 @NonNullByDefault
30 public class OwChannelConfig {
31     private static final Pattern CONFIG_PATTERN = Pattern.compile("^(.+):(.+):(.*)$");
32
33     public String channelId;
34     public ChannelTypeUID channelTypeUID;
35     public @Nullable String label;
36
37     public OwChannelConfig(String channelId, ChannelTypeUID channelTypeUID, @Nullable String label) {
38         this.channelId = channelId;
39         this.channelTypeUID = channelTypeUID;
40         this.label = label;
41     }
42
43     public OwChannelConfig(String channelId, ChannelTypeUID channelTypeUID) {
44         this(channelId, channelTypeUID, null);
45     }
46
47     public static OwChannelConfig fromString(String configString) {
48         Matcher matcher = CONFIG_PATTERN.matcher(configString);
49         if (matcher.matches()) {
50             if (matcher.group(3).trim().isEmpty()) {
51                 return new OwChannelConfig(matcher.group(1).trim(),
52                         new ChannelTypeUID(BINDING_ID, matcher.group(2).trim()));
53             } else {
54                 return new OwChannelConfig(matcher.group(1).trim(),
55                         new ChannelTypeUID(BINDING_ID, matcher.group(2).trim()), matcher.group(3).trim());
56             }
57         } else {
58             throw new IllegalArgumentException();
59         }
60     }
61
62     @Override
63     public String toString() {
64         return channelId + "/" + channelTypeUID.getAsString() + "/" + label;
65     }
66 }