GenericKeyedObjectPoolConfig.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 GenericKeyedObjectPool}.
  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 GenericKeyedObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {

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

  37.     /**
  38.      * The default value for the {@code maxTotal} configuration attribute: {@value}.
  39.      *
  40.      * @see GenericKeyedObjectPool#getMaxTotal()
  41.      */
  42.     public static final int DEFAULT_MAX_TOTAL = -1;

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

  49.     /**
  50.      * The default value for the {@code maxIdlePerKey} configuration attribute: {@value}.
  51.      *
  52.      * @see GenericKeyedObjectPool#getMaxIdlePerKey()
  53.      */
  54.     public static final int DEFAULT_MAX_IDLE_PER_KEY = 8;

  55.     private int minIdlePerKey = DEFAULT_MIN_IDLE_PER_KEY;

  56.     private int maxIdlePerKey = DEFAULT_MAX_IDLE_PER_KEY;

  57.     private int maxTotalPerKey = DEFAULT_MAX_TOTAL_PER_KEY;

  58.     private int maxTotal = DEFAULT_MAX_TOTAL;

  59.     /**
  60.      * Constructs a new configuration with default settings.
  61.      */
  62.     public GenericKeyedObjectPoolConfig() {
  63.     }

  64.     @SuppressWarnings("unchecked")
  65.     @Override
  66.     public GenericKeyedObjectPoolConfig<T> clone() {
  67.         try {
  68.             return (GenericKeyedObjectPoolConfig<T>) super.clone();
  69.         } catch (final CloneNotSupportedException e) {
  70.             throw new AssertionError(); // Can't happen
  71.         }
  72.     }

  73.     /**
  74.      * Gets the value for the {@code maxIdlePerKey} configuration attribute
  75.      * for pools created with this configuration instance.
  76.      *
  77.      * @return  The current setting of {@code maxIdlePerKey} for this
  78.      *          configuration instance
  79.      *
  80.      * @see GenericKeyedObjectPool#getMaxIdlePerKey()
  81.      */
  82.     public int getMaxIdlePerKey() {
  83.         return maxIdlePerKey;
  84.     }

  85.     /**
  86.      * Gets the value for the {@code maxTotal} configuration attribute
  87.      * for pools created with this configuration instance.
  88.      *
  89.      * @return  The current setting of {@code maxTotal} for this
  90.      *          configuration instance
  91.      *
  92.      * @see GenericKeyedObjectPool#getMaxTotal()
  93.      */
  94.     public int getMaxTotal() {
  95.         return maxTotal;
  96.     }

  97.     /**
  98.      * Gets the value for the {@code maxTotalPerKey} configuration attribute
  99.      * for pools created with this configuration instance.
  100.      *
  101.      * @return  The current setting of {@code maxTotalPerKey} for this
  102.      *          configuration instance
  103.      *
  104.      * @see GenericKeyedObjectPool#getMaxTotalPerKey()
  105.      */
  106.     public int getMaxTotalPerKey() {
  107.         return maxTotalPerKey;
  108.     }

  109.     /**
  110.      * Gets the value for the {@code minIdlePerKey} configuration attribute
  111.      * for pools created with this configuration instance.
  112.      *
  113.      * @return  The current setting of {@code minIdlePerKey} for this
  114.      *          configuration instance
  115.      *
  116.      * @see GenericKeyedObjectPool#getMinIdlePerKey()
  117.      */
  118.     public int getMinIdlePerKey() {
  119.         return minIdlePerKey;
  120.     }

  121.     /**
  122.      * Sets the value for the {@code maxIdlePerKey} configuration attribute for
  123.      * pools created with this configuration instance.
  124.      *
  125.      * @param maxIdlePerKey The new setting of {@code maxIdlePerKey}
  126.      *        for this configuration instance
  127.      *
  128.      * @see GenericKeyedObjectPool#setMaxIdlePerKey(int)
  129.      */
  130.     public void setMaxIdlePerKey(final int maxIdlePerKey) {
  131.         this.maxIdlePerKey = maxIdlePerKey;
  132.     }

  133.     /**
  134.      * Sets the value for the {@code maxTotal} configuration attribute for
  135.      * pools created with this configuration instance.
  136.      *
  137.      * @param maxTotal The new setting of {@code maxTotal}
  138.      *        for this configuration instance
  139.      *
  140.      * @see GenericKeyedObjectPool#setMaxTotal(int)
  141.      */
  142.     public void setMaxTotal(final int maxTotal) {
  143.         this.maxTotal = maxTotal;
  144.     }

  145.     /**
  146.      * Sets the value for the {@code maxTotalPerKey} configuration attribute for
  147.      * pools created with this configuration instance.
  148.      *
  149.      * @param maxTotalPerKey The new setting of {@code maxTotalPerKey}
  150.      *        for this configuration instance
  151.      *
  152.      * @see GenericKeyedObjectPool#setMaxTotalPerKey(int)
  153.      */
  154.     public void setMaxTotalPerKey(final int maxTotalPerKey) {
  155.         this.maxTotalPerKey = maxTotalPerKey;
  156.     }

  157.     /**
  158.      * Sets the value for the {@code minIdlePerKey} configuration attribute for
  159.      * pools created with this configuration instance.
  160.      *
  161.      * @param minIdlePerKey The new setting of {@code minIdlePerKey}
  162.      *        for this configuration instance
  163.      *
  164.      * @see GenericKeyedObjectPool#setMinIdlePerKey(int)
  165.      */
  166.     public void setMinIdlePerKey(final int minIdlePerKey) {
  167.         this.minIdlePerKey = minIdlePerKey;
  168.     }

  169.     @Override
  170.     protected void toStringAppendFields(final StringBuilder builder) {
  171.         super.toStringAppendFields(builder);
  172.         builder.append(", minIdlePerKey=");
  173.         builder.append(minIdlePerKey);
  174.         builder.append(", maxIdlePerKey=");
  175.         builder.append(maxIdlePerKey);
  176.         builder.append(", maxTotalPerKey=");
  177.         builder.append(maxTotalPerKey);
  178.         builder.append(", maxTotal=");
  179.         builder.append(maxTotal);
  180.     }
  181. }