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