    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 * </p>
    026 *
    027 * @param <T> Type of element pooled in this pool.
    028 *
    029 * @since 2.0
    030 */
    031public abstract class BaseObjectPool<T> extends BaseObject implements ObjectPool<T> {
    032
    033    @Override
    034    public abstract T borrowObject() throws Exception;
    035
    036    @Override
    037    public abstract void returnObject(T obj) throws Exception;
    038
    039    @Override
    040    public abstract void invalidateObject(T obj) throws Exception;
    041
    042    /**
    043     * Not supported in this base implementation.
    044     *
    045     * @return a negative value.
    046     */
    047    @Override
    048    public int getNumIdle() {
    049        return -1;
    050    }
    051
    052    /**
    053     * Not supported in this base implementation.
    054     *
    055     * @return a negative value.
    056     */
    057    @Override
    058    public int getNumActive() {
    059        return -1;
    060    }
    061
    062    /**
    063     * Not supported in this base implementation.
    064     *
    065     * @throws UnsupportedOperationException if the pool does not implement this
    066     *          method
    067     */
    068    @Override
    069    public void clear() throws Exception, UnsupportedOperationException {
    070        throw new UnsupportedOperationException();
    071    }
    072
    073    /**
    074     * Not supported in this base implementation. Subclasses should override
    075     * this behavior.
    076     *
    077     * @throws UnsupportedOperationException if the pool does not implement this
    078     *          method
    079     */
    080    @Override
    081    public void addObject() throws Exception, UnsupportedOperationException {
    082        throw new UnsupportedOperationException();
    083    }
    084
    085    /**
    086     * {@inheritDoc}
    087     * <p>
    088     * This affects the behavior of {@code isClosed} and
    089     * {@code assertOpen}.
    090     * </p>
    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} when this pool has been closed.
    101     */
    102    public final boolean isClosed() {
    103        return closed;
    104    }
    105
    106    /**
    107     * Throws an {@code IllegalStateException} 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
    122    @Override
    123    protected void toStringAppendFields(final StringBuilder builder) {
    124        builder.append("closed=");
    125        builder.append(closed);
    126    }
    127}