/*
 * $Source: /home/cvs/commons/pool1.1/ja/src/org/apache/commons/pool/impl/GenericKeyedObjectPool.java,v $
 * $Revision: 1.1 $
 * $Date: 2004/02/22 11:58:26 $
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowledgement:
 *       "This product includes software developed by the
 *        Apache Software Foundation - http://www.apache.org/"
 *    Alternately, this acknowledgement may appear in the software itself,
 *    if and wherever such third-party acknowledgements normally appear.
 *
 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * http://www.apache.org/
 *
 */

package org.apache.commons.pool.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.apache.commons.collections.CursorableLinkedList;
import org.apache.commons.pool.BaseKeyedObjectPool;
import org.apache.commons.pool.KeyedObjectPool;
import org.apache.commons.pool.KeyedPoolableObjectFactory;

/**
 * 詳細な設定の可能な {@link KeyedObjectPool} の実装です。
 * {@primary A configurable {@link KeyedObjectPool} implementation.}
 * <p>
 * 適切な {@link KeyedPoolableObjectFactory} と対となって動作することにより
 * <tt>GenericKeyedObjectPool</tt> は強固な任意のオブジェクトのプーリングの機能を提供します。
 * {@primary When coupled with the appropriate {@link KeyedPoolableObjectFactory},
 * <tt>GenericKeyedObjectPool</tt> provides robust pooling functionality for
 * arbitrary objects.}
 * <p>
 * <tt>GenericKeyedObjectPool</tt> は多くの設定可能なパラメータを提供します。
 * {@primary A <tt>GenericKeyedObjectPool</tt> provides a number of configurable parameters:}
 * <ul>
 *  <li>
 *    {@link #setMaxActive <i>maxActive</i>} は同時にプールから取り出すことのできるオブジェクトの(キー毎の)最大数を制御します。
 *    マイナスの値が設定された場合には同時にプールから取り出すことのできるオブジェクトの制限を行いません。
 *    {@primary {@link #setMaxActive <i>maxActive</i>} controls the maximum number of objects (per key)
 *    that can be borrowed from the pool at one time.  When non-positive, there
 *    is no limit to the number of objects that may be active at one time.
 *    When {@link #setMaxActive <i>maxActive</i>} is exceeded, the pool is said to be exhausted.}
 *  </li>
 *  <li>
 *    {@link #setMaxIdle <i>maxIdle</i>} はプール内に保持できる未使用のオブジェクトの(キー毎の)最大数を制御します。
 *    マイナスの値が設定された場合には同時にプール内に保持できるオブジェクトの制限を行いません。
 *    {@primary {@link #setMaxIdle <i>maxIdle</i>} controls the maximum number of objects that can
 *    sit idle in the pool (per key) at any time.  When negativee, there
 *    is no limit to the number of objects that may be idle at one time.}
 *  </li>
 *  <li>
 *    {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} 
 *    はプールが使い尽されている場合の {@link #borrowObject} メソッドの振る舞いを指定します:
 *    {@primary {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} specifies the
 *    behaviour of the {@link #borrowObject} method when the pool is exhausted:}
 *    <ul>
 *    <li>
 *      {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} が
 *      {@link #WHEN_EXHAUSTED_FAIL}の場合、
 *      {@link #borrowObject} は {@link NoSuchElementException} を投げます。
 *      {@primary When {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} is
 *      {@link #WHEN_EXHAUSTED_FAIL}, {@link #borrowObject} will throw
 *      a {@link NoSuchElementException}}
 *    </li>
 *    <li>
 *      {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} が
 *      {@link #WHEN_EXHAUSTED_GROW} の場合、
 *      {@link #borrowObject} は新たなオブジェクトを生成し、返します
 *      (実質、 {@link #setMaxActive <i>maxActive</i>} は意味をなしません)。
 *      {@primary When {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} is
 *      {@link #WHEN_EXHAUSTED_GROW}, {@link #borrowObject} will create a new
 *      object and return it(essentially making {@link #setMaxActive <i>maxActive</i>}
 *      meaningless.)}
 *    </li>
 *    <li>
 *      {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>} が
 *      {@link #WHEN_EXHAUSTED_BLOCK} の場合、
 *      {@link #borrowObject} は新たなもしくは未使用のオブジェクトが利用できるまで({@link Object#wait} を呼んで)待機します。
 *      有効な {@link #setMaxWait <i>maxWait</i>} が設定されている場合、
 *      {@link #borrowObject} は設定されたミリセカンド待機した後に {@link NoSuchElementException} を投げます。
 *      {@link #setMaxWait <i>maxWait</i>} がマイナスの値だった場合
 *      {@link #borrowObject} は無期限に待機します。
 *      {@primary When {@link #setWhenExhaustedAction <i>whenExhaustedAction</i>}
 *      is {@link #WHEN_EXHAUSTED_BLOCK}, {@link #borrowObject} will block
 *      (invoke {@link Object#wait} until a new or idle object is available.
 *      If a positive {@link #setMaxWait <i>maxWait</i>}
 *      value is supplied, the {@link #borrowObject} will block for at
 *      most that many milliseconds, after which a {@link NoSuchElementException}
 *      will be thrown.  If {@link #setMaxWait <i>maxWait</i>} is non-positive,
 *      the {@link #borrowObject} method will block indefinitely.}
 *    </li>
 *    </ul>
 *  </li>
 *  <li>
 *    {@link #setTestOnBorrow <i>testOnBorrow</i>} が設定されている場合、プールは
 *    {@link #borrowObject} メソッドにて取り出される前に
 *    (提供されたファクトリの {@link org.apache.commons.pool.PoolableObjectFactory#validateObject} メソッドを使用して)
 *    オブジェクトが有効かどうかの確認を試みます。
 *    有効でないと判断されたオブジェクトはプールから破棄され、他のオブジェクトが取り出されます。
 *    {@primary When {@link #setTestOnBorrow <i>testOnBorrow</i>} is set, the pool will
 *    attempt to validate each object before it is returned from the
 *    {@link #borrowObject} method. (Using the provided factory's
 *    {@link org.apache.commons.pool.PoolableObjectFactory#validateObject} method.)  Objects that fail
 *    to validate will be dropped from the pool, and a different object will
 *    be borrowed.}
 *  </li>
 *  <li>
 *    When {@link #setTestOnReturn <i>testOnReturn</i>} が設定されている場合、プールは
 *    {@link #returnObject} メソッドにて戻される前に
 *    (提供されたファクトリの {@link org.apache.commons.pool.PoolableObjectFactory#validateObject} メソッドを使用して)
 *    オブジェクトが有効かどうかの確認を試みます。
 *    有効でないと判断されたオブジェクトはプールから破棄されます。
 *    {@primary When {@link #setTestOnReturn <i>testOnReturn</i>} is set, the pool will
 *    attempt to validate each object before it is returned to the pool in the
 *    {@link #returnObject} method. (Using the provided factory's
 *    {@link org.apache.commons.pool.PoolableObjectFactory#validateObject}
 *    method.)  Objects that fail to validate will be dropped from the pool.}
 *  </li>
 * </ul>
 * <p>
 * オプションとして、プール内を走査して不正な状態でプール内に居続けるオブジェクトを排除する設定をすることができます。
 * これは非同期な "未使用オブジェクト排除" スレッドで行われます。
 * "未使用オブジェクト排除" スレッドの挙動は以下の属性で設定されます:
 * {@primary Optionally, one may configure the pool to examine and possibly evict objects as they
 * sit idle in the pool.  This is performed by an "idle object eviction" thread, which
 * runs asychronously.  The idle object eviction thread may be configured using the
 * following attributes:}
 * <ul>
 *  <li>
 *   {@link #setTimeBetweenEvictionRunsMillis <i>timeBetweenEvictionRunsMillis</i>} 
 *   は排除スレッドがオブジェクト排除処理を実行する前にどのくらいの間スリープするかを示します。
 *   マイナスの値が設定された場合、排除スレッドは起動しません。
 *   {@primary {@link #setTimeBetweenEvictionRunsMillis <i>timeBetweenEvictionRunsMillis</i>}
 *   indicates how long the eviction thread should sleep before "runs" of examining
 *   idle objects.  When non-positive, no eviction thread will be launched.}
 *  </li>
 *  <li>
 *   {@link #setMinEvictableIdleTimeMillis <i>minEvictableIdleTimeMillis</i>}
 *   はオブジェクトがプール内に未使用状態でいられる時間の最小値を指定します。
 *   未使用状態でいる時間がこの値に達すると排除処理の対象となります。
 *   マイナスの値が設定された場合、未使用状態でいる時間が原因ではオブジェクトの削除は行われません。
 *   {@primary {@link #setMinEvictableIdleTimeMillis <i>minEvictableIdleTimeMillis</i>}
 *   specifies the minimum amount of time that an object may sit idle in the pool
 *   before it is eligable for eviction due to idle time.  When non-positive, no object
 *   will be dropped from the pool due to idle time alone.}
 *  </li>
 *  <li>
 *   {@link #setTestWhileIdle <i>testWhileIdle</i>} 
 *   は未使用状態のオブジェクトに対してファクトリの
 *   {@link org.apache.commons.pool.PoolableObjectFactory#validateObject} メソッドを使ってチェックを行うかどうかを示します。
 *   有効でないと判断されたオブジェクトはプールから破棄されます。
 *   {@primary {@link #setTestWhileIdle <i>testWhileIdle</i>} indicates whether or not idle
 *   objects should be validated using the factory's
 *   {@link org.apache.commons.pool.PoolableObjectFactory#validateObject} method.  Objects
 *   that fail to validate will be dropped from the pool.}
 *  </li>
 * </ul>
 * <p>
 * {@link KeyedPoolableObjectFactory} を使用しない場合、GenericKeyedObjectPool は有用ではありません。
 * コンストラクタの引数または、{@link #setFactory} メソッドをコールすることによって
 * <code>null</code> ではないファクトリを提供する必要があります。
 * {@primary GenericKeyedObjectPool is not usable without a {@link KeyedPoolableObjectFactory}.  A
 * non-<code>null</code> factory must be provided either as a constructor argument
 * or via a call to {@link #setFactory} before the pool is used.}
 * </p>
 * @see GenericObjectPool
 * @author Rodney Waldhoff
 * @author Dirk Verbeeck
 * @version $Revision: 1.1 $ $Date: 2004/02/22 11:58:26 $
 *
 * @translator 日置 聡
 * @status firstdraft
 * @update 2004/02/21
 */
