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
019/**
020 * This class is used by pool implementations to pass configuration information
021 * to {@link EvictionPolicy} instances. The {@link EvictionPolicy} may also have
022 * its own specific configuration attributes.
023 * <p>
024 * This class is immutable and thread-safe.
025 * </p>
026 *
027 * @since 2.0
028 */
029public class EvictionConfig {
030
031    private final long idleEvictTime;
032    private final long idleSoftEvictTime;
033    private final int minIdle;
034
035    /**
036     * Create a new eviction configuration with the specified parameters.
037     * Instances are immutable.
038     *
039     * @param poolIdleEvictTime Expected to be provided by
040     *        {@link BaseGenericObjectPool#getMinEvictableIdleTimeMillis()}
041     * @param poolIdleSoftEvictTime Expected to be provided by
042     *        {@link BaseGenericObjectPool#getSoftMinEvictableIdleTimeMillis()}
043     * @param minIdle Expected to be provided by
044     *        {@link GenericObjectPool#getMinIdle()} or
045     *        {@link GenericKeyedObjectPool#getMinIdlePerKey()}
046     */
047    public EvictionConfig(final long poolIdleEvictTime, final long poolIdleSoftEvictTime,
048            final int minIdle) {
049        if (poolIdleEvictTime > 0) {
050            idleEvictTime = poolIdleEvictTime;
051        } else {
052            idleEvictTime = Long.MAX_VALUE;
053        }
054        if (poolIdleSoftEvictTime > 0) {
055            idleSoftEvictTime = poolIdleSoftEvictTime;
056        } else {
057            idleSoftEvictTime  = Long.MAX_VALUE;
058        }
059        this.minIdle = minIdle;
060    }
061
062    /**
063     * Obtain the {@code idleEvictTime} for this eviction configuration
064     * instance.
065     * <p>
066     * How the evictor behaves based on this value will be determined by the
067     * configured {@link EvictionPolicy}.
068     *
069     * @return The {@code idleEvictTime} in milliseconds
070     */
071    public long getIdleEvictTime() {
072        return idleEvictTime;
073    }
074
075    /**
076     * Obtain the {@code idleSoftEvictTime} 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     *
082     * @return The (@code idleSoftEvictTime} in milliseconds
083     */
084    public long getIdleSoftEvictTime() {
085        return idleSoftEvictTime;
086    }
087
088    /**
089     * Obtain the {@code minIdle} for this eviction configuration instance.
090     * <p>
091     * How the evictor behaves based on this value will be determined by the
092     * configured {@link EvictionPolicy}.
093     *
094     * @return The {@code minIdle}
095     */
096    public int getMinIdle() {
097        return minIdle;
098    }
099
100    /**
101     * @since 2.4
102     */
103    @Override
104    public String toString() {
105        final StringBuilder builder = new StringBuilder();
106        builder.append("EvictionConfig [idleEvictTime=");
107        builder.append(idleEvictTime);
108        builder.append(", idleSoftEvictTime=");
109        builder.append(idleSoftEvictTime);
110        builder.append(", minIdle=");
111        builder.append(minIdle);
112        builder.append("]");
113        return builder.toString();
114    }
115}