internal.util.SdmxWebDriverLoader 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.web.spi.FailsafeSdmxWebDriver;
import java.lang.Iterable;
import java.lang.Long;
import java.lang.Override;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import sdmxdl.web.spi.SdmxWebDriver;
/**
* Custom service loader for {@link sdmxdl.web.spi.SdmxWebDriver}.
*
This class is thread-safe.
* Properties:
*
* - Quantifier: MULTIPLE
* - Fallback: null
* - Preprocessing: wrapper: internal.sdmxdl.web.spi.FailsafeSdmxWebDriver filters:[isAvailable] sorters:[getRank]
* - Mutability: NONE
* - Singleton: false
* - Name: internal.util.SdmxWebDriverLoader
* - Backend: null
* - Cleaner: null
*
*/
public final class SdmxWebDriverLoader {
private final Iterable source = ServiceLoader.load(SdmxWebDriver.class);
private final List resource = doLoad();
private Spliterator spliterator() {
return new Spliterators.AbstractSpliterator(Long.MAX_VALUE, 0) {
final Iterator delegate = source.iterator();
@Override
public boolean tryAdvance(Consumer super SdmxWebDriver> action) {
if (delegate.hasNext()) {
action.accept((SdmxWebDriver) delegate.next());
return true;
}
return false;
}
};
}
private List doLoad() {
return StreamSupport.stream(spliterator(), false)
.map(FailsafeSdmxWebDriver::wrap)
.map(SdmxWebDriver.class::cast)
.filter(SdmxWebDriver::isAvailable)
.sorted(Collections.reverseOrder(Comparator.comparingInt(SdmxWebDriver::getRank)))
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}
/**
* Gets a list of {@link sdmxdl.web.spi.SdmxWebDriver} 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.SdmxWebDriver} instances.
*
This is equivalent to the following code: new SdmxWebDriverLoader().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 SdmxWebDriverLoader().get();
}
}