net.intelie.disq.DisqBuilder Maven / Gradle / Ivy
package net.intelie.disq;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
public class DisqBuilder {
private final Processor processor;
private Serializer serializer = new DefaultSerializer();
private Path directory = null; //default to temp directory
private long maxSize = Long.MAX_VALUE;
private boolean flushOnPop = true;
private boolean flushOnPush = true;
private boolean deleteOldestOnOverflow = true;
private int initialBufferCapacity = 4096;
private int maxBufferCapacity = -1;
private boolean compress = false;
private int fallbackBufferCapacity = 0;
private int threadCount = 1;
private ThreadFactory threadFactory = Executors.defaultThreadFactory();
public DisqBuilder(Processor processor) {
this.processor = processor;
}
public DisqBuilder setSerializer(Serializer serializer) {
this.serializer = serializer;
return this;
}
public DisqBuilder setDirectory(Path directory) {
this.directory = directory;
return this;
}
public DisqBuilder setDirectory(String first, String... rest) {
return setDirectory(Paths.get(first, rest));
}
public DisqBuilder setMaxSize(long maxSize) {
this.maxSize = maxSize;
return this;
}
public DisqBuilder setFlushOnPop(boolean flushOnPop) {
this.flushOnPop = flushOnPop;
return this;
}
public DisqBuilder setFlushOnPush(boolean flushOnPush) {
this.flushOnPush = flushOnPush;
return this;
}
public DisqBuilder setDeleteOldestOnOverflow(boolean deleteOldestOnOverflow) {
this.deleteOldestOnOverflow = deleteOldestOnOverflow;
return this;
}
public DisqBuilder setInitialBufferCapacity(int initialBufferCapacity) {
this.initialBufferCapacity = initialBufferCapacity;
return this;
}
public DisqBuilder setMaxBufferCapacity(int maxBufferCapacity) {
this.maxBufferCapacity = maxBufferCapacity;
return this;
}
public DisqBuilder setCompress(boolean compress) {
this.compress = compress;
return this;
}
public DisqBuilder setFallbackBufferCapacity(int fallbackBufferCapacity) {
this.fallbackBufferCapacity = fallbackBufferCapacity;
return this;
}
public DisqBuilder setThreadCount(int threadCount) {
this.threadCount = threadCount;
return this;
}
public DisqBuilder setNamedThreadFactory(String nameFormat) {
return setThreadFactory(new NamedThreadFactory(nameFormat));
}
public DisqBuilder setThreadFactory(ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
return this;
}
public Disq build() {
return build(false);
}
public Disq build(boolean paused) {
PersistentQueue queue = buildPersistentQueue();
queue.setPopPaused(paused);
return new Disq(threadFactory, threadCount, processor, queue);
}
public PersistentQueue buildPersistentQueue() {
return buildPersistentQueue(buildRawQueue());
}
public PersistentQueue buildPersistentQueue(RawQueue rawQueue) {
return new PersistentQueue<>(
rawQueue, serializer, initialBufferCapacity, maxBufferCapacity, compress, fallbackBufferCapacity);
}
public DiskRawQueue buildRawQueue() {
return new DiskRawQueue(
directory, maxSize, flushOnPop, flushOnPush, deleteOldestOnOverflow);
}
}