Made duration helper a static utility class

This commit is contained in:
Kai Zimmermann
2016-03-15 13:06:47 +01:00
parent 7473c1b226
commit 40e8fdf5e0
6 changed files with 27 additions and 30 deletions

View File

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

View File

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

View File

@@ -6,7 +6,7 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit;
package org.eclipse.hawkbit.repository;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

View File

@@ -10,7 +10,6 @@ package org.eclipse.hawkbit.repository;
import java.util.List;
import org.eclipse.hawkbit.RolloutProperties;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.slf4j.Logger;

View File

@@ -122,9 +122,6 @@ public class TargetInfo implements Persistable<Long>, Serializable {
@Column(name = "request_controller_attributes", nullable = false)
private boolean requestControllerAttributes = true;
@Transient
private final DurationHelper durationHelper = new DurationHelper();
/**
* Constructor for {@link TargetStatus}.
*
@@ -324,9 +321,9 @@ public class TargetInfo implements Persistable<Long>, Serializable {
return null;
}
final Duration pollTime = durationHelper.formattedStringToDuration(TenantConfigurationManagement.getInstance()
final Duration pollTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagement.getInstance()
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class).getValue());
final Duration overdueTime = durationHelper.formattedStringToDuration(TenantConfigurationManagement
final Duration overdueTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagement
.getInstance().getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class)
.getValue());
final LocalDateTime currentDate = LocalDateTime.now();

View File

@@ -43,7 +43,7 @@ public class PollingConfigurationView extends BaseConfigurationView
private I18N i18n;
@Autowired
private ControllerPollProperties controllerPollProperties;
private transient ControllerPollProperties controllerPollProperties;
@Autowired
private transient TenantConfigurationManagement tenantConfigurationManagement;
@@ -59,31 +59,29 @@ public class PollingConfigurationView extends BaseConfigurationView
private Duration tenantPollTime = null;
private Duration tenantOverdueTime = null;
private final DurationHelper durationHelper = new DurationHelper();
/**
* Initialize Authentication Configuration layout.
*/
@PostConstruct
public void init() {
minDuration = durationHelper.formattedStringToDuration(controllerPollProperties.getMinPollingTime());
maxDuration = durationHelper.formattedStringToDuration(controllerPollProperties.getMaxPollingTime());
globalPollTime = durationHelper.formattedStringToDuration(tenantConfigurationManagement
minDuration = DurationHelper.formattedStringToDuration(controllerPollProperties.getMinPollingTime());
maxDuration = DurationHelper.formattedStringToDuration(controllerPollProperties.getMaxPollingTime());
globalPollTime = DurationHelper.formattedStringToDuration(tenantConfigurationManagement
.getGlobalConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class));
globalOverdueTime = durationHelper.formattedStringToDuration(tenantConfigurationManagement
globalOverdueTime = DurationHelper.formattedStringToDuration(tenantConfigurationManagement
.getGlobalConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class));
final TenantConfigurationValue<String> pollTimeConfValue = tenantConfigurationManagement
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class);
if (!pollTimeConfValue.isGlobal()) {
tenantPollTime = durationHelper.formattedStringToDuration(pollTimeConfValue.getValue());
tenantPollTime = DurationHelper.formattedStringToDuration(pollTimeConfValue.getValue());
}
final TenantConfigurationValue<String> overdueTimeConfValue = tenantConfigurationManagement
.getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class);
if (!overdueTimeConfValue.isGlobal()) {
tenantOverdueTime = durationHelper.formattedStringToDuration(overdueTimeConfValue.getValue());
tenantOverdueTime = DurationHelper.formattedStringToDuration(overdueTimeConfValue.getValue());
}
final Panel rootPanel = new Panel();
@@ -133,7 +131,7 @@ public class PollingConfigurationView extends BaseConfigurationView
tenantConfigurationManagement.deleteConfiguration(key);
} else {
tenantConfigurationManagement.addOrUpdateConfiguration(key,
durationHelper.durationToFormattedString(duration));
DurationHelper.durationToFormattedString(duration));
}
}