Skip to content

Commit

Permalink
Merge pull request #19 from bosch-iot-things/feature/consolidate-enti…
Browse files Browse the repository at this point in the history
…tytype-resourcetype

Feature/consolidate entitytype resourcetype
  • Loading branch information
stmaute authored and GitHub Enterprise committed Apr 19, 2021
2 parents 8c516f4 + a2c092d commit a47d0d4
Show file tree
Hide file tree
Showing 191 changed files with 1,828 additions and 1,912 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2021 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.model.base.entity.id;

import static org.eclipse.ditto.model.base.common.ConditionChecker.argumentNotEmpty;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;

import java.util.Objects;

import javax.annotation.Nullable;

import org.eclipse.ditto.model.base.entity.type.EntityType;


/**
* Abstract base class for instances of @{code EntityId}.
*
* @since 2.0.0
*/
public abstract class AbstractEntityId implements EntityId {

private final EntityType entityType;
private final String id;

/**
* Constructs a new AbstractEntityId object.
*
* @param entityType the entity type.
* @param id the id of the entity.
* @throws NullPointerException if {@code id} or {@code entityType} is {@code null}.
* @throws IllegalArgumentException if {@code id} is empty.
*/
protected AbstractEntityId(final EntityType entityType, final CharSequence id) {
this.entityType = checkNotNull(entityType, "entityType");
this.id = argumentNotEmpty(id, "entityId").toString();
}

@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final AbstractEntityId that = (AbstractEntityId) o;
return Objects.equals(id, that.id) && Objects.equals(entityType, that.entityType);
}

@Override
public int hashCode() {
return Objects.hash(entityType, id);
}

@Override
public String toString() {
return id;
}

@Override
public EntityType getEntityType() {
return entityType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2021 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.model.base.entity.id;

import static org.eclipse.ditto.model.base.entity.id.RegexPatterns.NAMESPACE_DELIMITER;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.model.base.entity.type.EntityType;
import org.eclipse.ditto.model.base.entity.validation.EntityIdPatternValidator;

/**
* Base implementation for namespaced entity IDs which are aware of their entity type.
* <em>Subclasses are required to be immutable.</em>
*
* @since 2.0.0
*/
@Immutable
public abstract class AbstractNamespacedEntityId extends AbstractEntityId implements NamespacedEntityId {

private final String namespace;
private final String name;

/**
* Constructs a new AbstractNamespacedEntityId object.
*
* @param entityType the entity type to delegate to.
* @param namespace the entity type to delegate to.
* @param name the entity type to delegate to.
* @param shouldValidate the entity type to delegate to.
* @throws NullPointerException if {@code id} or {@code entityType} is {@code null}.
* @throws IllegalArgumentException if {@code id} is empty.
*/
protected AbstractNamespacedEntityId(final EntityType entityType, final String namespace, final String name,
final boolean shouldValidate) {
super(entityType, shouldValidate ? validate(namespace, name) : namespace + NAMESPACE_DELIMITER + name);

this.namespace = namespace;
this.name = name;
}

protected AbstractNamespacedEntityId(final EntityType entityType, @Nullable final CharSequence entityId) {
super(entityType, getNonEmptyEntityId(entityId));

final EntityIdPatternValidator validator = EntityIdPatternValidator.getInstance(entityId);
if (validator.isValid()) {
// the given entityId is valid, so we can safely split at NAMESPACE_DELIMITER
final String[] elements = entityId.toString().split(NAMESPACE_DELIMITER, 2);
namespace = elements[0];
name = elements[1];
} else {
throw NamespacedEntityIdInvalidException.newBuilder(entityId).build();
}
}

private static CharSequence getNonEmptyEntityId(@Nullable final CharSequence entityId) {
if (entityId == null || entityId.toString().isEmpty()) {
throw NamespacedEntityIdInvalidException.newBuilder(entityId).build();
}
return entityId;
}

private static String validate(final @Nullable String namespace, final @Nullable String name) {
final String sp = namespace + NAMESPACE_DELIMITER + name;

if (namespace == null || name == null || !EntityIdPatternValidator.getInstance(sp).isValid()) {
throw NamespacedEntityIdInvalidException.newBuilder(sp).build();
}

return sp;
}

@Override
public String getName() {
return name;
}

@Override
public String getNamespace() {
return namespace;
}

}

This file was deleted.

0 comments on commit a47d0d4

Please sign in to comment.