public class GenericKeyedObjectPool extends BaseKeyedObjectPool implements KeyedObjectPool {

    //--- public constants -------------------------------------------

    /**
     * "使い尽された時の処理"のタイプ、
     * プールが尽されている場合 (同時にプールから取り出すことのできるオブジェクトの最大数に達した場合)
     * の処理を示し、{@link #borrowObject} メソッドは失敗し、{@link NoSuchElementException} を投げます。
     * {@primary A "when exhausted action" type indicating that when the pool is
     * exhausted (i.e., the maximum number of active objects has
     * been reached), the {@link #borrowObject}
     * method should fail, throwing a {@link NoSuchElementException}.}
     * @see #WHEN_EXHAUSTED_BLOCK
     * @see #WHEN_EXHAUSTED_GROW
     * @see #setWhenExhaustedAction
     */
    public static final byte WHEN_EXHAUSTED_FAIL   = 0;

    /**
     * "使い尽された時の処理"のタイプ、
     * プールが尽されている場合 (同時にプールから取り出すことのできるオブジェクトの最大数に達した場合)
     * の処理を示し、{@link #borrowObject} メソッドは新たなオブジェクトが利用できるまでまたは
     * {@link #getMaxWait 最大待機時間} に達するまで待機します。
     * {@primary A "when exhausted action" type indicating that when the pool
     * is exhausted (i.e., the maximum number
     * of active objects has been reached), the {@link #borrowObject}
     * method should block until a new object is available, or the
     * {@link #getMaxWait maximum wait time} has been reached.}
     * @see #WHEN_EXHAUSTED_FAIL
     * @see #WHEN_EXHAUSTED_GROW
     * @see #setMaxWait
     * @see #getMaxWait
     * @see #setWhenExhaustedAction
     */
    public static final byte WHEN_EXHAUSTED_BLOCK  = 1;

    /**
     * "使い尽された時の処理"のタイプ、
     * プールが尽されている場合 (同時にプールから取り出すことのできるオブジェクトの最大数に達した場合)
     * の処理を示し、{@link #borrowObject} メソッドは単純にとにかく新たなオブジェクトを生成します。
     * {@primary A "when exhausted action" type indicating that when the pool is
     * exhausted (i.e., the maximum number
     * of active objects has been reached), the {@link #borrowObject}
     * method should simply create a new object anyway.}
     * @see #WHEN_EXHAUSTED_FAIL
     * @see #WHEN_EXHAUSTED_GROW
     * @see #setWhenExhaustedAction
     */
    public static final byte WHEN_EXHAUSTED_GROW   = 2;

    /**
     * プール内に保持できる未使用のオブジェクトの(キー毎の)最大数のデフォルト値です。
     * {@primary The default cap on the number of idle instances in the pool
     * (per key).}
     * @see #getMaxIdle
     * @see #setMaxIdle
     */
    public static final int DEFAULT_MAX_IDLE  = 8;

    /**
     * 同時にプールから取り出すことのできるオブジェクトの(キー毎の)最大数のデフォルト値です。
     * {@primary The default cap on the total number of active instances from the pool
     * (per key).}
     * @see #getMaxActive
     * @see #setMaxActive
     */
    public static final int DEFAULT_MAX_ACTIVE  = 8;

    /**
     * 同時にプール内に存在することのできるオブジェクト総数のデフォルト値です。
     * {@primary The default cap on the the maximum number of objects that can exists at one time.}
     * @see #getMaxTotal
     * @see #setMaxTotal
     */
    public static final int DEFAULT_MAX_TOTAL  = -1;

    /**
     * "使い尽された時の処理"のタイプのデフォルト値です。
     * {@primary The default "when exhausted action" for the pool.}
     * @see #WHEN_EXHAUSTED_BLOCK
     * @see #WHEN_EXHAUSTED_FAIL
     * @see #WHEN_EXHAUSTED_GROW
     * @see #setWhenExhaustedAction
     */
    public static final byte DEFAULT_WHEN_EXHAUSTED_ACTION = WHEN_EXHAUSTED_BLOCK;

    /**
     * プールが使い尽されていて {@link #getWhenExhaustedAction "使い尽された時の処理"} に
     * {@link #WHEN_EXHAUSTED_BLOCK} が設定されている場合の
     * {@link #borrowObject} メソッドが例外を投げるまでの最長待機時間(ミリセカンド)のデフォルト値です。
     * {@primary The default maximum amount of time (in millis) the
     * {@link #borrowObject} method should block before throwing
     * an exception when the pool is exhausted and the
     * {@link #getWhenExhaustedAction "when exhausted" action} is
     * {@link #WHEN_EXHAUSTED_BLOCK}.}
     * @see #getMaxWait
     * @see #setMaxWait
     */
    public static final long DEFAULT_MAX_WAIT = -1L;

    /**
     * オブジェクトの状態を "取り出し時に確認" するかどうかのデフォルトの値です。
     * {@primary The default "test on borrow" value.}
     * @see #getTestOnBorrow
     * @see #setTestOnBorrow
     */
    public static final boolean DEFAULT_TEST_ON_BORROW = false;

    /**
     * オブジェクトの状態を "戻される時に確認" するかどうかのデフォルトの値です。
     * {@primary The default "test on return" value.}
     * @see #getTestOnReturn
     * @see #setTestOnReturn
     */
    public static final boolean DEFAULT_TEST_ON_RETURN = false;

    /**
     * オブジェクトの状態を "未使用の間に確認" するかどうかのデフォルトの値です。
     * {@primary The default "test while idle" value.}
     * @see #getTestWhileIdle
     * @see #setTestWhileIdle
     * @see #getTimeBetweenEvictionRunsMillis
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public static final boolean DEFAULT_TEST_WHILE_IDLE = false;

    /**
     * "オブジェクト排除処理の実行間隔" のデフォルトの値です。
     * {@primary The default "time between eviction runs" value.}
     * @see #getTimeBetweenEvictionRunsMillis
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1L;

    /**
     * 1度のオブジェクト排除処理でチェックされるオブジェクトの数のデフォルト値です。
     * {@primary The default number of objects to examine per run in the
     * idle object evictor.}
     * @see #getNumTestsPerEvictionRun
     * @see #setNumTestsPerEvictionRun
     * @see #getTimeBetweenEvictionRunsMillis
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 3;

    /**
     * {@link #getMinEvictableIdleTimeMillis} のデフォルト値です。
     * {@primary The default value for {@link #getMinEvictableIdleTimeMillis}.}
     * @see #getMinEvictableIdleTimeMillis
     * @see #setMinEvictableIdleTimeMillis
     */
    public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000L * 60L * 30L;

