software.xdev.tci.serviceloading.TCIServiceLoader Maven / Gradle / Ivy
The newest version!
/*
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* 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 software.xdev.tci.serviceloading;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
/**
* Central point for service loading
*/
@SuppressWarnings("java:S6548")
public final class TCIServiceLoader
{
private static final TCIServiceLoader INSTANCE = new TCIServiceLoader();
public static TCIServiceLoader instance()
{
return INSTANCE;
}
private final Map, Object> loadedServices = Collections.synchronizedMap(new HashMap<>());
private TCIServiceLoader()
{
}
@SuppressWarnings("unchecked")
public T service(final Class clazz)
{
return (T)this.loadedServices.computeIfAbsent(
clazz,
c -> ServiceLoader.load(clazz)
.stream()
// Get by highest priority
.max(Comparator.comparingInt(p ->
Optional.ofNullable(p.type().getAnnotation(TCIProviderPriority.class))
.map(TCIProviderPriority::value)
.orElse(TCIProviderPriority.DEFAULT_PRIORITY)))
.map(ServiceLoader.Provider::get)
.orElse(null));
}
public boolean isLoaded(final Class> clazz)
{
return this.loadedServices.get(clazz) != null;
}
/**
* Force overwrite the loaded configuration manually.
*
* WARNING: Usage not recommended. Should only be used as last resort!
*
*/
public Object forceOverwrite(final Class> clazz, final Object value)
{
return this.loadedServices.put(clazz, value);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy