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