package org.ehcache.jsr107;
import org.ehcache.core.config.ExpiryUtils;
import java.io.Closeable;
import java.io.IOException;
import java.util.function.Supplier;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ExpiryPolicy;
class ExpiryPolicyToEhcacheExpiry<K, V> extends Eh107Expiry<K, V> implements Closeable {
private final ExpiryPolicy expiryPolicy;
ExpiryPolicyToEhcacheExpiry(ExpiryPolicy expiryPolicy) {
this.expiryPolicy = expiryPolicy;
}
@Override
public java.time.Duration getExpiryForCreation(K key, V value) {
try {
Duration duration = expiryPolicy.getExpiryForCreation();
return convertDuration(duration);
} catch (Throwable t) {
return java.time.Duration.ZERO;
}
}
@Override
protected java.time.Duration getExpiryForAccessInternal(K key, Supplier<? extends V> value) {
try {
Duration duration = expiryPolicy.getExpiryForAccess();
if (duration == null) {
return null;
}
return convertDuration(duration);
} catch (Throwable t) {
return java.time.Duration.ZERO;
}
}
@Override
public java.time.Duration getExpiryForUpdate(K key, Supplier<? extends V> oldValue, V newValue) {
try {
Duration duration = expiryPolicy.getExpiryForUpdate();
if (duration == null) {
return null;
}
return convertDuration(duration);
} catch (Throwable t) {
return java.time.Duration.ZERO;
}
}
@Override
public void close() throws IOException {
if (expiryPolicy instanceof Closeable) {
((Closeable)expiryPolicy).close();
}
}
private java.time.Duration convertDuration(Duration duration) {
if (duration.isEternal()) {
return org.ehcache.expiry.ExpiryPolicy.INFINITE;
}
return java.time.Duration.of(duration.getDurationAmount(), ExpiryUtils.jucTimeUnitToTemporalUnit(duration.getTimeUnit()));
}
}