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