Add a generic check for configuration validator

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-03-01 10:31:30 +01:00
parent 0e9991b0a4
commit e791954e13
4 changed files with 27 additions and 15 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}