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 simple base implementation of {@link ObjectPool}.
021 * Optional operations are implemented to either do nothing, return a value
022 * indicating it is unsupported or throw {@link UnsupportedOperationException}.
023 * <p>
024 * This class is intended to be thread-safe.
025 *
026 * @param <T> Type of element pooled in this pool.
027 *
028 * @version $Revision: 1566605 $
029 *
030 * @since 2.0
031 */
032public abstract class BaseObjectPool<T> implements ObjectPool<T> {
033
034    @Override
035    public abstract T borrowObject() throws Exception;
036
037    @Override
038    public abstract void returnObject(T obj) throws Exception;
039
040    @Override
041    public abstract void invalidateObject(T obj) throws Exception;
042
043    /**
044     * Not supported in this base implementation.
045     *
046     * @return a negative value.
047     */
048    @Override
049    public int getNumIdle() {
050        return -1;
051    }
052
053    /**
054     * Not supported in this base implementation.
055     *
056     * @return a negative value.
057     */
058    @Override
059    public int getNumActive() {
060        return -1;
061    }
062
063    /**
064     * Not supported in this base implementation.
065     *
066     * @throws UnsupportedOperationException if the pool does not implement this
067     *          method
068     */
069    @Override
070    public void clear() throws Exception, UnsupportedOperationException {
071        throw new UnsupportedOperationException();
072    }
073
074    /**
075     * Not supported in this base implementation. Subclasses should override
076     * this behavior.
077     *
078     * @throws UnsupportedOperationException if the pool does not implement this
079     *          method
080     */
081    @Override
082    public void addObject() throws Exception, UnsupportedOperationException {
083        throw new UnsupportedOperationException();
084    }
085
086    /**
087     * {@inheritDoc}
088     * <p>
089     * This affects the behavior of <code>isClosed</code> and
090     * <code>assertOpen</code>.
091     */
092    @Override
093    public void close() {
094        closed = true;
095    }
096
097    /**
098     * Has this pool instance been closed.
099     *
100     * @return <code>true</code> when this pool has been closed.
101     */
102    public final boolean isClosed() {
103        return closed;
104    }
105
106    /**
107     * Throws an <code>IllegalStateException</code> when this pool has been
108     * closed.
109     *
110     * @throws IllegalStateException when this pool has been closed.
111     *
112     * @see #isClosed()
113     */
114    protected final void assertOpen() throws IllegalStateException {
115        if (isClosed()) {
116            throw new IllegalStateException("Pool not open");
117        }
118    }
119
120    private volatile boolean closed = false;
121}