Source code

    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 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 *
    042 * @since 2.0
    043 */
    044public class DefaultEvictionPolicy<T> implements EvictionPolicy<T> {
    045
    046    @Override
    047    public boolean evict(final EvictionConfig config, final PooledObject<T> underTest, final int idleCount) {
    048        // @formatter:off
    049        return (config.getIdleSoftEvictDuration().compareTo(underTest.getIdleDuration()) < 0 &&
    050                config.getMinIdle() < idleCount) ||
    051                config.getIdleEvictDuration().compareTo(underTest.getIdleDuration()) < 0;
    052        // @formatter:on
    053    }
    054}