package com.wizzardo.tools.misc.pool;

import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Created by wizzardo on 18.06.15.
 */
public abstract class SharedPool<T> implements Pool<T> {

    protected final Queue<Holder<T>> queue = new ConcurrentLinkedQueue<Holder<T>>();

    @Override
    public T get() {
        T value;
        do {
            Holder<T> item = queue.poll();
            if (item == null)
                return create();

            value = item.get();
        } while (value == null);

        return value;
    }

    @Override
    public Holder<T> holder() {
        Holder<T> item = queue.poll();
        if (item == null)
            return createHolder(create());

        return item;
    }

    @Override
    public void release(T t) {
        release(createHolder(t));
    }

    protected void release(Holder<T> holder) {
        queue.add(holder);
    }

    protected Holder<T> createHolder(T t) {
        return new SimpleHolder(t);
    }

    protected class SimpleHolder implements Holder<T> {
        final T value;

        public SimpleHolder(T value) {
            this.value = value;
        }

        @Override
        public T get() {
            return value;
        }

        @Override
        public void close() throws IOException {
            release(value);
        }
    }
}

add SharedPool implementation