From 76e796e88c3da30c54bbd2311049380a21ab8999 Mon Sep 17 00:00:00 2001 From: Dejan Bosanac Date: Thu, 30 Apr 2020 14:12:43 +0200 Subject: [PATCH] Add properties to the TenantKey class to make it more reusable without inheritance --- .../service/tenant/TenantKey.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/services/device-registry-base/src/main/java/org/eclipse/hono/deviceregistry/service/tenant/TenantKey.java b/services/device-registry-base/src/main/java/org/eclipse/hono/deviceregistry/service/tenant/TenantKey.java index 92625659df..ddc882a482 100644 --- a/services/device-registry-base/src/main/java/org/eclipse/hono/deviceregistry/service/tenant/TenantKey.java +++ b/services/device-registry-base/src/main/java/org/eclipse/hono/deviceregistry/service/tenant/TenantKey.java @@ -12,6 +12,9 @@ *******************************************************************************/ package org.eclipse.hono.deviceregistry.service.tenant; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; import com.google.common.base.MoreObjects; @@ -26,6 +29,8 @@ public class TenantKey { private final String tenantId; private final String name; + private Map properties = new HashMap<>(); + /** * Creates a tenant key. @@ -57,6 +62,62 @@ public String getName() { return name; } + + /** + * Gets the properties of this tenant key. + * + * @return An unmodifiable view on the extension properties. + */ + public Map getProperties() { + return Collections.unmodifiableMap(properties); + } + + /** + * Sets the properties for this tenant key. + *

+ * Existing properties are completely replaced by the new properties. + * + * @param properties The properties. + * @return This instance, to allow chained invocations. + */ + public TenantKey setProperties(final Map properties) { + this.properties.clear(); + if (properties != null) { + this.properties.putAll(properties); + } + return this; + } + + /** + * Get a property from this tenant key. + * + * @param key The key of the entry. + * @return The property if found or {@code null} otherwise. + * @throws NullPointerException if the key argument is {@code null}. + */ + public Object getProperty(final String key) { + Objects.requireNonNull(key); + + return properties.get(key); + } + + /** + * Adds a property to this tenant key. + *

+ * If a property already exist for the specified key, the old value is replaced by the specified value. + * + * @param key The key of the entry. + * @param value The value of the entry. + * @return This instance, to allow chained invocations. + * @throws NullPointerException if any of the arguments are {@code null}. + */ + public TenantKey putProperty(final String key, final Object value) { + Objects.requireNonNull(key); + Objects.requireNonNull(value); + this.properties.put(key, value); + return this; + } + /** * Creates a tenant key from tenant identifier. *