All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickhouse.client.api.internal.CachingObjectsSupplier Maven / Gradle / Ivy

The newest version!
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();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy