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 *      http://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
019import java.time.Duration;
020
021/**
022 * This class is used by pool implementations to pass configuration information
023 * to {@link EvictionPolicy} instances. The {@link EvictionPolicy} may also have
024 * its own specific configuration attributes.
025 * <p>
026 * This class is immutable and thread-safe.
027 * </p>
028 *
029 * @since 2.0
030 */
031public class EvictionConfig {
032
033    private static final Duration MAX_DURATION = Duration.ofMillis(Long.MAX_VALUE);
034    private final Duration idleEvictDuration;
035    private final Duration idleSoftEvictDuration;
036    private final int minIdle;
037
038    /**
039     * Creates a new eviction configuration with the specified parameters.
040     * Instances are immutable.
041     *
042     * @param idleEvictDuration Expected to be provided by
043     *        {@link BaseGenericObjectPool#getMinEvictableIdleDuration()}
044     * @param idleSoftEvictDuration Expected to be provided by
045     *        {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()}
046     * @param minIdle Expected to be provided by
047     *        {@link GenericObjectPool#getMinIdle()} or
048     *        {@link GenericKeyedObjectPool#getMinIdlePerKey()}
049     * @since 2.10.0
050     */
051    public EvictionConfig(final Duration idleEvictDuration, final Duration idleSoftEvictDuration, final int minIdle) {
052        this.idleEvictDuration = PoolImplUtils.isPositive(idleEvictDuration) ? idleEvictDuration : MAX_DURATION;
053        this.idleSoftEvictDuration = PoolImplUtils.isPositive(idleSoftEvictDuration) ? idleSoftEvictDuration : MAX_DURATION;
054        this.minIdle = minIdle;
055    }
056
057    /**
058     * Creates a new eviction configuration with the specified parameters.
059     * Instances are immutable.
060     *
061     * @param poolIdleEvictMillis Expected to be provided by
062     *        {@link BaseGenericObjectPool#getMinEvictableIdleDuration()}
063     * @param poolIdleSoftEvictMillis Expected to be provided by
064     *        {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()}
065     * @param minIdle Expected to be provided by
066     *        {@link GenericObjectPool#getMinIdle()} or
067     *        {@link GenericKeyedObjectPool#getMinIdlePerKey()}
068     * @deprecated Use {@link #EvictionConfig(Duration, Duration, int)}.
069     */
070    @Deprecated
071    public EvictionConfig(final long poolIdleEvictMillis, final long poolIdleSoftEvictMillis, final int minIdle) {
072        this(Duration.ofMillis(poolIdleEvictMillis), Duration.ofMillis(poolIdleSoftEvictMillis), minIdle);
073    }
074
075    /**
076     * Gets the {@code idleEvictTime} for this eviction configuration
077     * instance.
078     * <p>
079     * How the evictor behaves based on this value will be determined by the
080     * configured {@link EvictionPolicy}.
081     * </p>
082     *
083     * @return The {@code idleEvictTime}.
084     * @since 2.11.0
085     */
086    public Duration getIdleEvictDuration() {
087        return idleEvictDuration;
088    }
089
090    /**
091     * Gets the {@code idleEvictTime} for this eviction configuration
092     * instance.
093     * <p>
094     * How the evictor behaves based on this value will be determined by the
095     * configured {@link EvictionPolicy}.
096     * </p>
097     *
098     * @return The {@code idleEvictTime} in milliseconds
099     * @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}