org.gradle.internal.service.CachingServiceLocator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2016 the original author or authors.
*
* 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.gradle.internal.service;
import org.gradle.internal.Cast;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CachingServiceLocator implements ServiceLocator {
private final ServiceLocator delegate;
private final Map, DefaultServiceLocator.ServiceFactory>> serviceFactories = new HashMap, DefaultServiceLocator.ServiceFactory>>();
private final Map, Object> services = new HashMap, Object>();
private final Map, List>> allServices = new HashMap, List>>();
public static CachingServiceLocator of(ServiceLocator other) {
return new CachingServiceLocator(other);
}
private CachingServiceLocator(ServiceLocator delegate) {
this.delegate = delegate;
}
@Override
public synchronized DefaultServiceLocator.ServiceFactory findFactory(Class serviceType) {
if (serviceFactories.containsKey(serviceType)) {
return Cast.uncheckedCast(serviceFactories.get(serviceType));
}
DefaultServiceLocator.ServiceFactory factory = delegate.findFactory(serviceType);
serviceFactories.put(serviceType, factory);
return factory;
}
@Override
public synchronized T get(Class serviceType) throws UnknownServiceException {
if (services.containsKey(serviceType)) {
return Cast.uncheckedCast(services.get(serviceType));
}
T t = delegate.get(serviceType);
services.put(serviceType, t);
return t;
}
@Override
public synchronized List getAll(Class serviceType) throws UnknownServiceException {
if (allServices.containsKey(serviceType)) {
return Cast.uncheckedCast(allServices.get(serviceType));
}
List all = delegate.getAll(serviceType);
allServices.put(serviceType, all);
return all;
}
@Override
public synchronized DefaultServiceLocator.ServiceFactory getFactory(Class serviceType) throws UnknownServiceException {
DefaultServiceLocator.ServiceFactory factory = findFactory(serviceType);
if (factory == null) {
throw new UnknownServiceException(serviceType, String.format("Could not find meta-data resource 'META-INF/services/%s' for service '%s'.", serviceType.getName(), serviceType.getName()));
}
return factory;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy