internal.util.http.HttpURLConnectionFactoryLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdmx-dl-provider-ri Show documentation
Show all versions of sdmx-dl-provider-ri Show documentation
Easily download official statistics - RI
package internal.util.http;
import java.lang.Iterable;
import java.lang.Long;
import java.lang.Override;
import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;
/**
* Custom service loader for {@link internal.util.http.HttpURLConnectionFactory}.
*
This class is thread-safe.
* Properties:
*
* - Quantifier: SINGLE
* - Fallback: {@link internal.util.http.DefaultHttpURLConnectionFactory}
* - Preprocessing: wrapper: none filters:[isRequired] sorters:[]
* - Mutability: NONE
* - Singleton: true
* - Name: null
* - Backend: null
* - Cleaner: null
*
*/
public final class HttpURLConnectionFactoryLoader {
private static final Iterable SOURCE = ServiceLoader.load(HttpURLConnectionFactory.class);
private static final HttpURLConnectionFactory RESOURCE = doLoad();
private HttpURLConnectionFactoryLoader() {
}
private static Spliterator spliterator() {
return new Spliterators.AbstractSpliterator(Long.MAX_VALUE, 0) {
final Iterator delegate = SOURCE.iterator();
@Override
public boolean tryAdvance(Consumer super HttpURLConnectionFactory> action) {
if (delegate.hasNext()) {
action.accept((HttpURLConnectionFactory) delegate.next());
return true;
}
return false;
}
};
}
private static HttpURLConnectionFactory doLoad() {
return StreamSupport.stream(spliterator(), false)
.filter(HttpURLConnectionFactory::isRequired)
.findFirst()
.orElseGet(() -> new DefaultHttpURLConnectionFactory());
}
/**
* Gets a {@link internal.util.http.HttpURLConnectionFactory} instance.
*
This method is thread-safe.
* @return the current non-null value
*/
public static HttpURLConnectionFactory get() {
return RESOURCE;
}
}