internal.util.WebCachingLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdmx-dl-api Show documentation
Show all versions of sdmx-dl-api Show documentation
Easily download official statistics - API
package internal.util;
import internal.sdmxdl.NoOpCaching;
import java.lang.Iterable;
import java.util.Collections;
import java.util.Comparator;
import java.util.ServiceLoader;
import java.util.stream.StreamSupport;
import sdmxdl.web.spi.WebCaching;
/**
* Custom service loader for {@link sdmxdl.web.spi.WebCaching}.
*
This class is thread-safe.
* Properties:
*
* - Quantifier: SINGLE
* - Fallback: {@link internal.sdmxdl.NoOpCaching}
* - Preprocessing: wrapper: none filters:[] sorters:[getWebCachingRank]
* - Mutability: NONE
* - Singleton: false
* - Name: internal.util.WebCachingLoader
* - Backend: null
* - Cleaner: null
* - Batch: false
* - Batch name: null
*
*/
public final class WebCachingLoader {
private final Iterable source = ServiceLoader.load(WebCaching.class);
private final WebCaching resource = doLoad();
private WebCaching doLoad() {
return StreamSupport.stream(source.spliterator(), false)
.sorted(Collections.reverseOrder(Comparator.comparingInt(WebCaching::getWebCachingRank)))
.findFirst()
.orElseGet(() -> NoOpCaching.INSTANCE);
}
/**
* Gets a {@link sdmxdl.web.spi.WebCaching} instance.
*
This method is thread-safe.
* @return the current non-null value
*/
public WebCaching get() {
return resource;
}
/**
* Gets a {@link sdmxdl.web.spi.WebCaching} instance.
*
This is equivalent to the following code: new WebCachingLoader().get()
*
Therefore, the returned value might be different at each call.
*
This method is thread-safe.
* @return a non-null value
*/
public static WebCaching load() {
return new WebCachingLoader().get();
}
}