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_MILLIS_DURATION = Duration.ofMillis(Long.MAX_VALUE);
034    private final Duration idleEvictTime;
035    private final Duration idleSoftEvictTime;
036    private final int minIdle;
037
038    /**
039     * Creates a new eviction configuration with the specified parameters.
040     * Instances are immutable.
041     *
042     * @param poolIdleEvictTime Expected to be provided by
043     *        {@link BaseGenericObjectPool#getMinEvictableIdleTimeMillis()}
044     * @param poolIdleSoftEvictTime Expected to be provided by
045     *        {@link BaseGenericObjectPool#getSoftMinEvictableIdleTimeMillis()}
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 poolIdleEvictTime, final Duration poolIdleSoftEvictTime, final int minIdle) {
052        if (PoolImplUtils.isPositive(poolIdleEvictTime)) {
053            idleEvictTime = poolIdleEvictTime;
054        } else {
055            idleEvictTime = MAX_MILLIS_DURATION;
056        }
057        if (PoolImplUtils.isPositive(poolIdleSoftEvictTime)) {
058            idleSoftEvictTime = poolIdleSoftEvictTime;
059        } else {
060            idleSoftEvictTime = MAX_MILLIS_DURATION;
061        }
062        this.minIdle = minIdle;
063    }
064
065    /**
066     * Creates a new eviction configuration with the specified parameters.
067     * Instances are immutable.
068     *
069     * @param poolIdleEvictTime Expected to be provided by
070     *        {@link BaseGenericObjectPool#getMinEvictableIdleTimeMillis()}
071     * @param poolIdleSoftEvictTime Expected to be provided by
072     *        {@link BaseGenericObjectPool#getSoftMinEvictableIdleTimeMillis()}
073     * @param minIdle Expected to be provided by
074     *        {@link GenericObjectPool#getMinIdle()} or
075     *        {@link GenericKeyedObjectPool#getMinIdlePerKey()}
076     * @deprecated Use {@link #EvictionConfig(Duration, Duration, int)}.
077     */
078    @Deprecated
079    public EvictionConfig(final long poolIdleEvictTime, final long poolIdleSoftEvictTime, final int minIdle) {
080        this(Duration.ofMillis(poolIdleEvictTime), Duration.ofMillis(poolIdleSoftEvictTime), minIdle);
081    }
082
083    /**
084     * Gets the {@code idleEvictTime} for this eviction configuration
085     * instance.
086     * <p>
087     * How the evictor behaves based on this value will be determined by the
088     * configured {@link EvictionPolicy}.
089     * </p>
090     *
091     * @return The {@code idleEvictTime} in milliseconds
092     */
093    public long getIdleEvictTime() {
094        return idleEvictTime.toMillis();
095    }
096
097    /**
098     * Gets the {@code idleEvictTime} for this eviction configuration
099     * instance.
100     * <p>
101     * How the evictor behaves based on this value will be determined by the
102     * configured {@link EvictionPolicy}.
103     * </p>
104     *
105     * @return The {@code idleEvictTime}.
106     * @since 2.10.0
107     */
108    public Duration getIdleEvictTimeDuration() {
109        return idleEvictTime;
110    }
111
112    /**
113     * Gets the {@code idleSoftEvictTime} for this eviction configuration
114     * instance.
115     * <p>
116     * How the evictor behaves based on this value will be determined by the
117     * configured {@link EvictionPolicy}.
118     * </p>
119     *
120     * @return The (@code idleSoftEvictTime} in milliseconds
121     */
122    public long getIdleSoftEvictTime() {
123        return idleSoftEvictTime.toMillis();
124    }
125
126    /**
127     * Gets the {@code idleSoftEvictTime} for this eviction configuration
128     * instance.
129     * <p>
130     * How the evictor behaves based on this value will be determined by the
131     * configured {@link EvictionPolicy}.
132     * </p>
133     *
134     * @return The (@code idleSoftEvictTime} in milliseconds
135     */
136    public Duration getIdleSoftEvictTimeDuration() {
137        return idleSoftEvictTime;
138    }
139
140    /**
141     * Gets the {@code minIdle} for this eviction configuration instance.
142     * <p>
143     * How the evictor behaves based on this value will be determined by the
144     * configured {@link EvictionPolicy}.
145     * </p>
146     *
147     * @return The {@code minIdle}
148     */
149    public int getMinIdle() {
150        return minIdle;
151    }
152
153    /**
154     * @since 2.4
155     */
156    @Override
157    public String toString() {
158        final StringBuilder builder = new StringBuilder();
159        builder.append("EvictionConfig [idleEvictTime=");
160        builder.append(idleEvictTime);
161        builder.append(", idleSoftEvictTime=");
162        builder.append(idleSoftEvictTime);
163        builder.append(", minIdle=");
164        builder.append(minIdle);
165        builder.append("]");
166        return builder.toString();
167    }
168}