internal.sdmxdl.web.spi.RegistryLoader 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
The newest version!
package internal.sdmxdl.web.spi;
import java.lang.Iterable;
import java.util.Collections;
import java.util.Comparator;
import java.util.ServiceLoader;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.StreamSupport;
import sdmxdl.web.spi.Registry;
/**
* Custom service loader for {@link sdmxdl.web.spi.Registry}.
*
This class is thread-safe.
* Properties:
*
* - Quantifier: SINGLE
* - Fallback: {@link internal.sdmxdl.web.spi.NoOpRegistry}
* - Preprocessing: wrapper: none filters:[] sorters:[getRegistryRank]
* - Mutability: NONE
* - Singleton: false
* - Name: internal.{{canonicalName}}Loader
* - Backend: null
* - Cleaner: null
* - Batch: false
* - Batch name: null
*
*/
public final class RegistryLoader {
public static final Pattern ID_PATTERN = Pattern.compile("^[A-Z0-9]+(?:_[A-Z0-9]+)*$");
private final Iterable source = ServiceLoader.load(Registry.class);
private final Predicate filter = o -> ID_PATTERN.matcher(o.getRegistryId()).matches();
private final Comparator sorter = Collections.reverseOrder(Comparator.comparingInt(Registry::getRegistryRank));
private final Registry resource = doLoad();
private Registry doLoad() {
return StreamSupport.stream(source.spliterator(), false)
.filter(filter)
.sorted(sorter)
.findFirst()
.orElseGet(() -> NoOpRegistry.INSTANCE);
}
/**
* Gets a {@link sdmxdl.web.spi.Registry} instance.
*
This method is thread-safe.
* @return the current non-null value
*/
public Registry get() {
return resource;
}
/**
* Gets a {@link sdmxdl.web.spi.Registry} instance.
*
This is equivalent to the following code: new RegistryLoader().get()
*
Therefore, the returned value might be different at each call.
*
This method is thread-safe.
* @return a non-null value
*/
public static Registry load() {
return new RegistryLoader().get();
}
}