001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.pool.impl;
019    
020    import org.apache.commons.pool.ObjectPool;
021    import org.apache.commons.pool.ObjectPoolFactory;
022    import org.apache.commons.pool.PoolableObjectFactory;
023    
024    /**
025     * A factory for creating {@link StackObjectPool} instances.
026     *
027     * @param <T> the type of objects held in this pool
028     * 
029     * @see StackObjectPool
030     * @see StackKeyedObjectPoolFactory
031     *
032     * @author Rodney Waldhoff
033     * @version $Revision: 1222396 $ $Date: 2011-12-22 14:02:25 -0500 (Thu, 22 Dec 2011) $
034     * @since Pool 1.0
035     */
036    public class StackObjectPoolFactory<T> implements ObjectPoolFactory<T> {
037        /**
038         * Create a new StackObjectPoolFactory.
039         *
040         * @see StackObjectPool#StackObjectPool()
041         * @deprecated to be removed in pool 2.0 - use {@link #StackObjectPoolFactory(PoolableObjectFactory)}
042         */
043        @Deprecated
044        public StackObjectPoolFactory() {
045            this(null,StackObjectPool.DEFAULT_MAX_SLEEPING,StackObjectPool.DEFAULT_INIT_SLEEPING_CAPACITY);
046        }
047    
048        /**
049         * Create a new StackObjectPoolFactory.
050         *
051         * @param maxIdle cap on the number of "sleeping" instances in the pool.
052         * @see StackObjectPool#StackObjectPool(int)
053         * @deprecated to be removed in pool 2.0 - use {@link #StackObjectPoolFactory(PoolableObjectFactory, int)}
054         */
055        @Deprecated
056        public StackObjectPoolFactory(int maxIdle) {
057            this(null,maxIdle,StackObjectPool.DEFAULT_INIT_SLEEPING_CAPACITY);
058        }
059    
060        /**
061         * Create a new StackObjectPoolFactory.
062         *
063         * @param maxIdle cap on the number of "sleeping" instances in the pool.
064         * @param initIdleCapacity - initial size of the pool (this specifies the size of the container,
065         * it does not cause the pool to be pre-populated.)
066         * @see StackObjectPool#StackObjectPool(int, int)
067         * @deprecated to be removed in pool 2.0 - use {@link #StackObjectPoolFactory(PoolableObjectFactory, int, int)}
068         */
069        @Deprecated
070        public StackObjectPoolFactory(int maxIdle, int initIdleCapacity) {
071            this(null,maxIdle,initIdleCapacity);
072        }
073    
074        /**
075         * Create a new StackObjectPoolFactory.
076         *
077         * @param factory the PoolableObjectFactory used by created pools.
078         * @see StackObjectPool#StackObjectPool(PoolableObjectFactory)
079         */
080        public StackObjectPoolFactory(PoolableObjectFactory<T> factory) {
081            this(factory,StackObjectPool.DEFAULT_MAX_SLEEPING,StackObjectPool.DEFAULT_INIT_SLEEPING_CAPACITY);
082        }
083    
084        /**
085         * Create a new StackObjectPoolFactory.
086         *
087         * @param factory the PoolableObjectFactory used by created pools.
088         * @param maxIdle cap on the number of "sleeping" instances in the pool.
089         */
090        public StackObjectPoolFactory(PoolableObjectFactory<T> factory, int maxIdle) {
091            this(factory,maxIdle,StackObjectPool.DEFAULT_INIT_SLEEPING_CAPACITY);
092        }
093    
094        /**
095         * Create a new StackObjectPoolFactory.
096         *
097         * @param factory the PoolableObjectFactory used by created pools.
098         * @param maxIdle cap on the number of "sleeping" instances in the pool.
099         * @param initIdleCapacity - initial size of the pool (this specifies the size of the container,
100         * it does not cause the pool to be pre-populated.)
101         */
102        public StackObjectPoolFactory(PoolableObjectFactory<T> factory, int maxIdle, int initIdleCapacity) {
103            _factory = factory;
104            _maxSleeping = maxIdle;
105            _initCapacity = initIdleCapacity;
106        }
107    
108        /**
109         * Create a StackObjectPool.
110         * 
111         * @return a new StackObjectPool with the configured factory, maxIdle and initial capacity settings
112         */
113        public ObjectPool<T> createPool() {
114            return new StackObjectPool<T>(_factory,_maxSleeping,_initCapacity);
115        }
116    
117        /**
118         * The PoolableObjectFactory used by created pools.
119         * @deprecated to be made private in pool 2.0
120         */
121        @Deprecated
122        protected PoolableObjectFactory<T> _factory = null;
123        
124        /**
125         * The maximum number of idle instances in created pools.
126         * @deprecated to be made private in pool 2.0
127         */
128        @Deprecated
129        protected int _maxSleeping = StackObjectPool.DEFAULT_MAX_SLEEPING;
130        
131        /**
132         * The initial size of created pools.
133         * @deprecated to be made private in pool 2.0
134         */
135        @Deprecated
136        protected int _initCapacity = StackObjectPool.DEFAULT_INIT_SLEEPING_CAPACITY;
137    
138        /**
139         * Returns the factory used by created pools.
140         * 
141         * @return the PoolableObjectFactory used by created pools
142         * @since 1.5.5
143         */
144        public PoolableObjectFactory<T> getFactory() {
145            return _factory;
146        }
147    
148        /**
149         * Returns the maxIdle setting for created pools.
150         * 
151         * @return the maximum number of idle instances in created pools
152         * @since 1.5.5
153         */
154        public int getMaxSleeping() {
155            return _maxSleeping;
156        }
157    
158        /**
159         * Returns the initial capacity of created pools.
160         * 
161         * @return size of created containers (created pools are not pre-populated)
162         * @since 1.5.5
163         */
164        public int getInitCapacity() {
165            return _initCapacity;
166        }
167    
168    }