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