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