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 * https://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.impl;
18
19 /**
20 * A simple structure encapsulating the configuration for a
21 * {@link GenericKeyedObjectPool}.
22 *
23 * <p>
24 * This class is not thread-safe; it is only intended to be used to provide
25 * attributes used when creating a pool.
26 * </p>
27 *
28 * @param <T> Type of element pooled.
29 * @since 2.0
30 */
31 public class GenericKeyedObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {
32
33 /**
34 * The default value for the {@code maxTotalPerKey} configuration attribute: {@value}.
35 *
36 * @see GenericKeyedObjectPool#getMaxTotalPerKey()
37 */
38 public static final int DEFAULT_MAX_TOTAL_PER_KEY = 8;
39
40 /**
41 * The default value for the {@code maxTotal} configuration attribute: {@value}.
42 *
43 * @see GenericKeyedObjectPool#getMaxTotal()
44 */
45 public static final int DEFAULT_MAX_TOTAL = -1;
46
47 /**
48 * The default value for the {@code minIdlePerKey} configuration attribute: {@value}.
49 *
50 * @see GenericKeyedObjectPool#getMinIdlePerKey()
51 */
52 public static final int DEFAULT_MIN_IDLE_PER_KEY = 0;
53
54 /**
55 * The default value for the {@code maxIdlePerKey} configuration attribute: {@value}.
56 *
57 * @see GenericKeyedObjectPool#getMaxIdlePerKey()
58 */
59 public static final int DEFAULT_MAX_IDLE_PER_KEY = 8;
60
61 /**
62 * The default value for the {@code reuseCapacityOnReturn} configuration attribute: {@value}.
63 *
64 * @see GenericKeyedObjectPool#getReuseCapacityOnReturn()
65 * @since 2.13.0
66 */
67 public static final boolean DEFAULT_REUSE_CAPACITY_ON_RETURN = true;
68
69 /**
70 * The default value for the {@code reuseCapacityOnMaintenance} configuration attribute: {@value}.
71 *
72 * @see GenericKeyedObjectPool#getReuseCapacityOnMaintenance()
73 * @since 2.13.0
74 */
75 public static final boolean DEFAULT_REUSE_CAPACITY_ON_MAINTENANCE = false;
76
77 private int minIdlePerKey = DEFAULT_MIN_IDLE_PER_KEY;
78
79 private int maxIdlePerKey = DEFAULT_MAX_IDLE_PER_KEY;
80
81 private int maxTotalPerKey = DEFAULT_MAX_TOTAL_PER_KEY;
82
83 private int maxTotal = DEFAULT_MAX_TOTAL;
84
85 private boolean reuseCapacityOnReturn = DEFAULT_REUSE_CAPACITY_ON_RETURN;
86
87 private boolean reuseCapacityOnMaintenance = DEFAULT_REUSE_CAPACITY_ON_MAINTENANCE;
88
89 /**
90 * Constructs a new configuration with default settings.
91 */
92 public GenericKeyedObjectPoolConfig() {
93 }
94
95 @SuppressWarnings("unchecked")
96 @Override
97 public GenericKeyedObjectPoolConfig<T> clone() {
98 try {
99 return (GenericKeyedObjectPoolConfig<T>) super.clone();
100 } catch (final CloneNotSupportedException e) {
101 throw new AssertionError(); // Can't happen
102 }
103 }
104
105 /**
106 * Gets the value for the {@code maxIdlePerKey} configuration attribute
107 * for pools created with this configuration instance.
108 *
109 * @return The current setting of {@code maxIdlePerKey} for this
110 * configuration instance
111 *
112 * @see GenericKeyedObjectPool#getMaxIdlePerKey()
113 */
114 public int getMaxIdlePerKey() {
115 return maxIdlePerKey;
116 }
117
118 /**
119 * Gets the value for the {@code maxTotal} configuration attribute
120 * for pools created with this configuration instance.
121 *
122 * @return The current setting of {@code maxTotal} for this
123 * configuration instance
124 *
125 * @see GenericKeyedObjectPool#getMaxTotal()
126 */
127 public int getMaxTotal() {
128 return maxTotal;
129 }
130
131 /**
132 * Gets the value for the {@code maxTotalPerKey} configuration attribute
133 * for pools created with this configuration instance.
134 *
135 * @return The current setting of {@code maxTotalPerKey} for this
136 * configuration instance
137 *
138 * @see GenericKeyedObjectPool#getMaxTotalPerKey()
139 */
140 public int getMaxTotalPerKey() {
141 return maxTotalPerKey;
142 }
143
144 /**
145 * Gets the value for the {@code minIdlePerKey} configuration attribute
146 * for pools created with this configuration instance.
147 *
148 * @return The current setting of {@code minIdlePerKey} for this
149 * configuration instance
150 *
151 * @see GenericKeyedObjectPool#getMinIdlePerKey()
152 */
153 public int getMinIdlePerKey() {
154 return minIdlePerKey;
155 }
156
157 /**
158 * Gets the value for the {@code reuseCapacityOnMaintenance} configuration attribute
159 * for pools created with this configuration instance.
160 *
161 * @return The current setting of {@code reuseCapacityOnMaintenance} for this
162 * configuration instance
163 *
164 * @see GenericKeyedObjectPool#getReuseCapacityOnMaintenance()
165 * @since 2.13.0
166 */
167 public boolean getReuseCapacityOnMaintenance() {
168 return reuseCapacityOnMaintenance;
169 }
170
171 /**
172 * Gets the value for the {@code reuseCapacityOnReturn} configuration attribute
173 * for pools created with this configuration instance.
174 *
175 * @return The current setting of {@code reuseCapacityOnReturn} for this
176 * configuration instance
177 *
178 * @see GenericKeyedObjectPool#getReuseCapacityOnReturn()
179 * @since 2.13.0
180 */
181 public boolean getReuseCapacityOnReturn() {
182 return reuseCapacityOnReturn;
183 }
184
185 /**
186 * Sets the value for the {@code maxIdlePerKey} configuration attribute for
187 * pools created with this configuration instance.
188 *
189 * @param maxIdlePerKey The new setting of {@code maxIdlePerKey}
190 * for this configuration instance
191 *
192 * @see GenericKeyedObjectPool#setMaxIdlePerKey(int)
193 */
194 public void setMaxIdlePerKey(final int maxIdlePerKey) {
195 this.maxIdlePerKey = maxIdlePerKey;
196 }
197
198 /**
199 * Sets the value for the {@code maxTotal} configuration attribute for
200 * pools created with this configuration instance.
201 *
202 * @param maxTotal The new setting of {@code maxTotal}
203 * for this configuration instance
204 *
205 * @see GenericKeyedObjectPool#setMaxTotal(int)
206 */
207 public void setMaxTotal(final int maxTotal) {
208 this.maxTotal = maxTotal;
209 }
210
211 /**
212 * Sets the value for the {@code maxTotalPerKey} configuration attribute for
213 * pools created with this configuration instance.
214 *
215 * @param maxTotalPerKey The new setting of {@code maxTotalPerKey}
216 * for this configuration instance
217 *
218 * @see GenericKeyedObjectPool#setMaxTotalPerKey(int)
219 */
220 public void setMaxTotalPerKey(final int maxTotalPerKey) {
221 this.maxTotalPerKey = maxTotalPerKey;
222 }
223
224 /**
225 * Sets the value for the {@code minIdlePerKey} configuration attribute for
226 * pools created with this configuration instance.
227 *
228 * @param minIdlePerKey The new setting of {@code minIdlePerKey}
229 * for this configuration instance
230 *
231 * @see GenericKeyedObjectPool#setMinIdlePerKey(int)
232 */
233 public void setMinIdlePerKey(final int minIdlePerKey) {
234 this.minIdlePerKey = minIdlePerKey;
235 }
236
237 /**
238 * Sets the value for the {@code reuseCapacityOnMaintenance} configuration attribute for
239 * pools created with this configuration instance.
240 *
241 * @param reuseCapacityOnMaintenance The new setting of {@code reuseCapacityOnMaintenance}
242 * for this configuration instance
243 *
244 * @see GenericKeyedObjectPool#setReuseCapacityOnMaintenance(boolean)
245 * @since 2.13.0
246 */
247 public void setReuseCapacityOnMaintenance(final boolean reuseCapacityOnMaintenance) {
248 this.reuseCapacityOnMaintenance = reuseCapacityOnMaintenance;
249 }
250
251 /**
252 * Sets the value for the {@code reuseCapacityOnReturn} configuration attribute for
253 * pools created with this configuration instance.
254 *
255 * @param reuseCapacityOnReturn The new setting of {@code reuseCapacityOnReturn}
256 * for this configuration instance
257 *
258 * @see GenericKeyedObjectPool#setReuseCapacityOnReturn(boolean)
259 * @since 2.13.0
260 */
261 public void setReuseCapacityOnReturn(final boolean reuseCapacityOnReturn) {
262 this.reuseCapacityOnReturn = reuseCapacityOnReturn;
263 }
264
265 @Override
266 protected void toStringAppendFields(final StringBuilder builder) {
267 super.toStringAppendFields(builder);
268 builder.append(", minIdlePerKey=");
269 builder.append(minIdlePerKey);
270 builder.append(", maxIdlePerKey=");
271 builder.append(maxIdlePerKey);
272 builder.append(", maxTotalPerKey=");
273 builder.append(maxTotalPerKey);
274 builder.append(", maxTotal=");
275 builder.append(maxTotal);
276 builder.append(", reuseCapacityOnReturn=");
277 builder.append(reuseCapacityOnReturn);
278 builder.append(", reuseCapacityOnMaintenance=");
279 builder.append(reuseCapacityOnMaintenance);
280 }
281 }