    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    private volatile boolean closed;
    034
    035    /**
    036     * Not supported in this base implementation. Subclasses should override
    037     * this behavior.
    038     *
    039     * @throws UnsupportedOperationException if the pool does not implement this
    040     *          method
    041     */
    042    @Override
    043    public void addObject() throws Exception, UnsupportedOperationException {
    044        throw new UnsupportedOperationException();
    045    }
    046
    047    /**
    048     * Throws an {@code IllegalStateException} when this pool has been
    049     * closed.
    050     *
    051     * @throws IllegalStateException when this pool has been closed.
    052     *
    053     * @see #isClosed()
    054     */
    055    protected final void assertOpen() throws IllegalStateException {
    056        if (isClosed()) {
    057            throw new IllegalStateException("Pool not open");
    058        }
    059    }
    060
    061    @Override
    062    public abstract T borrowObject() throws Exception;
    063
    064    /**
    065     * Not supported in this base implementation.
    066     *
    067     * @throws UnsupportedOperationException if the pool does not implement this
    068     *          method
    069     */
    070    @Override
    071    public void clear() throws Exception, UnsupportedOperationException {
    072        throw new UnsupportedOperationException();
    073    }
    074
    075    /**
    076     * {@inheritDoc}
    077     * <p>
    078     * This affects the behavior of {@code isClosed} and
    079     * {@code assertOpen}.
    080     * </p>
    081     */
    082    @Override
    083    public void close() {
    084        closed = true;
    085    }
    086
    087    /**
    088     * Not supported in this base implementation.
    089     *
    090     * @return a negative value.
    091     */
    092    @Override
    093    public int getNumActive() {
    094        return -1;
    095    }
    096
    097    /**
    098     * Not supported in this base implementation.
    099     *
    100     * @return a negative value.
    101     */
    102    @Override
    103    public int getNumIdle() {
    104        return -1;
    105    }
    106
    107    @Override
    108    public abstract void invalidateObject(T obj) throws Exception;
    109
    110    /**
    111     * Has this pool instance been closed.
    112     *
    113     * @return {@code true} when this pool has been closed.
    114     */
    115    public final boolean isClosed() {
    116        return closed;
    117    }
    118
    119    @Override
    120    public abstract void returnObject(T obj) throws Exception;
    121
    122    @Override
    123    protected void toStringAppendFields(final StringBuilder builder) {
    124        builder.append("closed=");
    125        builder.append(closed);
    126    }
    127}