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 */
017package org.apache.commons.pool2;
018
019/**
020 * A base implementation of <code>KeyedPooledObjectFactory</code>.
021 * <p>
022 * All operations defined here are essentially no-op's.
023 * </p>
024 * This class is immutable, and therefore thread-safe.
025 *
026 * @see KeyedPooledObjectFactory
027 *
028 * @param <K> The type of keys managed by this factory.
029 * @param <V> Type of element managed by this factory.
030 *
031 * @since 2.0
032 */
033public abstract class BaseKeyedPooledObjectFactory<K,V> extends BaseObject
034        implements KeyedPooledObjectFactory<K,V> {
035
036    /**
037     * Create an instance that can be served by the pool.
038     *
039     * @param key the key used when constructing the object
040     * @return an instance that can be served by the pool
041     *
042     * @throws Exception if there is a problem creating a new instance,
043     *    this will be propagated to the code requesting an object.
044     */
045    public abstract V create(K key)
046        throws Exception;
047
048    /**
049     * Wrap the provided instance with an implementation of
050     * {@link PooledObject}.
051     *
052     * @param value the instance to wrap
053     *
054     * @return The provided instance, wrapped by a {@link PooledObject}
055     */
056    public abstract PooledObject<V> wrap(V value);
057
058    @Override
059    public PooledObject<V> makeObject(final K key) throws Exception {
060        return wrap(create(key));
061    }
062
063    /**
064     * Destroy an instance no longer needed by the pool.
065     * <p>
066     * The default implementation is a no-op.
067     *
068     * @param key the key used when selecting the instance
069     * @param p a {@code PooledObject} wrapping the instance to be destroyed
070     */
071    @Override
072    public void destroyObject(final K key, final PooledObject<V> p)
073        throws Exception {
074        // The default implementation is a no-op.
075    }
076
077    /**
078     * Ensures that the instance is safe to be returned by the pool.
079     * <p>
080     * The default implementation always returns {@code true}.
081     *
082     * @param key the key used when selecting the object
083     * @param p a {@code PooledObject} wrapping the instance to be validated
084     * @return always <code>true</code> in the default implementation
085     */
086    @Override
087    public boolean validateObject(final K key, final PooledObject<V> p) {
088        return true;
089    }
090
091    /**
092     * Reinitialize an instance to be returned by the pool.
093     * <p>
094     * The default implementation is a no-op.
095     *
096     * @param key the key used when selecting the object
097     * @param p a {@code PooledObject} wrapping the instance to be activated
098     */
099    @Override
100    public void activateObject(final K key, final PooledObject<V> p)
101        throws Exception {
102        // The default implementation is a no-op.
103    }
104
105    /**
106     * Uninitialize an instance to be returned to the idle object pool.
107     * <p>
108     * The default implementation is a no-op.
109     *
110     * @param key the key used when selecting the object
111     * @param p a {@code PooledObject} wrapping the instance to be passivated
112     */
113    @Override
114    public void passivateObject(final K key, final PooledObject<V> p)
115        throws Exception {
116        // The default implementation is a no-op.
117    }
118}