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