BaseObjectPool.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.pool2;

  18. /**
  19.  * A simple base implementation of {@link ObjectPool}.
  20.  * Optional operations are implemented to either do nothing, return a value
  21.  * indicating it is unsupported or throw {@link UnsupportedOperationException}.
  22.  * <p>
  23.  * This class is intended to be thread-safe.
  24.  * </p>
  25.  *
  26.  * @param <T> Type of element pooled in this pool.
  27.  * @since 2.0
  28.  */
  29. public abstract class BaseObjectPool<T> extends BaseObject implements ObjectPool<T> {

  30.     private volatile boolean closed;

  31.     /**
  32.      * Constructs a new instance.
  33.      */
  34.     public BaseObjectPool() {
  35.         // empty
  36.     }

  37.     /**
  38.      * Not supported in this base implementation. Subclasses should override
  39.      * this behavior.
  40.      *
  41.      * @throws UnsupportedOperationException if the pool does not implement this
  42.      *          method
  43.      */
  44.     @Override
  45.     public void addObject() throws Exception {
  46.         throw new UnsupportedOperationException();
  47.     }

  48.     /**
  49.      * Throws an {@code IllegalStateException} when this pool has been
  50.      * closed.
  51.      *
  52.      * @throws IllegalStateException when this pool has been closed.
  53.      * @see #isClosed()
  54.      */
  55.     protected final void assertOpen() throws IllegalStateException {
  56.         if (isClosed()) {
  57.             throw new IllegalStateException("Pool not open");
  58.         }
  59.     }

  60.     @Override
  61.     public abstract T borrowObject() throws Exception;

  62.     /**
  63.      * Not supported in this base implementation.
  64.      *
  65.      * @throws UnsupportedOperationException if the pool does not implement this
  66.      *          method
  67.      */
  68.     @Override
  69.     public void clear() throws Exception {
  70.         throw new UnsupportedOperationException();
  71.     }

  72.     /**
  73.      * {@inheritDoc}
  74.      * <p>
  75.      * This affects the behavior of {@code isClosed} and
  76.      * {@code assertOpen}.
  77.      * </p>
  78.      */
  79.     @Override
  80.     public void close() {
  81.         closed = true;
  82.     }

  83.     /**
  84.      * Not supported in this base implementation.
  85.      *
  86.      * @return a negative value.
  87.      */
  88.     @Override
  89.     public int getNumActive() {
  90.         return -1;
  91.     }

  92.     /**
  93.      * Not supported in this base implementation.
  94.      *
  95.      * @return a negative value.
  96.      */
  97.     @Override
  98.     public int getNumIdle() {
  99.         return -1;
  100.     }

  101.     @Override
  102.     public abstract void invalidateObject(T obj) throws Exception;

  103.     /**
  104.      * Has this pool instance been closed.
  105.      *
  106.      * @return {@code true} when this pool has been closed.
  107.      */
  108.     public final boolean isClosed() {
  109.         return closed;
  110.     }

  111.     @Override
  112.     public abstract void returnObject(T obj) throws Exception;

  113.     @Override
  114.     protected void toStringAppendFields(final StringBuilder builder) {
  115.         builder.append("closed=");
  116.         builder.append(closed);
  117.     }
  118. }