package org.ehcache.impl.config;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.EvictionAdvisor;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.core.config.ExpiryUtils;
import org.ehcache.expiry.ExpiryPolicy;
import org.ehcache.spi.service.ServiceConfiguration;
import static org.ehcache.config.builders.CacheConfigurationBuilder.newCacheConfigurationBuilder;
public class BaseCacheConfiguration<K, V> implements CacheConfiguration<K,V> {
private final Class<K> keyType;
private final Class<V> valueType;
private final EvictionAdvisor<? super K, ? super V> evictionAdvisor;
private final Collection<ServiceConfiguration<?, ?>> serviceConfigurations;
private final ClassLoader classLoader;
private final ExpiryPolicy<? super K, ? super V> expiry;
private final ResourcePools resourcePools;
public BaseCacheConfiguration(Class<K> keyType, Class<V> valueType,
EvictionAdvisor<? super K, ? super V> evictionAdvisor,
ClassLoader classLoader, ExpiryPolicy<? super K, ? super V> expiry,
ResourcePools resourcePools, ServiceConfiguration<?, ?>... serviceConfigurations) {
if (keyType == null) {
throw new NullPointerException("keyType cannot be null");
}
if (valueType == null) {
throw new NullPointerException("valueType cannot be null");
}
if (resourcePools == null) {
throw new NullPointerException("resourcePools cannot be null");
}
this.keyType = keyType;
this.valueType = valueType;
this.evictionAdvisor = evictionAdvisor;
this.classLoader = classLoader;
if (expiry != null) {
this.expiry = expiry;
} else {
this.expiry = ExpiryPolicy.NO_EXPIRY;
}
this.resourcePools = resourcePools;
this.serviceConfigurations = Collections.unmodifiableCollection(Arrays.asList(serviceConfigurations));
}
@Override
public Collection<ServiceConfiguration<?, ?>> getServiceConfigurations() {
return serviceConfigurations;
}
@Override
public Class<K> getKeyType() {
return keyType;
}
@Override
public Class<V> getValueType() {
return valueType;
}
@Override
public EvictionAdvisor<? super K, ? super V> getEvictionAdvisor() {
return evictionAdvisor;
}
@Override
public ClassLoader getClassLoader() {
return classLoader;
}
@SuppressWarnings("deprecation")
@Override
public org.ehcache.expiry.Expiry<? super K, ? super V> getExpiry() {
return ExpiryUtils.convertToExpiry(expiry);
}
@Override
public ExpiryPolicy<? super K, ? super V> getExpiryPolicy() {
return expiry;
}
@Override
public ResourcePools getResourcePools() {
return resourcePools;
}
@Override
public CacheConfigurationBuilder<K, V> derive() {
return newCacheConfigurationBuilder(this);
}
}