internal.sdmxdl.file.spi.ReaderLoader 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.file.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.file.spi.Reader;
/**
* Custom service loader for {@link sdmxdl.file.spi.Reader}.
*
This class is thread-safe.
* Properties:
*
* - Quantifier: MULTIPLE
* - Fallback: null
* - Preprocessing: wrapper: none filters:[isReaderAvailable] sorters:[getReaderRank]
* - Mutability: NONE
* - Singleton: false
* - Name: internal.{{canonicalName}}Loader
* - Backend: null
* - Cleaner: null
* - Batch: false
* - Batch name: null
*
*/
public final class ReaderLoader {
public static final Pattern ID_PATTERN = Pattern.compile("^[A-Z0-9]+(?:_[A-Z0-9]+)*$");
private final Iterable source = ServiceLoader.load(Reader.class);
private final Predicate filter = ((Predicate)o -> ID_PATTERN.matcher(o.getReaderId()).matches()).and(Reader::isReaderAvailable);
private final Comparator sorter = Collections.reverseOrder(Comparator.comparingInt(Reader::getReaderRank));
private final List resource = doLoad();
private List doLoad() {
return StreamSupport.stream(source.spliterator(), false)
.filter(filter)
.sorted(sorter)
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}
/**
* Gets a list of {@link sdmxdl.file.spi.Reader} instances.
*
This method is thread-safe.
* @return the current non-null value
*/
public List get() {
return resource;
}
/**
* Gets a list of {@link sdmxdl.file.spi.Reader} instances.
*
This is equivalent to the following code: new ReaderLoader().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 ReaderLoader().get();
}
}