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