001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.pool2.impl;
018
019/**
020 * A simple structure encapsulating the configuration for a
021 * {@link GenericKeyedObjectPool}.
022 *
023 * <p>
024 * This class is not thread-safe; it is only intended to be used to provide
025 * attributes used when creating a pool.
026 * </p>
027 *
028 * @param <T> Type of element pooled.
029 * @since 2.0
030 */
031public class GenericKeyedObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {
032
033    /**
034     * The default value for the {@code maxTotalPerKey} configuration attribute: {@value}.
035     *
036     * @see GenericKeyedObjectPool#getMaxTotalPerKey()
037     */
038    public static final int DEFAULT_MAX_TOTAL_PER_KEY = 8;
039
040    /**
041     * The default value for the {@code maxTotal} configuration attribute: {@value}.
042     *
043     * @see GenericKeyedObjectPool#getMaxTotal()
044     */
045    public static final int DEFAULT_MAX_TOTAL = -1;
046
047    /**
048     * The default value for the {@code minIdlePerKey} configuration attribute: {@value}.
049     *
050     * @see GenericKeyedObjectPool#getMinIdlePerKey()
051     */
052    public static final int DEFAULT_MIN_IDLE_PER_KEY = 0;
053
054    /**
055     * The default value for the {@code maxIdlePerKey} configuration attribute: {@value}.
056     *
057     * @see GenericKeyedObjectPool#getMaxIdlePerKey()
058     */
059    public static final int DEFAULT_MAX_IDLE_PER_KEY = 8;
060
061    /**
062     * The default value for the {@code reuseCapacityOnReturn} configuration attribute: {@value}.
063     *
064     * @see GenericKeyedObjectPool#getReuseCapacityOnReturn()
065     * @since 2.13.0
066     */
067    public static final boolean DEFAULT_REUSE_CAPACITY_ON_RETURN = true;
068
069    /**
070     * The default value for the {@code reuseCapacityOnMaintenance} configuration attribute: {@value}.
071     *
072     * @see GenericKeyedObjectPool#getReuseCapacityOnMaintenance()
073     * @since 2.13.0
074     */
075    public static final boolean DEFAULT_REUSE_CAPACITY_ON_MAINTENANCE = false;
076
077    private int minIdlePerKey = DEFAULT_MIN_IDLE_PER_KEY;
078
079    private int maxIdlePerKey = DEFAULT_MAX_IDLE_PER_KEY;
080
081    private int maxTotalPerKey = DEFAULT_MAX_TOTAL_PER_KEY;
082
083    private int maxTotal = DEFAULT_MAX_TOTAL;
084
085    private boolean reuseCapacityOnReturn = DEFAULT_REUSE_CAPACITY_ON_RETURN;
086
087    private boolean reuseCapacityOnMaintenance = DEFAULT_REUSE_CAPACITY_ON_MAINTENANCE;
088
089    /**
090     * Constructs a new configuration with default settings.
091     */
092    public GenericKeyedObjectPoolConfig() {
093    }
094
095    @SuppressWarnings("unchecked")
096    @Override
097    public GenericKeyedObjectPoolConfig<T> clone() {
098        try {
099            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}