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 module hunt.pool.BaseObjectPool;
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  *
26  * @param <T> Type of element pooled in this pool.
27  *
28  */
29 abstract class BaseObjectPool(T) : BaseObject, ObjectPool!(T) {
30 
31     override
32     abstract T borrowObject();
33 
34     override
35     abstract void returnObject(T obj);
36 
37     override
38     abstract void invalidateObject(T obj);
39 
40     /**
41      * Not supported in this base implementation.
42      *
43      * @return a negative value.
44      */
45     override
46     int getNumIdle() {
47         return -1;
48     }
49 
50     /**
51      * Not supported in this base implementation.
52      *
53      * @return a negative value.
54      */
55     override
56     int getNumActive() {
57         return -1;
58     }
59 
60     /**
61      * Not supported in this base implementation.
62      *
63      * @throws UnsupportedOperationException if the pool does not implement this
64      *          method
65      */
66     override
67     void clear(){
68         throw new UnsupportedOperationException();
69     }
70 
71     /**
72      * Not supported in this base implementation. Subclasses should override
73      * this behavior.
74      *
75      * @throws UnsupportedOperationException if the pool does not implement this
76      *          method
77      */
78     override
79     void addObject(){
80         throw new UnsupportedOperationException();
81     }
82 
83     /**
84      * {@inheritDoc}
85      * <p>
86      * This affects the behavior of <code>isClosed</code> and
87      * <code>assertOpen</code>.
88      * </p>
89      */
90     override
91     void close() {
92         closed = true;
93     }
94 
95     /**
96      * Has this pool instance been closed.
97      *
98      * @return <code>true</code> when this pool has been closed.
99      */
100     bool isClosed() {
101         return closed;
102     }
103 
104     /**
105      *<code>IllegalStateException</code> when this pool has been
106      * closed.
107      *
108      * @throws IllegalStateException when this pool has been closed.
109      *
110      * @see #isClosed()
111      */
112     protected void assertOpen(){
113         if (isClosed()) {
114             throw new IllegalStateException("Pool not open");
115         }
116     }
117 
118     private bool closed = false;
119 
120     override
121     protected void toStringAppendFields(StringBuilder builder) {
122         builder.append("closed=");
123         builder.append(closed);
124     }
125 }