2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nest.internal.sdm.dto;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * A resource name uniquely identifies a structure, room or device.
24 * @author Wouter Born - Initial contribution
27 public class SDMResourceName {
29 public enum SDMResourceNameType {
36 private static final Pattern PATTERN = Pattern
37 .compile("^enterprises/([^/]+)(/devices/([^/]+)|/structures/([^/]+)(/rooms/([^/]+))?)$");
39 public static final SDMResourceName NAMELESS = new SDMResourceName("");
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;
48 public SDMResourceName(String name) {
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);
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;
65 type = SDMResourceNameType.UNKNOWN;
72 type = SDMResourceNameType.UNKNOWN;
77 public int hashCode() {
80 return prime * result + name.hashCode();
84 public boolean equals(@Nullable Object obj) {
91 if (getClass() != obj.getClass()) {
94 return name.equals(((SDMResourceName) obj).name);
98 public String toString() {