    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 *      https://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 org.apache.commons.pool2.PooledObject;
    020
    021/**
    022 * Provides the default implementation of {@link EvictionPolicy} used by the pools.
    023 * <p>
    024 * Objects will be evicted if the following conditions are met:
    025 * </p>
    026 * <ul>
    027 * <li>The object has been idle longer than
    028 *     {@link GenericObjectPool#getMinEvictableIdleDuration()} /
    029 *     {@link GenericKeyedObjectPool#getMinEvictableIdleDuration()}</li>
    030 * <li>There are more than {@link GenericObjectPool#getMinIdle()} /
    031 *     {@link GenericKeyedObjectPoolConfig#getMinIdlePerKey()} idle objects in
    032 *     the pool and the object has been idle for longer than
    033 *     {@link GenericObjectPool#getSoftMinEvictableIdleDuration()} /
    034 *     {@link GenericKeyedObjectPool#getSoftMinEvictableIdleDuration()}
    035 * </ul>
    036 * <p>
    037 * This class is immutable and thread-safe.
    038 * </p>
    039 *
    040 * @param <T> the type of objects in the pool.
    041 * @since 2.0
    042 */
    043public class DefaultEvictionPolicy<T> implements EvictionPolicy<T> {
    044
    045    /**
    046     * Constructs a new instance.
    047     */
    048    public DefaultEvictionPolicy() {
    049        // empty
    050    }
    051
    052    @Override
    053    public boolean evict(final EvictionConfig config, final PooledObject<T> underTest, final int idleCount) {
    054        // @formatter:off
    055        return config.getIdleSoftEvictDuration().compareTo(underTest.getIdleDuration()) < 0 &&
    056                config.getMinIdle() < idleCount ||
    057                config.getIdleEvictDuration().compareTo(underTest.getIdleDuration()) < 0;
    058        // @formatter:on
    059    }
    060}