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 GenericObjectPool}.
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 GenericObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {
32
33 /**
34 * The default value for the {@code maxTotal} configuration attribute: {@value}.
35 *
36 * @see GenericObjectPool#getMaxTotal()
37 */
38 public static final int DEFAULT_MAX_TOTAL = 8;
39
40 /**
41 * The default value for the {@code maxIdle} configuration attribute: {@value}.
42 *
43 * @see GenericObjectPool#getMaxIdle()
44 */
45 public static final int DEFAULT_MAX_IDLE = 8;
46
47 /**
48 * The default value for the {@code minIdle} configuration attribute: {@value}.
49 *
50 * @see GenericObjectPool#getMinIdle()
51 */
52 public static final int DEFAULT_MIN_IDLE = 0;
53
54 private int maxTotal = DEFAULT_MAX_TOTAL;
55
56 private int maxIdle = DEFAULT_MAX_IDLE;
57
58 private int minIdle = DEFAULT_MIN_IDLE;
59
60 /**
61 * Constructs a new instance.
62 */
63 public GenericObjectPoolConfig() {
64 // empty
65 }
66
67 @SuppressWarnings("unchecked")
68 @Override
69 public GenericObjectPoolConfig<T> clone() {
70 try {
71 return (GenericObjectPoolConfig<T>) super.clone();
72 } catch (final CloneNotSupportedException e) {
73 throw new AssertionError(); // Can't happen
74 }
75 }
76
77 /**
78 * Gets the value for the {@code maxIdle} configuration attribute
79 * for pools created with this configuration instance.
80 *
81 * @return The current setting of {@code maxIdle} for this
82 * configuration instance
83 *
84 * @see GenericObjectPool#getMaxIdle()
85 */
86 public int getMaxIdle() {
87 return maxIdle;
88 }
89
90 /**
91 * Gets the value for the {@code maxTotal} configuration attribute
92 * for pools created with this configuration instance.
93 *
94 * @return The current setting of {@code maxTotal} for this
95 * configuration instance
96 *
97 * @see GenericObjectPool#getMaxTotal()
98 */
99 public int getMaxTotal() {
100 return maxTotal;
101 }
102
103 /**
104 * Gets the value for the {@code minIdle} configuration attribute
105 * for pools created with this configuration instance.
106 *
107 * @return The current setting of {@code minIdle} for this
108 * configuration instance
109 *
110 * @see GenericObjectPool#getMinIdle()
111 */
112 public int getMinIdle() {
113 return minIdle;
114 }
115
116 /**
117 * Sets the value for the {@code maxIdle} configuration attribute for
118 * pools created with this configuration instance.
119 *
120 * @param maxIdle The new setting of {@code maxIdle}
121 * for this configuration instance
122 *
123 * @see GenericObjectPool#setMaxIdle(int)
124 */
125 public void setMaxIdle(final int maxIdle) {
126 this.maxIdle = maxIdle;
127 }
128
129 /**
130 * Sets the value for the {@code maxTotal} configuration attribute for
131 * pools created with this configuration instance.
132 *
133 * @param maxTotal The new setting of {@code maxTotal}
134 * for this configuration instance
135 *
136 * @see GenericObjectPool#setMaxTotal(int)
137 */
138 public void setMaxTotal(final int maxTotal) {
139 this.maxTotal = maxTotal;
140 }
141
142 /**
143 * Sets the value for the {@code minIdle} configuration attribute for
144 * pools created with this configuration instance.
145 *
146 * @param minIdle The new setting of {@code minIdle}
147 * for this configuration instance
148 *
149 * @see GenericObjectPool#setMinIdle(int)
150 */
151 public void setMinIdle(final int minIdle) {
152 this.minIdle = minIdle;
153 }
154
155 @Override
156 protected void toStringAppendFields(final StringBuilder builder) {
157 super.toStringAppendFields(builder);
158 builder.append(", maxTotal=");
159 builder.append(maxTotal);
160 builder.append(", maxIdle=");
161 builder.append(maxIdle);
162 builder.append(", minIdle=");
163 builder.append(minIdle);
164 }
165 }