    //--- constructors -----------------------------------------------

    /**
     * 新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt>..}
     */
    public GenericKeyedObjectPool() {
        this(null,DEFAULT_MAX_ACTIVE,DEFAULT_WHEN_EXHAUSTED_ACTION,DEFAULT_MAX_WAIT,DEFAULT_MAX_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory) {
        this(factory,DEFAULT_MAX_ACTIVE,DEFAULT_WHEN_EXHAUSTED_ACTION,DEFAULT_MAX_WAIT,DEFAULT_MAX_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     * @param config 設定情報を定義する {@link GenericKeyedObjectPool.Config} ( <tt>null</tt> を許容しません)
     * {@primary a non-<tt>null</tt> {@link GenericKeyedObjectPool.Config} describing my configuration}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory, GenericKeyedObjectPool.Config config) {
        this(factory,config.maxActive,config.whenExhaustedAction,config.maxWait,config.maxIdle,config.testOnBorrow,config.testOnReturn,config.timeBetweenEvictionRunsMillis,config.numTestsPerEvictionRun,config.minEvictableIdleTimeMillis,config.testWhileIdle);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     * @param maxActive 同時にプールから取り出すことのできるオブジェクトの(キー毎の)最大数({@link #setMaxActive} を参照)
     * {@primary the maximum number of objects that can be borrowed from me at one time (per key) (see {@link #setMaxActive})}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory, int maxActive) {
        this(factory,maxActive,DEFAULT_WHEN_EXHAUSTED_ACTION,DEFAULT_MAX_WAIT,DEFAULT_MAX_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     * @param maxActive 同時にプールから取り出すことのできるオブジェクトの(キー毎の)最大数({@link #setMaxActive} を参照)
     * {@primary the maximum number of objects that can be borrowed from me at one time (per key) (see {@link #setMaxActive})}
     * @param whenExhaustedAction プールが使い尽されている場合の処理 ({@link #getWhenExhaustedAction} を参照)
     * {@primary the action to take when the pool is exhausted (see {@link #getWhenExhaustedAction})}
     * @param maxWait プールが使い尽されていて <i>whenExhaustedAction</i> が {@link #WHEN_EXHAUSTED_BLOCK} の場合(それ以外の場合、無視されます)の最長待機時間 ({@link #getMaxWait} を参照)
     * {@primary the maximum amount of time to wait for an idle object when the pool is exhausted an and <i>whenExhaustedAction</i> is {@link #WHEN_EXHAUSTED_BLOCK} (otherwise ignored) (see {@link #getMaxWait})}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait) {
        this(factory,maxActive,whenExhaustedAction,maxWait,DEFAULT_MAX_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     * @param maxActive 同時にプールから取り出すことのできるオブジェクトの(キー毎の)最大数({@link #setMaxActive} を参照)
     * {@primary the maximum number of objects that can be borrowed from me at one time (per key) (see {@link #setMaxActive})}
     * @param whenExhaustedAction プールが使い尽されている場合の処理 ({@link #getWhenExhaustedAction} を参照)
     * {@primary the action to take when the pool is exhausted (see {@link #getWhenExhaustedAction})}
     * @param maxWait プールが使い尽されていて <i>whenExhaustedAction</i> が {@link #WHEN_EXHAUSTED_BLOCK} の場合(それ以外の場合、無視されます)の最長待機時間 ({@link #getMaxWait} を参照)
     * {@primary the maximum amount of time to wait for an idle object when the pool is exhausted an and <i>whenExhaustedAction</i> is {@link #WHEN_EXHAUSTED_BLOCK} (otherwise ignored) (see {@link #getMaxWait})}
     * @param testOnBorrow オブジェクトの状態を {@link #borrowObject} メソッドを使って取り出す前に確認するかどうか ({@link #getTestOnBorrow} を参照)
     * {@primary whether or not to validate objects before they are returned by the {@link #borrowObject} method (see {@link #getTestOnBorrow})}
     * @param testOnReturn オブジェクトの状態を {@link #returnObject} メソッドを使って戻した後に確認するかどうか ({@link #getTestOnReturn} を参照)
     * {@primary whether or not to validate objects after they are returned to the {@link #returnObject} method (see {@link #getTestOnReturn})}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) {
        this(factory,maxActive,whenExhaustedAction,maxWait,DEFAULT_MAX_IDLE,testOnBorrow,testOnReturn,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     * @param maxActive 同時にプールから取り出すことのできるオブジェクトの(キー毎の)最大数({@link #setMaxActive} を参照)
     * {@primary the maximum number of objects that can be borrowed from me at one time (per key) (see {@link #setMaxActive})}
     * @param whenExhaustedAction プールが使い尽されている場合の処理 ({@link #getWhenExhaustedAction} を参照)
     * {@primary the action to take when the pool is exhausted (see {@link #getWhenExhaustedAction})}
     * @param maxWait プールが使い尽されていて <i>whenExhaustedAction</i> が {@link #WHEN_EXHAUSTED_BLOCK} の場合(それ以外の場合、無視されます)の最長待機時間 ({@link #getMaxWait} を参照)
     * {@primary the maximum amount of time to wait for an idle object when the pool is exhausted an and <i>whenExhaustedAction</i> is {@link #WHEN_EXHAUSTED_BLOCK} (otherwise ignored) (see {@link #getMaxWait})}
     * @param maxIdle プール内に保持できる未使用のオブジェクトの(キー毎の)最大数 ({@link #getMaxIdle} を参照)
     * {@primary the maximum number of idle objects in my pool (per key) (see {@link #setMaxIdle})}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) {
        this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     * @param maxActive 同時にプールから取り出すことのできるオブジェクトの(キー毎の)最大数({@link #setMaxActive} を参照)
     * {@primary the maximum number of objects that can be borrowed from me at one time (per key) (see {@link #setMaxActive})}
     * @param whenExhaustedAction プールが使い尽されている場合の処理 ({@link #getWhenExhaustedAction} を参照)
     * {@primary the action to take when the pool is exhausted (see {@link #getWhenExhaustedAction})}
     * @param maxWait プールが使い尽されていて <i>whenExhaustedAction</i> が {@link #WHEN_EXHAUSTED_BLOCK} の場合(それ以外の場合、無視されます)の最長待機時間 ({@link #getMaxWait} を参照)
     * {@primary the maximum amount of time to wait for an idle object when the pool is exhausted an and <i>whenExhaustedAction</i> is {@link #WHEN_EXHAUSTED_BLOCK} (otherwise ignored) (see {@link #getMaxWait})}
     * @param maxIdle プール内に保持できる未使用のオブジェクトの(キー毎の)最大数 ({@link #getMaxIdle} を参照)
     * {@primary the maximum number of idle objects in my pool (see {@link #getMaxIdle})}
     * @param testOnBorrow オブジェクトの状態を {@link #borrowObject} メソッドを使って取り出す前に確認するかどうか ({@link #getTestOnBorrow} を参照)
     * {@primary whether or not to validate objects before they are returned by the {@link #borrowObject} method (see {@link #getTestOnBorrow})}
     * @param testOnReturn オブジェクトの状態を {@link #returnObject} メソッドを使って戻した後に確認するかどうか ({@link #getTestOnReturn} を参照)
     * {@primary whether or not to validate objects after they are returned to the {@link #returnObject} method (see {@link #getTestOnReturn})}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) {
        this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,testOnBorrow,testOnReturn,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     * @param maxActive the maximum number of objects that can be borrowed from me at one time (per key) (see {@link #setMaxActive})
     * @param whenExhaustedAction プールが使い尽されている場合の処理 ({@link #getWhenExhaustedAction} を参照)
     * {@primary the action to take when the pool is exhausted (see {@link #getWhenExhaustedAction})}
     * @param maxWait プールが使い尽されていて <i>whenExhaustedAction</i> が {@link #WHEN_EXHAUSTED_BLOCK} の場合(それ以外の場合、無視されます)の最長待機時間 ({@link #getMaxWait} を参照)
     * {@primary the maximum amount of time to wait for an idle object when the pool is exhausted an and <i>whenExhaustedAction</i> is {@link #WHEN_EXHAUSTED_BLOCK} (otherwise ignored) (see {@link #getMaxWait})}
     * @param maxIdle プール内に保持できる未使用のオブジェクトの(キー毎の)最大数 ({@link #getMaxIdle} を参照)
     * {@primary the maximum number of idle objects in my pool (see {@link #getMaxIdle})}
     * @param testOnBorrow オブジェクトの状態を {@link #borrowObject} メソッドを使って取り出す前に確認するかどうか ({@link #getTestOnBorrow} を参照)
     * {@primary whether or not to validate objects before they are returned by the {@link #borrowObject} method (see {@link #getTestOnBorrow})}
     * @param testOnReturn オブジェクトの状態を {@link #returnObject} メソッドを使って戻した後に確認するかどうか ({@link #getTestOnReturn} を参照)
     * {@primary whether or not to validate objects after they are returned to the {@link #returnObject} method (see {@link #getTestOnReturn})}
     * @param timeBetweenEvictionRunsMillis 未使用オブジェクト排除処理が次の実行までの間スリープする時間(ミリセカンド) ({@link #setTimeBetweenEvictionRunsMillis} を参照)
     * {@primary the amount of time (in milliseconds) to sleep between examining idle objects for eviction (see {@link #setTimeBetweenEvictionRunsMillis})}
     * @param numTestsPerEvictionRun 1度のオブジェクト排除処理でチェックされるオブジェクトの数 ({@link #setNumTestsPerEvictionRun} を参照)
     * {@primary the number of idle objects to examine per run within the idle object eviction thread (if any) (see {@link #setNumTestsPerEvictionRun})}
     * @param minEvictableIdleTimeMillis オブジェクトがプール内に未使用状態でいられる時間の最小値(ミリセカンド) ({@link #setMinEvictableIdleTimeMillis} を参照)
     * {@primary the minimum number of milliseconds an object can sit idle in the pool before it is eligable for evcition (see {@link #setMinEvictableIdleTimeMillis})}
     * @param testWhileIdle 未使用状態のオブジェクトを未使用オブジェクト排除スレッドでチェックするかどうか ({@link #setTestWhileIdle} を参照)
     * {@primary whether or not to validate objects in the idle object eviction thread, if any (see {@link #setTestWhileIdle})}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) {
        this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle);
    }

    /**
     * 指定された値を使用して新たな <tt>GenericKeyedObjectPool</tt> を生成します。
     * {@primary Create a new <tt>GenericKeyedObjectPool</tt> using the specified values.}
     * @param factory オブジェクトの生成、確認、破棄を行う際に使用される KeyedPoolableObjectFactory( <tt>null</tt> を許容します)
     * {@primary the (possibly <tt>null</tt>)KeyedPoolableObjectFactory to use to create, validate and destroy objects}
     * @param maxActive the maximum number of objects that can be borrowed from me at one time (per key) (see {@link #setMaxActive})
     * @param whenExhaustedAction プールが使い尽されている場合の処理 ({@link #getWhenExhaustedAction} を参照)
     * {@primary the action to take when the pool is exhausted (see {@link #getWhenExhaustedAction})}
     * @param maxWait プールが使い尽されていて <i>whenExhaustedAction</i> が {@link #WHEN_EXHAUSTED_BLOCK} の場合(それ以外の場合、無視されます)の最長待機時間 ({@link #getMaxWait} を参照)
     * {@primary the maximum amount of time to wait for an idle object when the pool is exhausted an and <i>whenExhaustedAction</i> is {@link #WHEN_EXHAUSTED_BLOCK} (otherwise ignored) (see {@link #getMaxWait})}
     * @param maxIdle プール内に保持できる未使用のオブジェクトの(キー毎の)最大数 ({@link #getMaxIdle} を参照)
     * {@primary the maximum number of idle objects in my pool (see {@link #getMaxIdle})}
     * @param maxTotal 同時に存在することのできるオブジェクトの最大数 ({@link #setMaxTotal} を参照)
     * {@primary the maximum number of objects that can exists at one time (see {@link #setMaxTotal})}
     * @param testOnBorrow オブジェクトの状態を {@link #borrowObject} メソッドを使って取り出す前に確認するかどうか ({@link #getTestOnBorrow} を参照)
     * {@primary whether or not to validate objects before they are returned by the {@link #borrowObject} method (see {@link #getTestOnBorrow})}
     * @param testOnReturn オブジェクトの状態を {@link #returnObject} メソッドを使って戻した後に確認するかどうか ({@link #getTestOnReturn} を参照)
     * {@primary whether or not to validate objects after they are returned to the {@link #returnObject} method (see {@link #getTestOnReturn})}
     * @param timeBetweenEvictionRunsMillis 未使用オブジェクト排除処理が次の実行までの間スリープする時間(ミリセカンド) ({@link #setTimeBetweenEvictionRunsMillis} を参照)
     * {@primary the amount of time (in milliseconds) to sleep between examining idle objects for eviction (see {@link #setTimeBetweenEvictionRunsMillis})}
     * @param numTestsPerEvictionRun 1度のオブジェクト排除処理でチェックされるオブジェクトの数 ({@link #setNumTestsPerEvictionRun} を参照)
     * {@primary the number of idle objects to examine per run within the idle object eviction thread (if any) (see {@link #setNumTestsPerEvictionRun})}
     * @param minEvictableIdleTimeMillis オブジェクトがプール内に未使用状態でいられる時間の最小値(ミリセカンド) ({@link #setMinEvictableIdleTimeMillis} を参照)
     * {@primary the minimum number of milliseconds an object can sit idle in the pool before it is eligable for evcition (see {@link #setMinEvictableIdleTimeMillis})}
     * @param testWhileIdle 未使用状態のオブジェクトを未使用オブジェクト排除スレッドでチェックするかどうか ({@link #setTestWhileIdle} を参照)
     * {@primary whether or not to validate objects in the idle object eviction thread, if any (see {@link #setTestWhileIdle})}
     */
    public GenericKeyedObjectPool(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) {
        _factory = factory;
        _maxActive = maxActive;
        switch(whenExhaustedAction) {
            case WHEN_EXHAUSTED_BLOCK:
            case WHEN_EXHAUSTED_FAIL:
            case WHEN_EXHAUSTED_GROW:
                _whenExhaustedAction = whenExhaustedAction;
                break;
            default:
                throw new IllegalArgumentException("whenExhaustedAction " + whenExhaustedAction + " not recognized.");
        }
        _maxWait = maxWait;
        _maxIdle = maxIdle;
        _maxTotal = maxTotal;
        _testOnBorrow = testOnBorrow;
        _testOnReturn = testOnReturn;
        _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
        _numTestsPerEvictionRun = numTestsPerEvictionRun;
        _minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
        _testWhileIdle = testWhileIdle;

        _poolMap = new HashMap();
        _activeMap = new HashMap();
        _poolList = new CursorableLinkedList();

        startEvictor(_timeBetweenEvictionRunsMillis);
    }

    //--- public methods ---------------------------------------------

    //--- configuration methods --------------------------------------

    /**
     * 同時にプールから取り出すことのできるインスタンス数の(キー毎の)最大値を返します。
     * {@primary Returns the cap on the number of active instances from my pool (per key).}
     * @return 同時にプールから取り出すことのできるインスタンス数の(キー毎の)最大値
     * {@primary the cap on the number of active instances from my pool (per key).}
     * @see #setMaxActive
     */
    public synchronized int getMaxActive() {
        return _maxActive;
    }

    /**
     * 同時にプールから取り出すことのできるインスタンス数の(キー毎の)最大値を設定します。
     * {@primary Sets the cap on the number of active instances from my pool (per key).}
     * @param maxActive 同時にプールから取り出すことのできるインスタンス数の(キー毎の)最大値。
     *                  インスタンスの数を制限しない場合にはマイナスの値を設定します
     * {@primary The cap on the number of active instances from my pool (per key).
     *           Use a negative value for an infinite number of instances.}
     * @see #getMaxActive
     */
    public synchronized void setMaxActive(int maxActive) {
        _maxActive = maxActive;
        notifyAll();
    }

    /**
     * プール内に保持することのできる(キー毎ではない)インスタンス総数の最大値を返します。
     * {@primary Returns the cap on the total number of instances from my pool.}
     * @return プール内に保持することのできるインスタンス総数の最大値
     * {@primary the cap on the total number of instances from my pool.}
     * @see #setMaxTotal
     */
    public synchronized int getMaxTotal() {
        return _maxTotal;
    }

    /**
     * プール内に保持することのできる(キー毎ではない)インスタンス総数の最大値を設定します。
     * {@primary Sets the cap on the total number of instances from my pool.}
     * @param maxTotal プール内に保持することのできるインスタンス総数の最大値。
     *                 インスタンスの数を制限しない場合にはマイナスの値を設定します
     * {@primary The cap on the total number of instances from my pool.
     *           Use a negative value for an infinite number of instances.}
     * @see #getMaxTotal
     */
    public synchronized void setMaxTotal(int maxTotal) {
        _maxTotal = maxTotal;
        notifyAll();
    }

    /**
     * プールが使い尽されている場合(取り出すことのできるオブジェクトが最大数に達した場合)に
     * {@link #borrowObject} メソッドが行う処理の種別を返します。
     * {@primary Returns the action to take when the {@link #borrowObject} method
     * is invoked when the pool is exhausted (the maximum number
     * of "active" objects has been reached).}
     *
     * @return {@link #WHEN_EXHAUSTED_BLOCK}、{@link #WHEN_EXHAUSTED_FAIL}、{@link #WHEN_EXHAUSTED_GROW} のうちののどれか
     * {@primary one of {@link #WHEN_EXHAUSTED_BLOCK}, {@link #WHEN_EXHAUSTED_FAIL} or {@link #WHEN_EXHAUSTED_GROW}}
     * @see #setWhenExhaustedAction
     */
    public synchronized byte getWhenExhaustedAction() {
        return _whenExhaustedAction;
    }

    /**
     * プールが使い尽されている場合(取り出すことのできるオブジェクトが最大数に達した場合)に
     * {@link #borrowObject} メソッドが行う処理の種別を設定します。
     * {@primary Sets the action to take when the {@link #borrowObject} method
     * is invoked when the pool is exhausted (the maximum number
     * of "active" objects has been reached).}
     *
     * @param whenExhaustedAction 処理の種別、
     *        {@link #WHEN_EXHAUSTED_BLOCK}、{@link #WHEN_EXHAUSTED_FAIL}、
     *        {@link #WHEN_EXHAUSTED_GROW} のうちののどれかである必要があります
     * {@primary the action code, which must be one of
     *           {@link #WHEN_EXHAUSTED_BLOCK}, {@link #WHEN_EXHAUSTED_FAIL},
     *            or {@link #WHEN_EXHAUSTED_GROW}}
     * @see #getWhenExhaustedAction
     */
    public synchronized void setWhenExhaustedAction(byte whenExhaustedAction) {
        switch(whenExhaustedAction) {
            case WHEN_EXHAUSTED_BLOCK:
            case WHEN_EXHAUSTED_FAIL:
            case WHEN_EXHAUSTED_GROW:
                _whenExhaustedAction = whenExhaustedAction;
                notifyAll();
                break;
            default:
                throw new IllegalArgumentException("whenExhaustedAction " + whenExhaustedAction + " not recognized.");
        }
    }


    /**
     * プールが使い尽されていて {@link #getWhenExhaustedAction "使い尽された時の処理"} に
     * {@link #WHEN_EXHAUSTED_BLOCK} が設定されている場合の
     * {@link #borrowObject} メソッドが例外を投げるまでの最長待機時間(ミリセカンド)を返します。
     * 0より小さな値が設定された場合、{@link #borrowObject}
     * メソッドは無期限に待機します。
     * {@primary Returns the maximum amount of time (in milliseconds) the
     * {@link #borrowObject} method should block before throwing
     * an exception when the pool is exhausted and the
     * {@link #setWhenExhaustedAction "when exhausted" action} is
     * {@link #WHEN_EXHAUSTED_BLOCK}.}
     * 
     * When less than 0, the {@link #borrowObject} method
     * may block indefinitely.}
     *
     * @see #setMaxWait
     * @see #setWhenExhaustedAction
     * @see #WHEN_EXHAUSTED_BLOCK
     */
    public synchronized long getMaxWait() {
        return _maxWait;
    }

    /**
     * プールが使い尽されていて {@link #getWhenExhaustedAction "使い尽された時の処理"} に
     * {@link #WHEN_EXHAUSTED_BLOCK} が設定されている場合の
     * {@link #borrowObject} メソッドが例外を投げるまでの最長待機時間(ミリセカンド)を設定します。
     * 0より小さな値が設定された場合、{@link #borrowObject}
     * メソッドは無期限に待機します。
     * {@primary Sets the maximum amount of time (in milliseconds) the
     * {@link #borrowObject} method should block before throwing
     * an exception when the pool is exhausted and the
     * {@link #setWhenExhaustedAction "when exhausted" action} is
     * {@link #WHEN_EXHAUSTED_BLOCK}.}
     * 
     * When less than 0, the {@link #borrowObject} method
     * may block indefinitely.}
     *
     * @see #getMaxWait
     * @see #setWhenExhaustedAction
     * @see #WHEN_EXHAUSTED_BLOCK
     */
    public synchronized void setMaxWait(long maxWait) {
        _maxWait = maxWait;
    }

    /**
     * プール内に保持できる未使用のオブジェクトの最大数を返します。
     * {@primary Returns the cap on the number of "idle" instances in the pool.}
     * @return プール内に保持できる未使用のオブジェクトの最大数
     * {@primary the cap on the number of "idle" instances in the pool.}
     * @see #setMaxIdle
     */
    public synchronized int getMaxIdle() {
        return _maxIdle;
    }

    /**
     * プール内に保持できる未使用のオブジェクトの最大数を設定します。
     * {@primary Sets the cap on the number of "idle" instances in the pool.}
     * @param maxIdle プール内に保持できる未使用のオブジェクトの最大数
     *                マイナスの値が設定された場合には同時にプール内に保持できるオブジェクトを制限しません
     * {@primary The cap on the number of "idle" instances in the pool.
     *           Use a negative value to indicate an unlimited number
     *           of idle instances.}
     * @see #getMaxIdle
     */
    public synchronized void setMaxIdle(int maxIdle) {
        _maxIdle = maxIdle;
        notifyAll();
    }

    /**
     * この値が <tt>true</tt> の場合
     * インスタンスが {@link #borrowObject} メソッドにて取り出される前に
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject 有効かどうかの確認} を行います。
     * 有効でないと判断された場合、オブジェクトはプールから破棄され、他のオブジェクトが取り出されます。
     * {@primary When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * before being returned by the {@link #borrowObject}
     * method.  If the object fails to validate,
     * it will be dropped from the pool, and we will attempt
     * to borrow another.}
     *
     * @see #setTestOnBorrow
     */
    public synchronized boolean getTestOnBorrow() {
        return _testOnBorrow;
    }

    /**
     * この値が <tt>true</tt> の場合
     * インスタンスが {@link #borrowObject} メソッドにて取り出される前に
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject 有効かどうかの確認} を行います。
     * 有効でないと判断された場合、オブジェクトはプールから破棄され、他のオブジェクトが取り出されます。
     * {@primary When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * before being returned by the {@link #borrowObject}
     * method.  If the object fails to validate,
     * it will be dropped from the pool, and we will attempt
     * to borrow another.}
     *
     * @see #getTestOnBorrow
     */
    public synchronized void setTestOnBorrow(boolean testOnBorrow) {
        _testOnBorrow = testOnBorrow;
    }

    /**
     * この値が <tt>true</tt> の場合
     * オブジェクトが {@link #returnObject} メソッドにて戻される前に
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject 有効かどうかの確認} を行います。
     * {@primary When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * before being returned to the pool within the
     * {@link #returnObject}.}
     *
     * @see #setTestOnReturn
     */
    public synchronized boolean getTestOnReturn() {
        return _testOnReturn;
    }

    /**
     * この値が <tt>true</tt> の場合
     * オブジェクトが {@link #returnObject} メソッドにて戻される前に
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject 有効かどうかの確認} を行います。
     * {@primary When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * before being returned to the pool within the
     * {@link #returnObject}.}
     *
     * @see #getTestOnReturn
     */
    public synchronized void setTestOnReturn(boolean testOnReturn) {
        _testOnReturn = testOnReturn;
    }

    /**
     * 未使用オブジェクト排除処理が次の実行までの間スリープする時間(ミリセカンド)を返します。
     * マイナスの値が設定された場合、排除スレッドは起動しません。
     * {@primary Returns the number of milliseconds to sleep between runs of the
     * idle object evictor thread.
     * When non-positive, no idle object evictor thread will be
     * run.}
     *
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public synchronized long getTimeBetweenEvictionRunsMillis() {
        return _timeBetweenEvictionRunsMillis;
    }

    /**
     * 未使用オブジェクト排除処理が次の実行までの間スリープする時間(ミリセカンド)を設定します。
     * マイナスの値が設定された場合、排除スレッドは起動しません。
     * {@primary Sets the number of milliseconds to sleep between runs of the
     * idle object evictor thread.
     * When non-positive, no idle object evictor thread will be
     * run.}
     *
     * @see #getTimeBetweenEvictionRunsMillis
     */
    public synchronized void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
        _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
        startEvictor(_timeBetweenEvictionRunsMillis);
    }

    /**
     * 1度のオブジェクト排除処理で排除スレッドにチェックされるオブジェクトの数を返します。
     * {@primary Returns the number of objects to examine during each run of the
     * idle object evictor thread (if any).}
     *
     * @see #setNumTestsPerEvictionRun
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public synchronized int getNumTestsPerEvictionRun() {
        return _numTestsPerEvictionRun;
    }

    /**
     * 1度のオブジェクト排除処理で排除スレッドにチェックされるオブジェクトの数を返します。
     * {@primary Sets the number of objects to examine during each run of the
     * idle object evictor thread (if any).}
     * <p>
     * マイナスの値が設定された場合、<tt>ceil({@link #getNumIdle})/abs({@link #getNumTestsPerEvictionRun})</tt>
     * 回のチェックを実施します。  例えば <i>-n</i> が設定された場合には、1/<i>n</i>
     * の未使用オブジェクトが1度のオブジェクト排除処理でチェックされます。
     * {@primary When a negative value is supplied, <tt>ceil({@link #getNumIdle})/abs({@link #getNumTestsPerEvictionRun})</tt>
     * tests will be run.  I.e., when the value is <i>-n</i>, roughly one <i>n</i>th of the
     * idle objects will be tested per run.}
     *
     * @see #getNumTestsPerEvictionRun
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public synchronized void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
        _numTestsPerEvictionRun = numTestsPerEvictionRun;
    }

    /**
     * オブジェクトがプール内に未使用状態でいられる時間の最小値を返します。
     * 未使用状態でいる時間がこの値に達すると排除処理の対象となります。
     * {@primary Returns the minimum amount of time an object may sit idle in the pool
     * before it is eligable for eviction by the idle object evictor
     * (if any).}
     *
     * @see #setMinEvictableIdleTimeMillis
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public synchronized long getMinEvictableIdleTimeMillis() {
        return _minEvictableIdleTimeMillis;
    }

    /**
     * オブジェクトがプール内に未使用状態でいられる時間の最小値を設定します。
     * 未使用状態でいる時間がこの値に達すると排除処理の対象となります。
     * マイナスの値が設定された場合、未使用状態でいる時間が原因ではオブジェクトの削除は行われません。
     * {@primary Sets the minimum amount of time an object may sit idle in the pool
     * before it is eligable for eviction by the idle object evictor
     * (if any).
     * When non-positive, no objects will be evicted from the pool
     * due to idle time alone.}
     *
     * @see #getMinEvictableIdleTimeMillis
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public synchronized void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
        _minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    /**
     * この値が <tt>true</tt> の場合
     * オブジェクト排除処理によってオブジェクトに対する
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject 有効かどうかの確認} が実施されます。
     * 有効でないと判断されたオブジェクトはプールから破棄されます。
     * {@primary When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * by the idle object evictor (if any).  If an object
     * fails to validate, it will be dropped from the pool.}
     *
     * @see #setTestWhileIdle
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public synchronized boolean getTestWhileIdle() {
        return _testWhileIdle;
    }

    /**
     * この値が <tt>true</tt> の場合
     * オブジェクト排除処理によってオブジェクトに対する
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject 有効かどうかの確認} が実施されます。
     * 有効でないと判断されたオブジェクトはプールから破棄されます。
     * {@primary When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * by the idle object evictor (if any).  If an object
     * fails to validate, it will be dropped from the pool.}
     *
     * @see #getTestWhileIdle
     * @see #setTimeBetweenEvictionRunsMillis
     */
    public synchronized void setTestWhileIdle(boolean testWhileIdle) {
        _testWhileIdle = testWhileIdle;
    }

    /**
     * 設定情報を登録します。
     * {@primary Sets my configuration.}
     * @see GenericKeyedObjectPool.Config
     */
    public synchronized void setConfig(GenericKeyedObjectPool.Config conf) {
        setMaxIdle(conf.maxIdle);
        setMaxActive(conf.maxActive);
        setMaxTotal(conf.maxTotal);
        setMaxWait(conf.maxWait);
        setWhenExhaustedAction(conf.whenExhaustedAction);
        setTestOnBorrow(conf.testOnBorrow);
        setTestOnReturn(conf.testOnReturn);
        setTestWhileIdle(conf.testWhileIdle);
        setNumTestsPerEvictionRun(conf.numTestsPerEvictionRun);
        setMinEvictableIdleTimeMillis(conf.minEvictableIdleTimeMillis);
        setTimeBetweenEvictionRunsMillis(conf.timeBetweenEvictionRunsMillis);
    }

    //-- ObjectPool methods ------------------------------------------

    public synchronized Object borrowObject(Object key) throws Exception {
        long starttime = System.currentTimeMillis();
        boolean newlyCreated = false;
        for(;;) {
            CursorableLinkedList pool = (CursorableLinkedList)(_poolMap.get(key));
            if(null == pool) {
                pool = new CursorableLinkedList();
                _poolMap.put(key,pool);
                _poolList.add(key);
            }
            ObjectTimestampPair pair = null;
            // if there are any sleeping, just grab one of those
            try {
                pair = (ObjectTimestampPair)(pool.removeFirst());
                if(null != pair) {
                    _totalIdle--;
                }
            } catch(NoSuchElementException e) { /* ignored */
            }
            // otherwise
            if(null == pair) {
                // if there is a totalMaxActive and we are at the limit then
                // we have to make room
                // TODO: this could be improved by only removing the oldest object
                if ((_maxTotal > 0) && (_totalActive + _totalIdle >= _maxTotal)) {
                    clear();
                }
                
                // check if we can create one
                // (note we know that the num sleeping is 0, else we wouldn't be here)
                int active = getActiveCount(key);
                if ((_maxActive <= 0 || active < _maxActive) &&
                    (_maxTotal <= 0 || _totalActive + _totalIdle < _maxTotal)) {
                    Object obj = _factory.makeObject(key);
                    pair = new ObjectTimestampPair(obj);
                    newlyCreated = true;
                } else {
                    // the pool is exhausted
                    switch(_whenExhaustedAction) {
                        case WHEN_EXHAUSTED_GROW:
                            Object obj = _factory.makeObject(key);
                            pair = new ObjectTimestampPair(obj);
                            break;
                        case WHEN_EXHAUSTED_FAIL:
                            throw new NoSuchElementException();
                        case WHEN_EXHAUSTED_BLOCK:
                            try {
                                if(_maxWait <= 0) {
                                    wait();
                                } else {
                                    wait(_maxWait);
                                }
                            } catch(InterruptedException e) {
                                // ignored
                            }
                            if(_maxWait > 0 && ((System.currentTimeMillis() - starttime) >= _maxWait)) {
                                throw new NoSuchElementException("Timeout waiting for idle object");
                            } else {
                                continue; // keep looping
                            }
                        default:
                            throw new IllegalArgumentException("whenExhaustedAction " + _whenExhaustedAction + " not recognized.");
                    }
                }
            }
            _factory.activateObject(key,pair.value);
            if(_testOnBorrow && !_factory.validateObject(key,pair.value)) {
                _factory.destroyObject(key,pair.value);
                if(newlyCreated) {
                    throw new NoSuchElementException("Could not create a validated object");
                } // else keep looping
            } else {
                incrementActiveCount(key);
                return pair.value;
            }
        }
    }

    public synchronized void clear() {
        for(Iterator keyiter = _poolList.iterator(); keyiter.hasNext(); ) {
            Object key = keyiter.next();
            CursorableLinkedList list = (CursorableLinkedList)(_poolMap.get(key));
            for(Iterator it = list.iterator(); it.hasNext(); ) {
                try {
                    _factory.destroyObject(key,((ObjectTimestampPair)(it.next())).value);
                } catch(Exception e) {
                    // ignore error, keep destroying the rest
                }
                it.remove();
            }
        }
        _poolMap.clear();
        _poolList.clear();
        _totalIdle = 0;
        notifyAll();
    }

    public synchronized void clear(Object key) {
        CursorableLinkedList pool = (CursorableLinkedList)(_poolMap.remove(key));
        if(null == pool) {
            return;
        } else {
            _poolList.remove(key);
            for(Iterator it = pool.iterator(); it.hasNext(); ) {
                try {
                    _factory.destroyObject(key,((ObjectTimestampPair)(it.next())).value);
                } catch(Exception e) {
                    // ignore error, keep destroying the rest
                }
                it.remove();
                _totalIdle--;
            }
        }
        notifyAll();
    }

    public synchronized int getNumActive() {
        return _totalActive;
    }

    public synchronized int getNumIdle() {
        return _totalIdle;
    }

    public synchronized int getNumActive(Object key) {
        return getActiveCount(key);
    }

    public synchronized int getNumIdle(Object key) {
        try {
            return((CursorableLinkedList)(_poolMap.get(key))).size();
        } catch(Exception e) {
            return 0;
        }
    }

    public void returnObject(Object key, Object obj) throws Exception {

        // if we need to validate this object, do so
        boolean success = true; // whether or not this object passed validation
        if(_testOnReturn && !_factory.validateObject(key, obj)) {
            success = false;
            try {
                _factory.destroyObject(key, obj);
            } catch(Exception e) {
                // ignored
            }
        } else {
            try {
                _factory.passivateObject(key, obj);
            } catch(Exception e) {
                success = false;
            }
        }

        boolean shouldDestroy = false;
        synchronized(this) {
            // grab the pool (list) of objects associated with the given key
            CursorableLinkedList pool = (CursorableLinkedList) (_poolMap.get(key));
            // if it doesn't exist, create it
            if(null == pool) {
                pool = new CursorableLinkedList();
                _poolMap.put(key, pool);
                _poolList.add(key);
            }
            decrementActiveCount(key);
            // if there's no space in the pool, flag the object for destruction
            // else if we passivated succesfully, return it to the pool
            if(_maxIdle >= 0 && (pool.size() >= _maxIdle)) {
                shouldDestroy = true;
            } else if(success) {
                pool.addFirst(new ObjectTimestampPair(obj));
                _totalIdle++;
            }
            notifyAll();
        }

        if(shouldDestroy) {
            try {
                _factory.destroyObject(key, obj);
            } catch(Exception e) {
                // ignored?
            }
        }
    }

    public void invalidateObject(Object key, Object obj) throws Exception {
        try {
            _factory.destroyObject(key, obj);
        }
        finally {
            synchronized(this) {
                decrementActiveCount(key);
                notifyAll(); // _totalActive has changed
            }
        }
    }

    public void addObject(Object key) throws Exception {
        Object obj = _factory.makeObject(key);
        synchronized(this) {
            incrementActiveCount(key); // returnObject will decrement this
            returnObject(key,obj);
        }
    }

    public synchronized void close() throws Exception {
        clear();
        _poolList = null;
        _poolMap = null;
        _activeMap = null;
        if(null != _evictionCursor) {
            _evictionCursor.close();
            _evictionCursor = null;
        }
        if(null != _evictionKeyCursor) {
            _evictionKeyCursor.close();
            _evictionKeyCursor = null;
        }
        if(null != _evictor) {
            _evictor.cancel();
            _evictor = null;
        }
    }

    public synchronized void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException {
        if(0 < getNumActive()) {
            throw new IllegalStateException("Objects are already active");
        } else {
            clear();
            _factory = factory;
        }
    }

    public synchronized void evict() throws Exception {
        Object key = null;
        for(int i=0,m=getNumTests();i<m;i++) {
            if(_poolMap.size() > 0) {
                // if we don't have a key cursor, then create one, and close any object cursor
                if(null == _evictionKeyCursor) {
                    _evictionKeyCursor = _poolList.cursor();
                    key = null;
                    if(null != _evictionCursor) {
                        _evictionCursor.close();
                        _evictionCursor = null;
                    }
                }
                // if we don't have an object cursor
                if(null == _evictionCursor) {
                    // if the _evictionKeyCursor has a next value, then use it
                    if(_evictionKeyCursor.hasNext()) {
                        key = _evictionKeyCursor.next();
                        CursorableLinkedList pool = (CursorableLinkedList)(_poolMap.get(key));
                        _evictionCursor = pool.cursor(pool.size());
                    } else {
                        // else close the key cursor and loop back around
                        if(null != _evictionKeyCursor) {
                            _evictionKeyCursor.close();
                            _evictionKeyCursor = _poolList.cursor();
                            if(null != _evictionCursor) {
                                _evictionCursor.close();
                                _evictionCursor = null;
                            }
                        }
                        continue;
                    }
                }
                // if the _evictionCursor has a previous object, then test it
                if(_evictionCursor.hasPrevious()) {
                    ObjectTimestampPair pair = (ObjectTimestampPair)(_evictionCursor.previous());
                    boolean removeObject=false;
                    if(_minEvictableIdleTimeMillis > 0 &&
                       System.currentTimeMillis() - pair.tstamp > _minEvictableIdleTimeMillis) {
                       removeObject=true;
                    } else if(_testWhileIdle) {
                        boolean active = false;
                        try {
                            _factory.activateObject(key,pair.value);
                            active = true;
                        } catch(Exception e) {
                            removeObject=true;
                        }
                        if(active) {
                            if(!_factory.validateObject(key,pair.value)) {
                                removeObject=true;
                            } else {
                                try {
                                    _factory.passivateObject(key,pair.value);
                                } catch(Exception e) {
                                    removeObject=true;
                                }
                            }
                        }
                    }
                    if(removeObject) {
                        try {
                            _evictionCursor.remove();
                            _totalIdle--;
                            _factory.destroyObject(key,pair.value);

                            // if that was the last object for that key, drop that pool
                            if( ((CursorableLinkedList)(_poolMap.get(key))).isEmpty() ) {
                                _poolMap.remove(key);
                                _poolList.remove(key);
                            }
                        } catch(Exception e) {
                            ; // ignored
                        }
                    }
                } else {
                    // else the _evictionCursor is done, so close it and loop around
                    if(_evictionCursor != null) {
                        _evictionCursor.close();
                        _evictionCursor = null;
                    }
                }
            }
        }
    }

    //--- non-public methods ----------------------------------------

    /**
     * Start the eviction thread or service, or when
     * <i>delay</i> is non-positive, stop it
     * if it is already running.
     */
    protected synchronized void startEvictor(long delay) {
        if(null != _evictor) {
            _evictor.cancel();
            _evictor = null;
        }
        if(delay > 0) {
            _evictor = new Evictor(delay);
            Thread t = new Thread(_evictor);
            t.setDaemon(true);
            t.start();
        }
    }

    synchronized String debugInfo() {
        StringBuffer buf = new StringBuffer();
        buf.append("Active: ").append(getNumActive()).append("\n");
        buf.append("Idle: ").append(getNumIdle()).append("\n");
        Iterator it = _poolList.iterator();
        while(it.hasNext()) {
            buf.append("\t").append(_poolMap.get(it.next())).append("\n");
        }
        return buf.toString();
    }

    private synchronized int getNumTests() {
        if(_numTestsPerEvictionRun >= 0) {
            return _numTestsPerEvictionRun;
        } else {
            return(int)(Math.ceil((double)_totalIdle/Math.abs((double)_numTestsPerEvictionRun)));
        }
    }

    private synchronized void incrementActiveCount(Object key) {
        _totalActive++;
        Integer active = (Integer)(_activeMap.get(key));
        if(null == active) {
            _activeMap.put(key,new Integer(1));
        } else {
            _activeMap.put(key,new Integer(active.intValue() + 1));
        }
    }

    private synchronized void decrementActiveCount(Object key) {
        _totalActive--;
        Integer active = (Integer)(_activeMap.get(key));
        if(null == active) {
            // do nothing, either null or zero is OK
        } else if(active.intValue() <= 1) {
            _activeMap.remove(key);
        } else {
            _activeMap.put(key, new Integer(active.intValue() - 1));
        }
    }

    private synchronized int getActiveCount(Object key) {
        int active = 0;
        Integer act = (Integer)(_activeMap.get(key));
        if(null != act) {
            active = act.intValue();
        }
        return active;
    }

    //--- inner classes ----------------------------------------------

    /**
     * A simple "struct" encapsulating an object instance and a timestamp.
     */
    class ObjectTimestampPair {
        Object value;
        long tstamp;

        ObjectTimestampPair(Object val) {
            value = val;
            tstamp = System.currentTimeMillis();
        }

        ObjectTimestampPair(Object val, long time) {
            value = val;
            tstamp = time;
        }

        public String toString() {
            return value + ";" + tstamp;
        }
    }

    /**
     * The idle object evictor thread.
     * @see #setTimeBetweenEvictionRunsMillis
     */
    class Evictor implements Runnable {
        private boolean _cancelled = false;
        private long _delay = 0L;

        public Evictor(long delay) {
            _delay = delay;
        }

        void cancel() {
            _cancelled = true;
        }

        public void run() {
            while(!_cancelled) {
                long sleeptime = 0L;
                synchronized(GenericKeyedObjectPool.this) {
                    sleeptime = _timeBetweenEvictionRunsMillis;
                }
                try {
                    Thread.sleep(sleeptime);
                } catch(Exception e) {
                    ; // ignored
                }
                try {
                    evict();
                } catch(Exception e) {
                    ; // ignored
                }
            }
            synchronized(GenericKeyedObjectPool.this) {
                if(null != _evictionCursor) {
                    _evictionCursor.close();
                    _evictionCursor = null;
                }
                if(null != _evictionKeyCursor) {
                    _evictionKeyCursor.close();
                    _evictionKeyCursor = null;
                }
            }
        }
    }

    /**
     * {@link GenericKeyedObjectPool} のための設定情報を格納したシンプルな "構造体(のようなもの)" です。
     * {@primary A simple "struct" encapsulating the
     * configuration information for a {@link GenericKeyedObjectPool}.}
     * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory,GenericKeyedObjectPool.Config)
     * @see GenericKeyedObjectPool#setConfig
     */
    public static class Config {
        public int maxIdle = GenericKeyedObjectPool.DEFAULT_MAX_IDLE;
        public int maxActive = GenericKeyedObjectPool.DEFAULT_MAX_ACTIVE;
        public int maxTotal = GenericKeyedObjectPool.DEFAULT_MAX_TOTAL;
        public long maxWait = GenericKeyedObjectPool.DEFAULT_MAX_WAIT;
        public byte whenExhaustedAction = GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION;
        public boolean testOnBorrow = GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW;
        public boolean testOnReturn = GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN;
        public boolean testWhileIdle = GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE;
        public long timeBetweenEvictionRunsMillis = GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
        public int numTestsPerEvictionRun =  GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
        public long minEvictableIdleTimeMillis = GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
    }

    //--- protected attributes ---------------------------------------

    /**
     * The cap on the number of idle instances in the pool (per key).
     * @see #setMaxIdle
     * @see #getMaxIdle
     */
    private int _maxIdle = DEFAULT_MAX_IDLE;

    /**
     * The cap on the number of active instances from the pool (per key).
     * @see #setMaxActive
     * @see #getMaxActive
     */
    private int _maxActive = DEFAULT_MAX_ACTIVE;

    /**
     * The cap on the total number of instances from the pool.
     * @see #setMaxTotal
     * @see #getMaxTotal
     */
    private int _maxTotal = DEFAULT_MAX_TOTAL;
    
    /**
     * The maximum amount of time (in millis) the
     * {@link #borrowObject} method should block before throwing
     * an exception when the pool is exhausted and the
     * {@link #getWhenExhaustedAction "when exhausted" action} is
     * {@link #WHEN_EXHAUSTED_BLOCK}.
     *
     * When less than 0, the {@link #borrowObject} method
     * may block indefinitely.
     *
     * @see #setMaxWait
     * @see #getMaxWait
     * @see #WHEN_EXHAUSTED_BLOCK
     * @see #setWhenExhaustedAction
     * @see #getWhenExhaustedAction
     */
    private long _maxWait = DEFAULT_MAX_WAIT;

    /**
     * The action to take when the {@link #borrowObject} method
     * is invoked when the pool is exhausted (the maximum number
     * of "active" objects has been reached).
     *
     * @see #WHEN_EXHAUSTED_BLOCK
     * @see #WHEN_EXHAUSTED_FAIL
     * @see #WHEN_EXHAUSTED_GROW
     * @see #DEFAULT_WHEN_EXHAUSTED_ACTION
     * @see #setWhenExhaustedAction
     * @see #getWhenExhaustedAction
     */
    private byte _whenExhaustedAction = DEFAULT_WHEN_EXHAUSTED_ACTION;

    /**
     * When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * before being returned by the {@link #borrowObject}
     * method.  If the object fails to validate,
     * it will be dropped from the pool, and we will attempt
     * to borrow another.
     *
     * @see #setTestOnBorrow
     * @see #getTestOnBorrow
     */
    private boolean _testOnBorrow = DEFAULT_TEST_ON_BORROW;

    /**
     * When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * before being returned to the pool within the
     * {@link #returnObject}.
     *
     * @see #getTestOnReturn
     * @see #setTestOnReturn
     */
    private boolean _testOnReturn = DEFAULT_TEST_ON_RETURN;

    /**
     * When <tt>true</tt>, objects will be
     * {@link org.apache.commons.pool.PoolableObjectFactory#validateObject validated}
     * by the idle object evictor (if any).  If an object
     * fails to validate, it will be dropped from the pool.
     *
     * @see #setTestWhileIdle
     * @see #getTestWhileIdle
     * @see #getTimeBetweenEvictionRunsMillis
     * @see #setTimeBetweenEvictionRunsMillis
     */
    private boolean _testWhileIdle = DEFAULT_TEST_WHILE_IDLE;

    /**
     * The number of milliseconds to sleep between runs of the
     * idle object evictor thread.
     * When non-positive, no idle object evictor thread will be
     * run.
     *
     * @see #setTimeBetweenEvictionRunsMillis
     * @see #getTimeBetweenEvictionRunsMillis
     */
    private long _timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;

    /**
     * The number of objects to examine during each run of the
     * idle object evictor thread (if any).
     * <p>
     * When a negative value is supplied, <tt>ceil({@link #getNumIdle})/abs({@link #getNumTestsPerEvictionRun})</tt>
     * tests will be run.  I.e., when the value is <i>-n</i>, roughly one <i>n</i>th of the
     * idle objects will be tested per run.
     *
     * @see #setNumTestsPerEvictionRun
     * @see #getNumTestsPerEvictionRun
     * @see #getTimeBetweenEvictionRunsMillis
     * @see #setTimeBetweenEvictionRunsMillis
     */
    private int _numTestsPerEvictionRun =  DEFAULT_NUM_TESTS_PER_EVICTION_RUN;

    /**
     * The minimum amount of time an object may sit idle in the pool
     * before it is eligable for eviction by the idle object evictor
     * (if any).
     * When non-positive, no objects will be evicted from the pool
     * due to idle time alone.
     *
     * @see #setMinEvictableIdleTimeMillis
     * @see #getMinEvictableIdleTimeMillis
     * @see #getTimeBetweenEvictionRunsMillis
     * @see #setTimeBetweenEvictionRunsMillis
     */
    private long _minEvictableIdleTimeMillis = DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;

    /** My hash of pools (CursorableLinkedLists). */
    private HashMap _poolMap = null;

    /**
     * A cursorable list of my pools.
     * @see GenericKeyedObjectPool.Evictor#run
     */
    private CursorableLinkedList _poolList = null;

    /** Count of active objects, per key. */
    private HashMap _activeMap = null;

    /** The total number of active instances. */
    private int _totalActive = 0;

    /** The total number of idle instances. */
    private int _totalIdle = 0;

    /** My {@link KeyedPoolableObjectFactory}. */
    private KeyedPoolableObjectFactory _factory = null;

    /**
     * My idle object eviction thread, if any.
     */
    private Evictor _evictor = null;

    private CursorableLinkedList.Cursor _evictionCursor = null;
    private CursorableLinkedList.Cursor _evictionKeyCursor = null;

}

