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 * @since 2.0
29 */
30 public abstract class BaseObjectPool<T> extends BaseObject implements ObjectPool<T> {
31
32 private volatile boolean closed;
33
34 /**
35 * Constructs a new instance.
36 */
37 public BaseObjectPool() {
38 // empty
39 }
40
41 /**
42 * Not supported in this base implementation. Subclasses should override
43 * this behavior.
44 *
45 * @throws UnsupportedOperationException if the pool does not implement this
46 * method
47 */
48 @Override
49 public void addObject() throws Exception {
50 throw new UnsupportedOperationException();
51 }
52
53 /**
54 * Throws an {@code IllegalStateException} when this pool has been
55 * closed.
56 *
57 * @throws IllegalStateException when this pool has been closed.
58 * @see #isClosed()
59 */
60 protected final void assertOpen() throws IllegalStateException {
61 if (isClosed()) {
62 throw new IllegalStateException("Pool not open");
63 }
64 }
65
66 @Override
67 public abstract T borrowObject() throws Exception;
68
69 /**
70 * Not supported in this base implementation.
71 *
72 * @throws UnsupportedOperationException if the pool does not implement this
73 * method
74 */
75 @Override
76 public void clear() throws Exception {
77 throw new UnsupportedOperationException();
78 }
79
80 /**
81 * {@inheritDoc}
82 * <p>
83 * This affects the behavior of {@code isClosed} and
84 * {@code assertOpen}.
85 * </p>
86 */
87 @Override
88 public void close() {
89 closed = true;
90 }
91
92 /**
93 * Not supported in this base implementation.
94 *
95 * @return a negative value.
96 */
97 @Override
98 public int getNumActive() {
99 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 }