bt.LazyClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bt-core Show documentation
Show all versions of bt-core Show documentation
BitTorrent Client Library (Core)
package bt;
import bt.runtime.BtClient;
import bt.torrent.TorrentSession;
import bt.torrent.TorrentSessionState;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Client decorator with lazy (on-demand) initialization.
*
* @since 1.0
*/
class LazyClient implements BtClient {
private Supplier clientSupplier;
private volatile BtClient delegate;
/**
* @since 1.0
*/
public LazyClient(Supplier clientSupplier) {
this.clientSupplier = clientSupplier;
}
private synchronized void initClient() {
if (delegate == null) {
delegate = clientSupplier.get();
}
}
@Override
public CompletableFuture> startAsync() {
if (delegate == null) {
initClient();
}
return delegate.startAsync();
}
@Override
public CompletableFuture> startAsync(Consumer listener, long period) {
if (delegate == null) {
initClient();
}
return delegate.startAsync(listener, period);
}
@Override
public void stop() {
if (delegate == null) {
return;
}
delegate.stop();
}
@Override
public TorrentSession getSession() {
if (delegate == null) {
initClient();
}
return delegate.getSession();
}
@Override
public boolean isStarted() {
return delegate != null && delegate.isStarted();
}
}