1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 module hunt.pool.impl.EvictionConfig;
18 
19 import hunt.Long;
20 import hunt.util.StringBuilder;
21 
22 /**
23  * This class is used by pool implementations to pass configuration information
24  * to {@link EvictionPolicy} instances. The {@link EvictionPolicy} may also have
25  * its own specific configuration attributes.
26  * <p>
27  * This class is immutable and thread-safe.
28  *
29  */
30 class EvictionConfig {
31 
32     private long idleEvictTime;
33     private long idleSoftEvictTime;
34     private int minIdle;
35 
36     /**
37      * Create a new eviction configuration with the specified parameters.
38      * Instances are immutable.
39      *
40      * @param poolIdleEvictTime Expected to be provided by
41      *        {@link BaseGenericObjectPool#getMinEvictableIdleTimeMillis()}
42      * @param poolIdleSoftEvictTime Expected to be provided by
43      *        {@link BaseGenericObjectPool#getSoftMinEvictableIdleTimeMillis()}
44      * @param minIdle Expected to be provided by
45      *        {@link GenericObjectPool#getMinIdle()} or
46      *        {@link GenericKeyedObjectPool#getMinIdlePerKey()}
47      */
48     this(long poolIdleEvictTime, long poolIdleSoftEvictTime,
49             int minIdle) {
50         if (poolIdleEvictTime > 0) {
51             idleEvictTime = poolIdleEvictTime;
52         } else {
53             idleEvictTime = Long.MAX_VALUE;
54         }
55         if (poolIdleSoftEvictTime > 0) {
56             idleSoftEvictTime = poolIdleSoftEvictTime;
57         } else {
58             idleSoftEvictTime  = Long.MAX_VALUE;
59         }
60         this.minIdle = minIdle;
61     }
62 
63     /**
64      * Obtain the {@code idleEvictTime} for this eviction configuration
65      * instance.
66      * <p>
67      * How the evictor behaves based on this value will be determined by the
68      * configured {@link EvictionPolicy}.
69      *
70      * @return The {@code idleEvictTime} in milliseconds
71      */
72     long getIdleEvictTime() {
73         return idleEvictTime;
74     }
75 
76     /**
77      * Obtain the {@code idleSoftEvictTime} for this eviction configuration
78      * instance.
79      * <p>
80      * How the evictor behaves based on this value will be determined by the
81      * configured {@link EvictionPolicy}.
82      *
83      * @return The (@code idleSoftEvictTime} in milliseconds
84      */
85     long getIdleSoftEvictTime() {
86         return idleSoftEvictTime;
87     }
88 
89     /**
90      * Obtain the {@code minIdle} for this eviction configuration instance.
91      * <p>
92      * How the evictor behaves based on this value will be determined by the
93      * configured {@link EvictionPolicy}.
94      *
95      * @return The {@code minIdle}
96      */
97     int getMinIdle() {
98         return minIdle;
99     }
100 
101     /**
102      */
103     override
104     string toString() {
105         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 }