EvictionConfig.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. import java.time.Duration;

  19. /**
  20.  * This class is used by pool implementations to pass configuration information
  21.  * to {@link EvictionPolicy} instances. The {@link EvictionPolicy} may also have
  22.  * its own specific configuration attributes.
  23.  * <p>
  24.  * This class is immutable and thread-safe.
  25.  * </p>
  26.  *
  27.  * @since 2.0
  28.  */
  29. public class EvictionConfig {

  30.     private static final Duration MAX_DURATION = Duration.ofMillis(Long.MAX_VALUE);
  31.     private final Duration idleEvictDuration;
  32.     private final Duration idleSoftEvictDuration;
  33.     private final int minIdle;

  34.     /**
  35.      * Creates a new eviction configuration with the specified parameters.
  36.      * Instances are immutable.
  37.      *
  38.      * @param idleEvictDuration Expected to be provided by
  39.      *        {@link BaseGenericObjectPool#getMinEvictableIdleDuration()}
  40.      * @param idleSoftEvictDuration Expected to be provided by
  41.      *        {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()}
  42.      * @param minIdle Expected to be provided by
  43.      *        {@link GenericObjectPool#getMinIdle()} or
  44.      *        {@link GenericKeyedObjectPool#getMinIdlePerKey()}
  45.      * @since 2.10.0
  46.      */
  47.     public EvictionConfig(final Duration idleEvictDuration, final Duration idleSoftEvictDuration, final int minIdle) {
  48.         this.idleEvictDuration = PoolImplUtils.isPositive(idleEvictDuration) ? idleEvictDuration : MAX_DURATION;
  49.         this.idleSoftEvictDuration = PoolImplUtils.isPositive(idleSoftEvictDuration) ? idleSoftEvictDuration : MAX_DURATION;
  50.         this.minIdle = minIdle;
  51.     }

  52.     /**
  53.      * Creates a new eviction configuration with the specified parameters.
  54.      * Instances are immutable.
  55.      *
  56.      * @param poolIdleEvictMillis Expected to be provided by
  57.      *        {@link BaseGenericObjectPool#getMinEvictableIdleDuration()}
  58.      * @param poolIdleSoftEvictMillis Expected to be provided by
  59.      *        {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()}
  60.      * @param minIdle Expected to be provided by
  61.      *        {@link GenericObjectPool#getMinIdle()} or
  62.      *        {@link GenericKeyedObjectPool#getMinIdlePerKey()}
  63.      * @deprecated Use {@link #EvictionConfig(Duration, Duration, int)}.
  64.      */
  65.     @Deprecated
  66.     public EvictionConfig(final long poolIdleEvictMillis, final long poolIdleSoftEvictMillis, final int minIdle) {
  67.         this(Duration.ofMillis(poolIdleEvictMillis), Duration.ofMillis(poolIdleSoftEvictMillis), minIdle);
  68.     }

  69.     /**
  70.      * Gets the {@code idleEvictTime} for this eviction configuration
  71.      * instance.
  72.      * <p>
  73.      * How the evictor behaves based on this value will be determined by the
  74.      * configured {@link EvictionPolicy}.
  75.      * </p>
  76.      *
  77.      * @return The {@code idleEvictTime}.
  78.      * @since 2.11.0
  79.      */
  80.     public Duration getIdleEvictDuration() {
  81.         return idleEvictDuration;
  82.     }

  83.     /**
  84.      * Gets the {@code idleEvictTime} for this eviction configuration
  85.      * instance.
  86.      * <p>
  87.      * How the evictor behaves based on this value will be determined by the
  88.      * configured {@link EvictionPolicy}.
  89.      * </p>
  90.      *
  91.      * @return The {@code idleEvictTime} in milliseconds
  92.      * @deprecated Use {@link #getIdleEvictDuration()}.
  93.      */
  94.     @Deprecated
  95.     public long getIdleEvictTime() {
  96.         return idleEvictDuration.toMillis();
  97.     }

  98.     /**
  99.      * Gets the {@code idleEvictTime} for this eviction configuration
  100.      * instance.
  101.      * <p>
  102.      * How the evictor behaves based on this value will be determined by the
  103.      * configured {@link EvictionPolicy}.
  104.      * </p>
  105.      *
  106.      * @return The {@code idleEvictTime}.
  107.      * @since 2.10.0
  108.      * @deprecated Use {@link #getIdleEvictDuration()}.
  109.      */
  110.     @Deprecated
  111.     public Duration getIdleEvictTimeDuration() {
  112.         return idleEvictDuration;
  113.     }

  114.     /**
  115.      * Gets the {@code idleSoftEvictTime} for this eviction configuration
  116.      * instance.
  117.      * <p>
  118.      * How the evictor behaves based on this value will be determined by the
  119.      * configured {@link EvictionPolicy}.
  120.      * </p>
  121.      *
  122.      * @return The (@code idleSoftEvictTime} in milliseconds
  123.      * @since 2.11.0
  124.      */
  125.     public Duration getIdleSoftEvictDuration() {
  126.         return idleSoftEvictDuration;
  127.     }

  128.     /**
  129.      * Gets the {@code idleSoftEvictTime} for this eviction configuration
  130.      * instance.
  131.      * <p>
  132.      * How the evictor behaves based on this value will be determined by the
  133.      * configured {@link EvictionPolicy}.
  134.      * </p>
  135.      *
  136.      * @return The (@code idleSoftEvictTime} in milliseconds
  137.      * @deprecated Use {@link #getIdleSoftEvictDuration()}.
  138.      */
  139.     @Deprecated
  140.     public long getIdleSoftEvictTime() {
  141.         return idleSoftEvictDuration.toMillis();
  142.     }

  143.     /**
  144.      * Gets the {@code idleSoftEvictTime} for this eviction configuration
  145.      * instance.
  146.      * <p>
  147.      * How the evictor behaves based on this value will be determined by the
  148.      * configured {@link EvictionPolicy}.
  149.      * </p>
  150.      *
  151.      * @return The (@code idleSoftEvictTime} in milliseconds
  152.      * @deprecated Use {@link #getIdleSoftEvictDuration()}.
  153.      */
  154.     @Deprecated
  155.     public Duration getIdleSoftEvictTimeDuration() {
  156.         return idleSoftEvictDuration;
  157.     }

  158.     /**
  159.      * Gets the {@code minIdle} for this eviction configuration instance.
  160.      * <p>
  161.      * How the evictor behaves based on this value will be determined by the
  162.      * configured {@link EvictionPolicy}.
  163.      * </p>
  164.      *
  165.      * @return The {@code minIdle}
  166.      */
  167.     public int getMinIdle() {
  168.         return minIdle;
  169.     }

  170.     /**
  171.      * @since 2.4
  172.      */
  173.     @Override
  174.     public String toString() {
  175.         final StringBuilder builder = new StringBuilder();
  176.         builder.append("EvictionConfig [idleEvictDuration=");
  177.         builder.append(idleEvictDuration);
  178.         builder.append(", idleSoftEvictDuration=");
  179.         builder.append(idleSoftEvictDuration);
  180.         builder.append(", minIdle=");
  181.         builder.append(minIdle);
  182.         builder.append("]");
  183.         return builder.toString();
  184.     }
  185. }