Made duration helper a static utility class
This commit is contained in:
@@ -22,6 +22,10 @@ import java.time.temporal.TemporalAccessor;
|
||||
*/
|
||||
public class DurationHelper {
|
||||
|
||||
private DurationHelper() {
|
||||
// utility class
|
||||
}
|
||||
|
||||
/**
|
||||
* Format of the String expected in configuration file and in the database.
|
||||
*/
|
||||
@@ -35,7 +39,7 @@ public class DurationHelper {
|
||||
* @return String in the duration format, specified at
|
||||
* {@link #DURATION_FORMAT}
|
||||
*/
|
||||
public String durationToFormattedString(final Duration duration) {
|
||||
public static String durationToFormattedString(final Duration duration) {
|
||||
if (duration == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -52,7 +56,7 @@ public class DurationHelper {
|
||||
* @throws DateTimeParseException
|
||||
* when String is in wrong format
|
||||
*/
|
||||
public Duration formattedStringToDuration(final String formattedDuration) throws DateTimeParseException {
|
||||
public static Duration formattedStringToDuration(final String formattedDuration) {
|
||||
if (formattedDuration == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -72,11 +76,11 @@ public class DurationHelper {
|
||||
* count of seconds
|
||||
* @return duration
|
||||
*/
|
||||
public Duration getDurationByTimeValues(final long hours, final long minutes, final long seconds) {
|
||||
public static Duration getDurationByTimeValues(final long hours, final long minutes, final long seconds) {
|
||||
return Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds);
|
||||
}
|
||||
|
||||
public DurationRangeValidator durationRangeValidator(final Duration min, final Duration max) {
|
||||
public static DurationRangeValidator durationRangeValidator(final Duration min, final Duration max) {
|
||||
return new DurationRangeValidator(min, max);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
*/
|
||||
public class TenantConfigurationPollingDurationValidator implements TenantConfigurationValidator {
|
||||
|
||||
private final DurationHelper durationHelper = new DurationHelper();
|
||||
|
||||
private final Duration minDuration;
|
||||
|
||||
private final Duration maxDuration;
|
||||
@@ -36,8 +34,8 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig
|
||||
*/
|
||||
@Autowired
|
||||
public TenantConfigurationPollingDurationValidator(final ControllerPollProperties properties) {
|
||||
minDuration = durationHelper.formattedStringToDuration(properties.getMinPollingTime());
|
||||
maxDuration = durationHelper.formattedStringToDuration(properties.getMaxPollingTime());
|
||||
minDuration = DurationHelper.formattedStringToDuration(properties.getMinPollingTime());
|
||||
maxDuration = DurationHelper.formattedStringToDuration(properties.getMaxPollingTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -47,18 +45,19 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig
|
||||
|
||||
final Duration tenantConfigurationValue;
|
||||
try {
|
||||
tenantConfigurationValue = durationHelper.formattedStringToDuration(tenantConfigurationString);
|
||||
tenantConfigurationValue = DurationHelper.formattedStringToDuration(tenantConfigurationString);
|
||||
} catch (final DateTimeParseException ex) {
|
||||
throw new TenantConfigurationValidatorException(
|
||||
String.format("The given configuration value is expected as a string in the format %s.",
|
||||
DurationHelper.DURATION_FORMAT));
|
||||
DurationHelper.DURATION_FORMAT),
|
||||
ex);
|
||||
}
|
||||
|
||||
if (!durationHelper.durationRangeValidator(minDuration, maxDuration).isWithinRange(tenantConfigurationValue)) {
|
||||
if (!DurationHelper.durationRangeValidator(minDuration, maxDuration).isWithinRange(tenantConfigurationValue)) {
|
||||
throw new TenantConfigurationValidatorException(
|
||||
String.format("The given configuration value is not in the allowed range from %s to %s.",
|
||||
durationHelper.durationToFormattedString(minDuration),
|
||||
durationHelper.durationToFormattedString(maxDuration)));
|
||||
DurationHelper.durationToFormattedString(minDuration),
|
||||
DurationHelper.durationToFormattedString(maxDuration)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user