org.zodiac.netty.http.headers.DurationHeader Maven / Gradle / Ivy
package org.zodiac.netty.http.headers;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.sdk.toolkit.util.lang.NumUtil;
final class DurationHeader extends AbstractHeader {
private final Logger log = LoggerFactory.getLogger(getClass());
DurationHeader(CharSequence name) {
super(Duration.class, name);
}
@Override
public CharSequence toCharSequence(Duration value) {
return Long.toString(Objects.requireNonNull(value, "value").getSeconds());
}
@Override
public Duration toValue(CharSequence value) {
try {
return Duration.of(NumUtil.parseLong(Objects.requireNonNull(value, "value")), ChronoUnit.SECONDS);
} catch (NumberFormatException nfe) {
log.warn(String.format("Bad duration header '%s'", value), nfe);
return Duration.ZERO;
}
}
}