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 import java.time.Duration; 20 21 /** 22 * This class is used by pool implementations to pass configuration information 23 * to {@link EvictionPolicy} instances. The {@link EvictionPolicy} may also have 24 * its own specific configuration attributes. 25 * <p> 26 * This class is immutable and thread-safe. 27 * </p> 28 * 29 * @since 2.0 30 */ 31 public class EvictionConfig { 32 33 private static final Duration MAX_DURATION = Duration.ofMillis(Long.MAX_VALUE); 34 private final Duration idleEvictDuration; 35 private final Duration idleSoftEvictDuration; 36 private final int minIdle; 37 38 /** 39 * Creates a new eviction configuration with the specified parameters. 40 * Instances are immutable. 41 * 42 * @param idleEvictDuration Expected to be provided by 43 * {@link BaseGenericObjectPool#getMinEvictableIdleDuration()} 44 * @param idleSoftEvictDuration Expected to be provided by 45 * {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()} 46 * @param minIdle Expected to be provided by 47 * {@link GenericObjectPool#getMinIdle()} or 48 * {@link GenericKeyedObjectPool#getMinIdlePerKey()} 49 * @since 2.10.0 50 */ 51 public EvictionConfig(final Duration idleEvictDuration, final Duration idleSoftEvictDuration, final int minIdle) { 52 this.idleEvictDuration = PoolImplUtils.isPositive(idleEvictDuration) ? idleEvictDuration : MAX_DURATION; 53 this.idleSoftEvictDuration = PoolImplUtils.isPositive(idleSoftEvictDuration) ? idleSoftEvictDuration : MAX_DURATION; 54 this.minIdle = minIdle; 55 } 56 57 /** 58 * Creates a new eviction configuration with the specified parameters. 59 * Instances are immutable. 60 * 61 * @param poolIdleEvictMillis Expected to be provided by 62 * {@link BaseGenericObjectPool#getMinEvictableIdleDuration()} 63 * @param poolIdleSoftEvictMillis Expected to be provided by 64 * {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()} 65 * @param minIdle Expected to be provided by 66 * {@link GenericObjectPool#getMinIdle()} or 67 * {@link GenericKeyedObjectPool#getMinIdlePerKey()} 68 * @deprecated Use {@link #EvictionConfig(Duration, Duration, int)}. 69 */ 70 @Deprecated 71 public EvictionConfig(final long poolIdleEvictMillis, final long poolIdleSoftEvictMillis, final int minIdle) { 72 this(Duration.ofMillis(poolIdleEvictMillis), Duration.ofMillis(poolIdleSoftEvictMillis), minIdle); 73 } 74 75 /** 76 * Gets the {@code idleEvictTime} for this eviction configuration 77 * instance. 78 * <p> 79 * How the evictor behaves based on this value will be determined by the 80 * configured {@link EvictionPolicy}. 81 * </p> 82 * 83 * @return The {@code idleEvictTime}. 84 * @since 2.11.0 85 */ 86 public Duration getIdleEvictDuration() { 87 return idleEvictDuration; 88 } 89 90 /** 91 * Gets the {@code idleEvictTime} for this eviction configuration 92 * instance. 93 * <p> 94 * How the evictor behaves based on this value will be determined by the 95 * configured {@link EvictionPolicy}. 96 * </p> 97 * 98 * @return The {@code idleEvictTime} in milliseconds 99 * @deprecated Use {@link #getIdleEvictDuration()}. 100 */ 101 @Deprecated 102 public long getIdleEvictTime() { 103 return idleEvictDuration.toMillis(); 104 } 105 106 /** 107 * Gets the {@code idleEvictTime} for this eviction configuration 108 * instance. 109 * <p> 110 * How the evictor behaves based on this value will be determined by the 111 * configured {@link EvictionPolicy}. 112 * </p> 113 * 114 * @return The {@code idleEvictTime}. 115 * @since 2.10.0 116 * @deprecated Use {@link #getIdleEvictDuration()}. 117 */ 118 @Deprecated 119 public Duration getIdleEvictTimeDuration() { 120 return idleEvictDuration; 121 } 122 123 /** 124 * Gets the {@code idleSoftEvictTime} for this eviction configuration 125 * instance. 126 * <p> 127 * How the evictor behaves based on this value will be determined by the 128 * configured {@link EvictionPolicy}. 129 * </p> 130 * 131 * @return The (@code idleSoftEvictTime} in milliseconds 132 * @since 2.11.0 133 */ 134 public Duration getIdleSoftEvictDuration() { 135 return idleSoftEvictDuration; 136 } 137 138 /** 139 * Gets the {@code idleSoftEvictTime} for this eviction configuration 140 * instance. 141 * <p> 142 * How the evictor behaves based on this value will be determined by the 143 * configured {@link EvictionPolicy}. 144 * </p> 145 * 146 * @return The (@code idleSoftEvictTime} in milliseconds 147 * @deprecated Use {@link #getIdleSoftEvictDuration()}. 148 */ 149 @Deprecated 150 public long getIdleSoftEvictTime() { 151 return idleSoftEvictDuration.toMillis(); 152 } 153 154 /** 155 * Gets the {@code idleSoftEvictTime} for this eviction configuration 156 * instance. 157 * <p> 158 * How the evictor behaves based on this value will be determined by the 159 * configured {@link EvictionPolicy}. 160 * </p> 161 * 162 * @return The (@code idleSoftEvictTime} in milliseconds 163 * @deprecated Use {@link #getIdleSoftEvictDuration()}. 164 */ 165 @Deprecated 166 public Duration getIdleSoftEvictTimeDuration() { 167 return idleSoftEvictDuration; 168 } 169 170 /** 171 * Gets the {@code minIdle} for this eviction configuration instance. 172 * <p> 173 * How the evictor behaves based on this value will be determined by the 174 * configured {@link EvictionPolicy}. 175 * </p> 176 * 177 * @return The {@code minIdle} 178 */ 179 public int getMinIdle() { 180 return minIdle; 181 } 182 183 /** 184 * @since 2.4 185 */ 186 @Override 187 public String toString() { 188 final StringBuilder builder = new StringBuilder(); 189 builder.append("EvictionConfig [idleEvictDuration="); 190 builder.append(idleEvictDuration); 191 builder.append(", idleSoftEvictDuration="); 192 builder.append(idleSoftEvictDuration); 193 builder.append(", minIdle="); 194 builder.append(minIdle); 195 builder.append("]"); 196 return builder.toString(); 197 } 198 }