Add a generic check for configuration validator
Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
@@ -15,11 +15,8 @@ package org.eclipse.hawkbit.tenancy.configuration.validator;
|
||||
public class TenantConfigurationBooleanValidator implements TenantConfigurationValidator {
|
||||
|
||||
@Override
|
||||
public void validate(final Object tenantConfigurationValue) {
|
||||
if (tenantConfigurationValue instanceof Boolean) {
|
||||
return;
|
||||
}
|
||||
throw new TenantConfigurationValidatorException("The given configuration value is expected as a boolean.");
|
||||
public Class<?> validateToClass() {
|
||||
return Boolean.class;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,10 +43,7 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig
|
||||
|
||||
@Override
|
||||
public void validate(final Object tenantConfigurationObject) {
|
||||
if (!(tenantConfigurationObject instanceof String)) {
|
||||
throw new TenantConfigurationValidatorException("The given configuration value is expected as a string.");
|
||||
}
|
||||
|
||||
TenantConfigurationValidator.super.validate(tenantConfigurationObject);
|
||||
final String tenantConfigurationString = (String) tenantConfigurationObject;
|
||||
|
||||
final Duration tenantConfigurationValue;
|
||||
@@ -66,4 +63,9 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig
|
||||
durationHelper.durationToFormattedString(maxDuration)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> validateToClass() {
|
||||
return String.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,10 +15,7 @@ package org.eclipse.hawkbit.tenancy.configuration.validator;
|
||||
public class TenantConfigurationStringValidator implements TenantConfigurationValidator {
|
||||
|
||||
@Override
|
||||
public void validate(final Object tenantConfigurationValue) {
|
||||
if (tenantConfigurationValue instanceof String) {
|
||||
return;
|
||||
}
|
||||
throw new TenantConfigurationValidatorException("The given configuration value is expected as a String.");
|
||||
public Class<?> validateToClass() {
|
||||
return String.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,5 +22,21 @@ public interface TenantConfigurationValidator {
|
||||
* @throws TenantConfigurationValidatorException
|
||||
* is thrown, when parameter is invalid.
|
||||
*/
|
||||
void validate(Object tenantConfigurationValue) throws TenantConfigurationValidatorException;
|
||||
default void validate(final Object tenantConfigurationValue) throws TenantConfigurationValidatorException {
|
||||
if (tenantConfigurationValue != null
|
||||
&& validateToClass().isAssignableFrom(tenantConfigurationValue.getClass())) {
|
||||
return;
|
||||
}
|
||||
throw new TenantConfigurationValidatorException(
|
||||
"The given configuration value is expected as a " + validateToClass().getSimpleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the generic class to check the object
|
||||
*
|
||||
* @return the class to check the value
|
||||
*/
|
||||
default Class<?> validateToClass() {
|
||||
return Object.class;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user