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.impl.GenericKeyedObjectPoolConfig;
18 
19 import hunt.pool.impl.BaseObjectPoolConfig;
20 
21 import hunt.Exceptions;
22 import hunt.util.StringBuilder;
23 
24 /**
25  * A simple "struct" encapsulating the configuration for a
26  * {@link GenericKeyedObjectPool}.
27  *
28  * <p>
29  * This class is not thread-safe; it is only intended to be used to provide
30  * attributes used when creating a pool.
31  * </p>
32  *
33  * @param <T> Type of element pooled.
34  */
35 class GenericKeyedObjectPoolConfig : BaseObjectPoolConfig {
36 
37     /**
38      * The default value for the {@code maxTotalPerKey} configuration attribute.
39      * @see GenericKeyedObjectPool#getMaxTotalPerKey()
40      */
41     enum int DEFAULT_MAX_TOTAL_PER_KEY = 8;
42 
43     /**
44      * The default value for the {@code maxTotal} configuration attribute.
45      * @see GenericKeyedObjectPool#getMaxTotal()
46      */
47     enum int DEFAULT_MAX_TOTAL = -1;
48 
49     /**
50      * The default value for the {@code minIdlePerKey} configuration attribute.
51      * @see GenericKeyedObjectPool#getMinIdlePerKey()
52      */
53     enum int DEFAULT_MIN_IDLE_PER_KEY = 0;
54 
55     /**
56      * The default value for the {@code maxIdlePerKey} configuration attribute.
57      * @see GenericKeyedObjectPool#getMaxIdlePerKey()
58      */
59     enum int DEFAULT_MAX_IDLE_PER_KEY = 8;
60 
61 
62     private int minIdlePerKey = DEFAULT_MIN_IDLE_PER_KEY;
63 
64     private int maxIdlePerKey = DEFAULT_MAX_IDLE_PER_KEY;
65 
66     private int maxTotalPerKey = DEFAULT_MAX_TOTAL_PER_KEY;
67 
68     private int maxTotal = DEFAULT_MAX_TOTAL;
69 
70     /**
71      * Create a new configuration with default settings.
72      */
73     this() {
74     }
75 
76     /**
77      * Get the value for the {@code maxTotal} configuration attribute
78      * for pools created with this configuration instance.
79      *
80      * @return  The current setting of {@code maxTotal} for this
81      *          configuration instance
82      *
83      * @see GenericKeyedObjectPool#getMaxTotal()
84      */
85     int getMaxTotal() {
86         return maxTotal;
87     }
88 
89     /**
90      * Set the value for the {@code maxTotal} configuration attribute for
91      * pools created with this configuration instance.
92      *
93      * @param maxTotal The new setting of {@code maxTotal}
94      *        for this configuration instance
95      *
96      * @see GenericKeyedObjectPool#setMaxTotal(int)
97      */
98     void setMaxTotal(int maxTotal) {
99         this.maxTotal = maxTotal;
100     }
101 
102     /**
103      * Get the value for the {@code maxTotalPerKey} configuration attribute
104      * for pools created with this configuration instance.
105      *
106      * @return  The current setting of {@code maxTotalPerKey} for this
107      *          configuration instance
108      *
109      * @see GenericKeyedObjectPool#getMaxTotalPerKey()
110      */
111     int getMaxTotalPerKey() {
112         return maxTotalPerKey;
113     }
114 
115     /**
116      * Set the value for the {@code maxTotalPerKey} configuration attribute for
117      * pools created with this configuration instance.
118      *
119      * @param maxTotalPerKey The new setting of {@code maxTotalPerKey}
120      *        for this configuration instance
121      *
122      * @see GenericKeyedObjectPool#setMaxTotalPerKey(int)
123      */
124     void setMaxTotalPerKey(int maxTotalPerKey) {
125         this.maxTotalPerKey = maxTotalPerKey;
126     }
127 
128     /**
129      * Get the value for the {@code minIdlePerKey} configuration attribute
130      * for pools created with this configuration instance.
131      *
132      * @return  The current setting of {@code minIdlePerKey} for this
133      *          configuration instance
134      *
135      * @see GenericKeyedObjectPool#getMinIdlePerKey()
136      */
137     int getMinIdlePerKey() {
138         return minIdlePerKey;
139     }
140 
141     /**
142      * Set the value for the {@code minIdlePerKey} configuration attribute for
143      * pools created with this configuration instance.
144      *
145      * @param minIdlePerKey The new setting of {@code minIdlePerKey}
146      *        for this configuration instance
147      *
148      * @see GenericKeyedObjectPool#setMinIdlePerKey(int)
149      */
150     void setMinIdlePerKey(int minIdlePerKey) {
151         this.minIdlePerKey = minIdlePerKey;
152     }
153 
154     /**
155      * Get the value for the {@code maxIdlePerKey} configuration attribute
156      * for pools created with this configuration instance.
157      *
158      * @return  The current setting of {@code maxIdlePerKey} for this
159      *          configuration instance
160      *
161      * @see GenericKeyedObjectPool#getMaxIdlePerKey()
162      */
163     int getMaxIdlePerKey() {
164         return maxIdlePerKey;
165     }
166 
167     /**
168      * Set the value for the {@code maxIdlePerKey} configuration attribute for
169      * pools created with this configuration instance.
170      *
171      * @param maxIdlePerKey The new setting of {@code maxIdlePerKey}
172      *        for this configuration instance
173      *
174      * @see GenericKeyedObjectPool#setMaxIdlePerKey(int)
175      */
176     void setMaxIdlePerKey(int maxIdlePerKey) {
177         this.maxIdlePerKey = maxIdlePerKey;
178     }
179 
180     override
181     GenericKeyedObjectPoolConfig clone() {
182         try {
183             return cast(GenericKeyedObjectPoolConfig) super.clone();
184         } catch (CloneNotSupportedException e) {
185             throw new AssertionError(); // Can't happen
186         }
187     }
188 
189     override
190     protected void toStringAppendFields(StringBuilder builder) {
191         super.toStringAppendFields(builder);
192         builder.append(", minIdlePerKey=");
193         builder.append(minIdlePerKey);
194         builder.append(", maxIdlePerKey=");
195         builder.append(maxIdlePerKey);
196         builder.append(", maxTotalPerKey=");
197         builder.append(maxTotalPerKey);
198         builder.append(", maxTotal=");
199         builder.append(maxTotal);
200     }
201 }