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.BaseKeyedPooledObjectFactory;
18 
19 /**
20  * A base implementation of <code>KeyedPooledObjectFactory</code>.
21  * <p>
22  * All operations defined here are essentially no-op's.
23  * </p>
24  * This class is immutable, and therefore thread-safe.
25  *
26  * @see KeyedPooledObjectFactory
27  *
28  * @param <K> The type of keys managed by this factory.
29  * @param <V> Type of element managed by this factory.
30  *
31  */
32 abstract class BaseKeyedPooledObjectFactory(K, V) : BaseObject,
33         KeyedPooledObjectFactory!(K, V) {
34 
35     /**
36      * Create an instance that can be served by the pool.
37      *
38      * @param key the key used when constructing the object
39      * @return an instance that can be served by the pool
40      *
41      * @throws Exception if there is a problem creating a new instance,
42      *    this will be propagated to the code requesting an object.
43      */
44     abstract V create(K key)
45 ;
46 
47     /**
48      * Wrap the provided instance with an implementation of
49      * {@link PooledObject}.
50      *
51      * @param value the instance to wrap
52      *
53      * @return The provided instance, wrapped by a {@link PooledObject}
54      */
55     abstract PooledObject!(V) wrap(V value);
56 
57     override
58     PooledObject!(V) makeObject(K key){
59         return wrap(create(key));
60     }
61 
62     /**
63      * Destroy an instance no longer needed by the pool.
64      * <p>
65      * The default implementation is a no-op.
66      * </p>
67      *
68      * @param key the key used when selecting the instance
69      * @param p a {@code PooledObject} wrapping the instance to be destroyed
70      */
71     override
72     void destroyObject(K key, PooledObject!(V) p)
73 {
74         // The default implementation is a no-op.
75     }
76 
77     /**
78      * Ensures that the instance is safe to be returned by the pool.
79      * <p>
80      * The default implementation always returns {@code true}.
81      * </p>
82      *
83      * @param key the key used when selecting the object
84      * @param p a {@code PooledObject} wrapping the instance to be validated
85      * @return always <code>true</code> in the default implementation
86      */
87     override
88     bool validateObject(K key, PooledObject!(V) p) {
89         return true;
90     }
91 
92     /**
93      * Reinitialize an instance to be returned by the pool.
94      * <p>
95      * The default implementation is a no-op.
96      * </p>
97      *
98      * @param key the key used when selecting the object
99      * @param p a {@code PooledObject} wrapping the instance to be activated
100      */
101     override
102     void activateObject(K key, PooledObject!(V) p)
103 {
104         // The default implementation is a no-op.
105     }
106 
107     /**
108      * Uninitialize an instance to be returned to the idle object pool.
109      * <p>
110      * The default implementation is a no-op.
111      * </p>
112      *
113      * @param key the key used when selecting the object
114      * @param p a {@code PooledObject} wrapping the instance to be passivated
115      */
116     override
117     void passivateObject(K key, PooledObject!(V) p)
118 {
119         // The default implementation is a no-op.
120     }
121 }