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.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 private int minIdlePerKey = DEFAULT_MIN_IDLE_PER_KEY;
62
63 private int maxIdlePerKey = DEFAULT_MAX_IDLE_PER_KEY;
64
65 private int maxTotalPerKey = DEFAULT_MAX_TOTAL_PER_KEY;
66
67 private int maxTotal = DEFAULT_MAX_TOTAL;
68
69 /**
70 * Constructs a new configuration with default settings.
71 */
72 public GenericKeyedObjectPoolConfig() {
73 }
74
75 @SuppressWarnings("unchecked")
76 @Override
77 public GenericKeyedObjectPoolConfig<T> clone() {
78 try {
79 return (GenericKeyedObjectPoolConfig<T>) super.clone();
80 } catch (final CloneNotSupportedException e) {
81 throw new AssertionError(); // Can't happen
82 }
83 }
84
85 /**
86 * Gets the value for the {@code maxIdlePerKey} configuration attribute
87 * for pools created with this configuration instance.
88 *
89 * @return The current setting of {@code maxIdlePerKey} for this
90 * configuration instance
91 *
92 * @see GenericKeyedObjectPool#getMaxIdlePerKey()
93 */
94 public int getMaxIdlePerKey() {
95 return maxIdlePerKey;
96 }
97
98 /**
99 * Gets the value for the {@code maxTotal} configuration attribute
100 * for pools created with this configuration instance.
101 *
102 * @return The current setting of {@code maxTotal} for this
103 * configuration instance
104 *
105 * @see GenericKeyedObjectPool#getMaxTotal()
106 */
107 public int getMaxTotal() {
108 return maxTotal;
109 }
110
111 /**
112 * Gets the value for the {@code maxTotalPerKey} configuration attribute
113 * for pools created with this configuration instance.
114 *
115 * @return The current setting of {@code maxTotalPerKey} for this
116 * configuration instance
117 *
118 * @see GenericKeyedObjectPool#getMaxTotalPerKey()
119 */
120 public int getMaxTotalPerKey() {
121 return maxTotalPerKey;
122 }
123
124 /**
125 * Gets the value for the {@code minIdlePerKey} configuration attribute
126 * for pools created with this configuration instance.
127 *
128 * @return The current setting of {@code minIdlePerKey} for this
129 * configuration instance
130 *
131 * @see GenericKeyedObjectPool#getMinIdlePerKey()
132 */
133 public int getMinIdlePerKey() {
134 return minIdlePerKey;
135 }
136
137 /**
138 * Sets the value for the {@code maxIdlePerKey} configuration attribute for
139 * pools created with this configuration instance.
140 *
141 * @param maxIdlePerKey The new setting of {@code maxIdlePerKey}
142 * for this configuration instance
143 *
144 * @see GenericKeyedObjectPool#setMaxIdlePerKey(int)
145 */
146 public void setMaxIdlePerKey(final int maxIdlePerKey) {
147 this.maxIdlePerKey = maxIdlePerKey;
148 }
149
150 /**
151 * Sets the value for the {@code maxTotal} configuration attribute for
152 * pools created with this configuration instance.
153 *
154 * @param maxTotal The new setting of {@code maxTotal}
155 * for this configuration instance
156 *
157 * @see GenericKeyedObjectPool#setMaxTotal(int)
158 */
159 public void setMaxTotal(final int maxTotal) {
160 this.maxTotal = maxTotal;
161 }
162
163 /**
164 * Sets the value for the {@code maxTotalPerKey} configuration attribute for
165 * pools created with this configuration instance.
166 *
167 * @param maxTotalPerKey The new setting of {@code maxTotalPerKey}
168 * for this configuration instance
169 *
170 * @see GenericKeyedObjectPool#setMaxTotalPerKey(int)
171 */
172 public void setMaxTotalPerKey(final int maxTotalPerKey) {
173 this.maxTotalPerKey = maxTotalPerKey;
174 }
175
176 /**
177 * Sets the value for the {@code minIdlePerKey} configuration attribute for
178 * pools created with this configuration instance.
179 *
180 * @param minIdlePerKey The new setting of {@code minIdlePerKey}
181 * for this configuration instance
182 *
183 * @see GenericKeyedObjectPool#setMinIdlePerKey(int)
184 */
185 public void setMinIdlePerKey(final int minIdlePerKey) {
186 this.minIdlePerKey = minIdlePerKey;
187 }
188
189 @Override
190 protected void toStringAppendFields(final 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 }