javax.cache.expiry.ModifiedExpiryPolicy Maven / Gradle / Ivy
package javax.cache.expiry;
import javax.cache.Cache;
import javax.cache.configuration.Factory;
import javax.cache.configuration.FactoryBuilder;
import java.io.Serializable;
/**
* An {@link javax.cache.expiry.ExpiryPolicy} that defines the expiry {@link Duration}
* of a Cache Entry based on the last time it was modified. Modified
* includes created and updated.
*
* @param the type of cache keys
* @param the type of cache values
*/
public final class ModifiedExpiryPolicy implements ExpiryPolicy, Serializable {
/**
* The serialVersionUID required for {@link java.io.Serializable}.
*/
public static final long serialVersionUID = 201305101602L;
/**
* The {@link Duration} a Cache Entry should be available before it expires.
*/
private Duration expiryDuration;
/**
* Constructs an {@link ModifiedExpiryPolicy} {@link javax.cache.expiry.ExpiryPolicy}.
*
* @param expiryDuration the {@link Duration} a Cache Entry should exist be
* before it expires after being modified
*/
public ModifiedExpiryPolicy(Duration expiryDuration) {
this.expiryDuration = expiryDuration;
}
/**
* Obtains a {@link javax.cache.configuration.Factory} for a Modified {@link javax.cache.expiry.ExpiryPolicy}.
*
* @return a {@link javax.cache.configuration.Factory} for a Modified {@link javax.cache.expiry.ExpiryPolicy}.
*/
public static Factory> factoryOf(Duration duration) {
return new FactoryBuilder.SingletonFactory>(new ModifiedExpiryPolicy(duration));
}
/**
* {@inheritDoc}
*/
@Override
public Duration getExpiryForCreatedEntry(Cache.Entry extends K, ? extends V> entry) {
//for newly created entries we use the specified expiry duration
return expiryDuration;
}
/**
* {@inheritDoc}
*/
@Override
public Duration getExpiryForAccessedEntry(Cache.Entry extends K, ? extends V> entry) {
//accessing a cache entry has no affect on the current expiry duration
return null;
}
/**
* {@inheritDoc}
*/
@Override
public Duration getExpiryForModifiedEntry(Cache.Entry extends K, ? extends V> entry) {
//when a cache entry is modified, we return the specified expiry duration,
//ignoring the current expiry duration
return expiryDuration;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((expiryDuration == null) ? 0 : expiryDuration.hashCode());
return result;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof ModifiedExpiryPolicy)) {
return false;
}
ModifiedExpiryPolicy, ?> other = (ModifiedExpiryPolicy, ?>) obj;
if (expiryDuration == null) {
if (other.expiryDuration != null) {
return false;
}
} else if (!expiryDuration.equals(other.expiryDuration)) {
return false;
}
return true;
}
}