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 { public class TenantConfigurationBooleanValidator implements TenantConfigurationValidator {
@Override @Override
public void validate(final Object tenantConfigurationValue) { public Class<?> validateToClass() {
if (tenantConfigurationValue instanceof Boolean) { return Boolean.class;
return;
}
throw new TenantConfigurationValidatorException("The given configuration value is expected as a boolean.");
} }
} }

View File

@@ -43,10 +43,7 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig
@Override @Override
public void validate(final Object tenantConfigurationObject) { public void validate(final Object tenantConfigurationObject) {
if (!(tenantConfigurationObject instanceof String)) { TenantConfigurationValidator.super.validate(tenantConfigurationObject);
throw new TenantConfigurationValidatorException("The given configuration value is expected as a string.");
}
final String tenantConfigurationString = (String) tenantConfigurationObject; final String tenantConfigurationString = (String) tenantConfigurationObject;
final Duration tenantConfigurationValue; final Duration tenantConfigurationValue;
@@ -66,4 +63,9 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig
durationHelper.durationToFormattedString(maxDuration))); 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 { public class TenantConfigurationStringValidator implements TenantConfigurationValidator {
@Override @Override
public void validate(final Object tenantConfigurationValue) { public Class<?> validateToClass() {
if (tenantConfigurationValue instanceof String) { return String.class;
return;
}
throw new TenantConfigurationValidatorException("The given configuration value is expected as a String.");
} }
} }

View File

@@ -22,5 +22,21 @@ public interface TenantConfigurationValidator {
* @throws TenantConfigurationValidatorException * @throws TenantConfigurationValidatorException
* is thrown, when parameter is invalid. * 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;
}
} }