package com.xqbase.apool.impl;

import com.xqbase.apool.AsyncPool;
import com.xqbase.apool.callback.Callback;
import com.xqbase.apool.util.Cancellable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Deque;
import java.util.LinkedList;

/**
 * The Async Pool Implementation.
 *
 * @author Tony He
 */
public class AsyncPoolImpl<T> implements AsyncPool<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(AsyncPoolImpl.class);

    private final String poolName;
    private final int maxSize;
    private final int minSize;
    private final int maxWaiters;

    private enum State { NOT_YET_STARTED, RUNNING, SHUTTING_DOWN, STOPPED }

    private int poolSize = 0;
    private final Object lock = new Object();

    private final Deque<T> idle = new LinkedList<>();

    private int totalCreated = 0;
    private int totalDestroyed = 0;
    private int totalTimeout = 0;
    private int checkedOut = 0;

    public AsyncPoolImpl(String poolName, int maxSize, int minSize, int maxWaiters) {
        this.poolName = poolName;
        this.maxSize = maxSize;
        this.minSize = minSize;
        this.maxWaiters = maxWaiters;
    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public void start() {

    }

    @Override
    public Cancellable get(Callback<T> callback) {
        return null;
    }

    @Override
    public void put(T obj) {

    }
}

@@ -145,16 +145,58 @@
 llable;%0A
+import com.xqbase.apool.util.LinkedDeque;%0A
 import o
@@ -297,16 +297,108 @@
 kedList;
+%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.TimeUnit;
 %0A%0A/**%0A *
@@ -730,16 +730,112 @@
 Waiters;
+%0A    private final long idleTimeout;%0A    private final ScheduledExecutorService timeoutExecutor;
 %0A%0A    pr
@@ -1035,16 +1035,140 @@
 ist%3C%3E();
+%0A    private final LinkedDeque%3CCallback%3CT%3E%3E waiters = new LinkedDeque%3C%3E();%0A%0A    private State state = State.NOT_YET_STARTED;
 %0A%0A    pr
@@ -1380,16 +1380,92 @@
 xWaiters
+,%0A                long idleTimeout, ScheduledExecutorService timeoutExecutor
 ) %7B%0A    
@@ -1592,24 +1592,112 @@
 maxWaiters;%0A
+        this.idleTimeout = idleTimeout;%0A        this.timeoutExecutor = timeoutExecutor;%0A
     %7D%0A%0A    @
@@ -1742,36 +1742,40 @@
 %0A        return 
-null
+poolName
 ;%0A    %7D%0A%0A    @Ov
@@ -1811,148 +1811,1210 @@
 ) %7B%0A
-%0A    %7D%0A%0A    @Override%0A    public Cancellable get(Callback%3CT%3E callback) %7B%0A        return null;%0A    %7D%0A%0A    @Override%0A    public void put(T obj
+        synchronized (lock) %7B%0A            if (state != State.NOT_YET_STARTED) %7B%0A                throw new IllegalStateException(poolName + %22 is %22 + state);%0A            %7D%0A            state = State.RUNNING;%0A            if (idleTimeout %3E 0) %7B%0A                long freq = Math.min(idleTimeout, 1000);%0A                timeoutExecutor.scheduleAtFixedRate(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A%0A                    %7D%0A                %7D, freq, freq, TimeUnit.MILLISECONDS);%0A            %7D%0A        %7D%0A%0A        // make the minimum required number of objects now%0A        for (int i = 0; i %3C minSize; i++) %7B%0A            if (shouldCreate()) %7B%0A                create();%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public Cancellable get(Callback%3CT%3E callback) %7B%0A        return null;%0A    %7D%0A%0A    @Override%0A    public void put(T obj) %7B%0A%0A    %7D%0A%0A    /**%0A     * Whether another object creation should be initiated.%0A     *%0A     * @return true if another object create should be initiated.%0A     */%0A    public boolean shouldCreate() %7B%0A        boolean result = true;%0A        return result;%0A    %7D%0A%0A    /**%0A     * The real object creation method.%0A     */%0A    public void create(
 ) %7B%0A

add the start method...