![JAR search and dependency download from the Maven repository](/logo.png)
org.ehcache.config.builders.ConfigurationBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache-impl Show documentation
Show all versions of ehcache-impl Show documentation
The implementation module of Ehcache 3
/*
* Copyright Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
/**
* Companion type to the {@link CacheManagerBuilder} that handles the {@link Configuration} building.
*
* @author Alex Snaps
*/
class ConfigurationBuilder implements Builder {
private final Map> caches;
private final List> serviceConfigurations;
private final ClassLoader classLoader;
static ConfigurationBuilder newConfigurationBuilder() {
return new ConfigurationBuilder();
}
private ConfigurationBuilder() {
this.caches = emptyMap();
this.serviceConfigurations = emptyList();
this.classLoader = null;
}
private ConfigurationBuilder(ConfigurationBuilder builder, Map> caches) {
this.caches = unmodifiableMap(caches);
this.serviceConfigurations = builder.serviceConfigurations;
this.classLoader = builder.classLoader;
}
private ConfigurationBuilder(ConfigurationBuilder builder, List> 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()]));
}
ConfigurationBuilder addCache(String alias, CacheConfiguration, ?> config) {
Map> newCaches = new HashMap>(caches);
newCaches.put(alias, config);
return new ConfigurationBuilder(this, newCaches);
}
public ConfigurationBuilder removeCache(String alias) {
Map> newCaches = new HashMap>(caches);
newCaches.remove(alias);
return new ConfigurationBuilder(this, newCaches);
}
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> newServiceConfigurations = new ArrayList>(serviceConfigurations);
newServiceConfigurations.add(serviceConfiguration);
return new ConfigurationBuilder(this, newServiceConfigurations);
}
T findServiceByClass(Class type) {
for (ServiceCreationConfiguration> serviceConfiguration : serviceConfigurations) {
if (serviceConfiguration.getClass().equals(type)) {
return (T) serviceConfiguration;
}
}
return null;
}
ConfigurationBuilder removeService(ServiceCreationConfiguration> serviceConfiguration) {
List> newServiceConfigurations = new ArrayList>(serviceConfigurations);
newServiceConfigurations.remove(serviceConfiguration);
return new ConfigurationBuilder(this, newServiceConfigurations);
}
ConfigurationBuilder withClassLoader(ClassLoader classLoader) {
return new ConfigurationBuilder(this, classLoader);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy