package org.ehcache.config.builders;
import org.ehcache.config.Builder;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.Configuration;
import org.ehcache.core.config.DefaultConfiguration;
import org.ehcache.spi.service.ServiceCreationConfiguration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
public class ConfigurationBuilder implements Builder<Configuration> {
private final Map<String, CacheConfiguration<?, ?>> caches;
private final List<ServiceCreationConfiguration<?>> serviceConfigurations;
private final ClassLoader classLoader;
public static ConfigurationBuilder newConfigurationBuilder() {
return new ConfigurationBuilder();
}
private ConfigurationBuilder() {
this.caches = emptyMap();
this.serviceConfigurations = emptyList();
this.classLoader = null;
}
private ConfigurationBuilder(ConfigurationBuilder builder, Map<String, CacheConfiguration<?, ?>> caches) {
this.caches = unmodifiableMap(caches);
this.serviceConfigurations = builder.serviceConfigurations;
this.classLoader = builder.classLoader;
}
private ConfigurationBuilder(ConfigurationBuilder builder, List<ServiceCreationConfiguration<?>> serviceConfigurations) {
this.caches = builder.caches;
this.serviceConfigurations = unmodifiableList(serviceConfigurations);
this.classLoader = builder.classLoader;
}
private ConfigurationBuilder(ConfigurationBuilder builder, ClassLoader classLoader) {
this.caches = builder.caches;
this.serviceConfigurations = builder.serviceConfigurations;
this.classLoader = classLoader;
}
@Override
public Configuration build() {
return new DefaultConfiguration(caches, classLoader, serviceConfigurations.toArray(new ServiceCreationConfiguration<?>[serviceConfigurations.size()]));
}
public ConfigurationBuilder addCache(String alias, CacheConfiguration<?, ?> config) {
Map<String, CacheConfiguration<?, ?>> newCaches = new HashMap<>(caches);
if(newCaches.put(alias, config) != null) {
throw new IllegalArgumentException("Cache alias '" + alias + "' already exists");
}
return new ConfigurationBuilder(this, newCaches);
}
public ConfigurationBuilder removeCache(String alias) {
Map<String, CacheConfiguration<?, ?>> newCaches = new HashMap<>(caches);
newCaches.remove(alias);
return new ConfigurationBuilder(this, newCaches);
}
public ConfigurationBuilder addService(ServiceCreationConfiguration<?> serviceConfiguration) {
if (findServiceByClass(serviceConfiguration.getClass()) != null) {
throw new IllegalArgumentException("There is already a ServiceCreationConfiguration registered for service " + serviceConfiguration
.getServiceType() + " of type " + serviceConfiguration.getClass());
}
List<ServiceCreationConfiguration<?>> newServiceConfigurations = new ArrayList<>(serviceConfigurations);
newServiceConfigurations.add(serviceConfiguration);
return new ConfigurationBuilder(this, newServiceConfigurations);
}
<T> T findServiceByClass(Class<T> type) {
for (ServiceCreationConfiguration<?> serviceConfiguration : serviceConfigurations) {
if (serviceConfiguration.getClass().equals(type)) {
return type.cast(serviceConfiguration);
}
}
return null;
}
public ConfigurationBuilder removeService(ServiceCreationConfiguration<?> serviceConfiguration) {
List<ServiceCreationConfiguration<?>> newServiceConfigurations = new ArrayList<>(serviceConfigurations);
newServiceConfigurations.remove(serviceConfiguration);
return new ConfigurationBuilder(this, newServiceConfigurations);
}
public ConfigurationBuilder withClassLoader(ClassLoader classLoader) {
return new ConfigurationBuilder(this, classLoader);
}
public boolean containsCache(String alias) {
return caches.containsKey(alias);
}
}