GenericObjectPoolConfig.java

  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.  * A simple structure encapsulating the configuration for a
  20.  * {@link GenericObjectPool}.
  21.  *
  22.  * <p>
  23.  * This class is not thread-safe; it is only intended to be used to provide
  24.  * attributes used when creating a pool.
  25.  * </p>
  26.  *
  27.  * @param <T> Type of element pooled.
  28.  * @since 2.0
  29.  */
  30. public class GenericObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {

  31.     /**
  32.      * The default value for the {@code maxTotal} configuration attribute: {@value}.
  33.      *
  34.      * @see GenericObjectPool#getMaxTotal()
  35.      */
  36.     public static final int DEFAULT_MAX_TOTAL = 8;

  37.     /**
  38.      * The default value for the {@code maxIdle} configuration attribute: {@value}.
  39.      *
  40.      * @see GenericObjectPool#getMaxIdle()
  41.      */
  42.     public static final int DEFAULT_MAX_IDLE = 8;

  43.     /**
  44.      * The default value for the {@code minIdle} configuration attribute: {@value}.
  45.      *
  46.      * @see GenericObjectPool#getMinIdle()
  47.      */
  48.     public static final int DEFAULT_MIN_IDLE = 0;

  49.     private int maxTotal = DEFAULT_MAX_TOTAL;

  50.     private int maxIdle = DEFAULT_MAX_IDLE;

  51.     private int minIdle = DEFAULT_MIN_IDLE;

  52.     /**
  53.      * Constructs a new instance.
  54.      */
  55.     public GenericObjectPoolConfig() {
  56.         // empty
  57.     }

  58.     @SuppressWarnings("unchecked")
  59.     @Override
  60.     public GenericObjectPoolConfig<T> clone() {
  61.         try {
  62.             return (GenericObjectPoolConfig<T>) super.clone();
  63.         } catch (final CloneNotSupportedException e) {
  64.             throw new AssertionError(); // Can't happen
  65.         }
  66.     }

  67.     /**
  68.      * Gets the value for the {@code maxIdle} configuration attribute
  69.      * for pools created with this configuration instance.
  70.      *
  71.      * @return  The current setting of {@code maxIdle} for this
  72.      *          configuration instance
  73.      *
  74.      * @see GenericObjectPool#getMaxIdle()
  75.      */
  76.     public int getMaxIdle() {
  77.         return maxIdle;
  78.     }

  79.     /**
  80.      * Gets the value for the {@code maxTotal} configuration attribute
  81.      * for pools created with this configuration instance.
  82.      *
  83.      * @return  The current setting of {@code maxTotal} for this
  84.      *          configuration instance
  85.      *
  86.      * @see GenericObjectPool#getMaxTotal()
  87.      */
  88.     public int getMaxTotal() {
  89.         return maxTotal;
  90.     }

  91.     /**
  92.      * Gets the value for the {@code minIdle} configuration attribute
  93.      * for pools created with this configuration instance.
  94.      *
  95.      * @return  The current setting of {@code minIdle} for this
  96.      *          configuration instance
  97.      *
  98.      * @see GenericObjectPool#getMinIdle()
  99.      */
  100.     public int getMinIdle() {
  101.         return minIdle;
  102.     }

  103.     /**
  104.      * Sets the value for the {@code maxIdle} configuration attribute for
  105.      * pools created with this configuration instance.
  106.      *
  107.      * @param maxIdle The new setting of {@code maxIdle}
  108.      *        for this configuration instance
  109.      *
  110.      * @see GenericObjectPool#setMaxIdle(int)
  111.      */
  112.     public void setMaxIdle(final int maxIdle) {
  113.         this.maxIdle = maxIdle;
  114.     }

  115.     /**
  116.      * Sets the value for the {@code maxTotal} configuration attribute for
  117.      * pools created with this configuration instance.
  118.      *
  119.      * @param maxTotal The new setting of {@code maxTotal}
  120.      *        for this configuration instance
  121.      *
  122.      * @see GenericObjectPool#setMaxTotal(int)
  123.      */
  124.     public void setMaxTotal(final int maxTotal) {
  125.         this.maxTotal = maxTotal;
  126.     }

  127.     /**
  128.      * Sets the value for the {@code minIdle} configuration attribute for
  129.      * pools created with this configuration instance.
  130.      *
  131.      * @param minIdle The new setting of {@code minIdle}
  132.      *        for this configuration instance
  133.      *
  134.      * @see GenericObjectPool#setMinIdle(int)
  135.      */
  136.     public void setMinIdle(final int minIdle) {
  137.         this.minIdle = minIdle;
  138.     }

  139.     @Override
  140.     protected void toStringAppendFields(final StringBuilder builder) {
  141.         super.toStringAppendFields(builder);
  142.         builder.append(", maxTotal=");
  143.         builder.append(maxTotal);
  144.         builder.append(", maxIdle=");
  145.         builder.append(maxIdle);
  146.         builder.append(", minIdle=");
  147.         builder.append(minIdle);
  148.     }
  149. }