javax.cache.expiry.TouchedExpiryPolicy Maven / Gradle / Ivy
package javax.cache.expiry;
import javax.cache.configuration.Factory;
import javax.cache.configuration.FactoryBuilder;
import java.io.Serializable;
/**
* An {@link ExpiryPolicy} that defines the expiry {@link Duration}
* of a Cache Entry based on when it was last touched. A touch includes
* creation, update or access.
*
* @author Greg Luck
* @author Brian Oliver
* @since 1.0
* @see ExpiryPolicy
*/
public final class TouchedExpiryPolicy implements ExpiryPolicy, Serializable {
/**
* The serialVersionUID required for {@link java.io.Serializable}.
*/
public static final long serialVersionUID = 201305291023L;
/**
* The {@link Duration} a Cache Entry should be available before it expires.
*/
private Duration expiryDuration;
/**
* Constructs an {@link TouchedExpiryPolicy} {@link ExpiryPolicy}.
*
* @param expiryDuration the {@link Duration} a Cache Entry should exist be
* before it expires after being modified
*/
public TouchedExpiryPolicy(Duration expiryDuration) {
this.expiryDuration = expiryDuration;
}
/**
* Obtains a {@link Factory} for a Touched {@link ExpiryPolicy}.
*
* @return a {@link Factory} for a Touched {@link ExpiryPolicy}.
*/
public static Factory factoryOf(Duration duration) {
return new FactoryBuilder.SingletonFactory(new TouchedExpiryPolicy(duration));
}
/**
* {@inheritDoc}
*/
@Override
public Duration getExpiryForCreation() {
//for newly created entries we use the specified expiry duration.
return expiryDuration;
}
/**
* {@inheritDoc}
*/
@Override
public Duration getExpiryForAccess() {
//accessing a cache entry resets the duration.
return expiryDuration;
}
/**
* {@inheritDoc}
*/
@Override
public Duration getExpiryForUpdate() {
//updating a cache entry resets the 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 object) {
if (this == object) {
return true;
}
if (object == null) {
return false;
}
if (!(object instanceof TouchedExpiryPolicy)) {
return false;
}
TouchedExpiryPolicy other = (TouchedExpiryPolicy) object;
if (expiryDuration == null) {
if (other.expiryDuration != null) {
return false;
}
} else if (!expiryDuration.equals(other.expiryDuration)) {
return false;
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy