internal.sdmxdl.web.spi.DriverLoader 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.List;
import java.util.ServiceLoader;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import sdmxdl.web.spi.Driver;
/**
* Custom service loader for {@link sdmxdl.web.spi.Driver}.
*
This class is thread-safe.
* Properties:
*
* - Quantifier: MULTIPLE
* - Fallback: null
* - Preprocessing: wrapper: internal.sdmxdl.web.spi.FailsafeDriver filters:[isDriverAvailable] sorters:[getDriverRank]
* - Mutability: NONE
* - Singleton: false
* - Name: internal.{{canonicalName}}Loader
* - Backend: null
* - Cleaner: null
* - Batch: false
* - Batch name: null
*
*/
public final class DriverLoader {
public static final Pattern ID_PATTERN = Pattern.compile("^[A-Z0-9]+(?:_[A-Z0-9]+)*$");
private final Iterable source = ServiceLoader.load(Driver.class);
private final Predicate filter = ((Predicate)o -> ID_PATTERN.matcher(o.getDriverId()).matches()).and(Driver::isDriverAvailable);
private final Comparator sorter = Collections.reverseOrder(Comparator.comparingInt(Driver::getDriverRank));
private final List resource = doLoad();
private List doLoad() {
return StreamSupport.stream(source.spliterator(), false)
.map(FailsafeDriver::wrap)
.map(Driver.class::cast)
.filter(filter)
.sorted(sorter)
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}
/**
* Gets a list of {@link sdmxdl.web.spi.Driver} instances.
*
This method is thread-safe.
* @return the current non-null value
*/
public List get() {
return resource;
}
/**
* Gets a list of {@link sdmxdl.web.spi.Driver} instances.
*
This is equivalent to the following code: new DriverLoader().get()
*
Therefore, the returned value might be different at each call.
*
This method is thread-safe.
* @return a non-null value
*/
public static List load() {
return new DriverLoader().get();
}
}