-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingId.java
118 lines (102 loc) · 4.19 KB
/
ThingId.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.things.model;
import java.util.UUID;
import java.util.function.Supplier;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.entity.id.AbstractNamespacedEntityId;
import org.eclipse.ditto.base.model.entity.id.NamespacedEntityId;
import org.eclipse.ditto.base.model.entity.id.NamespacedEntityIdInvalidException;
import org.eclipse.ditto.base.model.entity.id.TypedEntityId;
/**
* Java representation of a validated Thing ID.
*/
@Immutable
@TypedEntityId(type = "thing")
public final class ThingId extends AbstractNamespacedEntityId {
/**
* Will be resolved to the actual default namespace inside ditto.
*/
private static final String DEFAULT_NAMESPACE = "";
private ThingId(final CharSequence thingId) {
super(ThingConstants.ENTITY_TYPE, thingId);
}
private ThingId(final String namespace, final String name, final boolean shouldValidate) {
super(ThingConstants.ENTITY_TYPE, namespace, name, shouldValidate);
}
/**
* Returns an instance of this class based on the given CharSequence.
* May return the same instance as the parameter if the given parameter is already a ThingId.
* Skips validation if the given {@code thingId} is an instance of NamespacedEntityId.
*
* @param thingId the thing ID.
* @return the ID.
* @throws ThingIdInvalidException if the given {@code thingId} is invalid.
*/
public static ThingId of(final CharSequence thingId) {
if (thingId instanceof ThingId) {
return (ThingId) thingId;
}
if (thingId instanceof NamespacedEntityId) {
final NamespacedEntityId namespacedEntityId = (NamespacedEntityId) thingId;
return new ThingId(namespacedEntityId.getNamespace(), namespacedEntityId.getName(), false);
}
return wrapInThingIdInvalidException(() -> new ThingId(thingId));
}
/**
* Returns an instance of this class with the given namespace and name.
*
* @param namespace the namespace of the thing.
* @param name the name of the thing.
* @return the created ID.
*/
public static ThingId of(final String namespace, final String name) {
return wrapInThingIdInvalidException(() -> new ThingId(namespace, name, true));
}
/**
* Generates a thing ID with a random unique name inside the given namespace.
*
* @param namespace the namespace of the thing.
* @return The generated unique thing ID.
* @throws ThingIdInvalidException if for the given {@code namespace} a ThingId cannot be derived.
* @since 3.0.0
*/
public static ThingId inNamespaceWithRandomName(final String namespace) {
return of(namespace, UUID.randomUUID().toString());
}
/**
* Returns an instance of this class with default namespace placeholder.
*
* @param name the name of the thing.
* @return the created ID.
* @throws ThingIdInvalidException if for the given {@code name} a ThingId cannot be derived.
*/
public static ThingId inDefaultNamespace(final String name) {
return wrapInThingIdInvalidException(() -> new ThingId(DEFAULT_NAMESPACE, name, true));
}
/**
* Generates a new thing ID with the default namespace placeholder and a unique name.
*
* @return the generated thing ID.
*/
public static ThingId generateRandom() {
return wrapInThingIdInvalidException(() -> new ThingId(DEFAULT_NAMESPACE, UUID.randomUUID().toString(), true));
}
private static <T> T wrapInThingIdInvalidException(final Supplier<T> supplier) {
try {
return supplier.get();
} catch (final NamespacedEntityIdInvalidException e) {
throw ThingIdInvalidException.newBuilder(e.getEntityId().orElse(null)).cause(e).build();
}
}
}