]> git.basschouten.com Git - openhab-addons.git/blob
87f2fea9601408d357f72628d6397dcbeb701df0
[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.nest.internal.sdm.dto;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * A resource name uniquely identifies a structure, room or device.
23  *
24  * @author Wouter Born - Initial contribution
25  */
26 @NonNullByDefault
27 public class SDMResourceName {
28
29     public enum SDMResourceNameType {
30         DEVICE,
31         ROOM,
32         STRUCTURE,
33         UNKNOWN
34     }
35
36     private static final Pattern PATTERN = Pattern
37             .compile("^enterprises/([^/]+)(/devices/([^/]+)|/structures/([^/]+)(/rooms/([^/]+))?)$");
38
39     public static final SDMResourceName NAMELESS = new SDMResourceName("");
40
41     public final String name;
42     public final String projectId;
43     public final String deviceId;
44     public final String structureId;
45     public final String roomId;
46     public final SDMResourceNameType type;
47
48     public SDMResourceName(String name) {
49         this.name = name;
50
51         Matcher matcher = PATTERN.matcher(name);
52         if (matcher.matches()) {
53             projectId = matcher.group(1);
54             deviceId = matcher.group(3) == null ? "" : matcher.group(3);
55             structureId = matcher.group(4) == null ? "" : matcher.group(4);
56             roomId = matcher.group(6) == null ? "" : matcher.group(6);
57
58             if (!deviceId.isEmpty()) {
59                 type = SDMResourceNameType.DEVICE;
60             } else if (!roomId.isEmpty()) {
61                 type = SDMResourceNameType.ROOM;
62             } else if (!structureId.isEmpty()) {
63                 type = SDMResourceNameType.STRUCTURE;
64             } else {
65                 type = SDMResourceNameType.UNKNOWN;
66             }
67         } else {
68             projectId = "";
69             deviceId = "";
70             structureId = "";
71             roomId = "";
72             type = SDMResourceNameType.UNKNOWN;
73         }
74     }
75
76     @Override
77     public int hashCode() {
78         final int prime = 31;
79         int result = 1;
80         return prime * result + name.hashCode();
81     }
82
83     @Override
84     public boolean equals(@Nullable Object obj) {
85         if (this == obj) {
86             return true;
87         }
88         if (obj == null) {
89             return false;
90         }
91         if (getClass() != obj.getClass()) {
92             return false;
93         }
94         return name.equals(((SDMResourceName) obj).name);
95     }
96
97     @Override
98     public String toString() {
99         return name;
100     }
101 }