com.clickhouse.client.api.internal.CachingObjectsSupplier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of client-v2 Show documentation
Show all versions of client-v2 Show documentation
New client api for ClickHouse
package com.clickhouse.client.api.internal;
import java.util.Deque;
import java.util.Iterator;
import java.util.function.Supplier;
public abstract class CachingObjectsSupplier implements Supplier {
private Iterator iterator;
private Deque cache;;
/**
* Constructs caching object supplier that uses Deque as cache. Deque may be a thread safe implementation.
* It will be filled with {@code preallocate} number of objects.
*
* @param cache
* @param preallocate
*/
public CachingObjectsSupplier(Deque cache, int preallocate) {
this.cache = cache;
}
@Override
public T get() {
T obj;
if (iterator.hasNext()) {
obj = iterator.next();
} else {
obj = create();
cache.addFirst(obj);
}
return obj;
}
/**
* Resets internal iterator to begin with the first object in the cache.
*/
public void reset() {
iterator = cache.iterator();
}
public abstract T create();
}