View Javadoc
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  /**
20   * A simple base implementation of {@link ObjectPool}.
21   * Optional operations are implemented to either do nothing, return a value
22   * indicating it is unsupported or throw {@link UnsupportedOperationException}.
23   * <p>
24   * This class is intended to be thread-safe.
25   * </p>
26   *
27   * @param <T> Type of element pooled in this pool.
28   * 
29   *
30   * @since 2.0
31   */
32  public abstract class BaseObjectPool<T> extends BaseObject implements ObjectPool<T> {
33  
34      private volatile boolean closed;
35  
36      /**
37       * Not supported in this base implementation. Subclasses should override
38       * this behavior.
39       *
40       * @throws UnsupportedOperationException if the pool does not implement this
41       *          method
42       */
43      @Override
44      public void addObject() throws Exception {
45          throw new UnsupportedOperationException();
46      }
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       *
54       * @see #isClosed()
55       */
56      protected final void assertOpen() throws IllegalStateException {
57          if (isClosed()) {
58              throw new IllegalStateException("Pool not open");
59          }
60      }
61  
62      @Override
63      public abstract T borrowObject() throws Exception;
64  
65      /**
66       * Not supported in this base implementation.
67       *
68       * @throws UnsupportedOperationException if the pool does not implement this
69       *          method
70       */
71      @Override
72      public void clear() throws Exception {
73          throw new UnsupportedOperationException();
74      }
75  
76      /**
77       * {@inheritDoc}
78       * <p>
79       * This affects the behavior of {@code isClosed} and
80       * {@code assertOpen}.
81       * </p>
82       */
83      @Override
84      public void close() {
85          closed = true;
86      }
87  
88      /**
89       * Not supported in this base implementation.
90       *
91       * @return a negative value.
92       */
93      @Override
94      public int getNumActive() {
95          return -1;
96      }
97  
98      /**
99       * 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 }