KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > LRUConfiguration


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.eviction;
8
9 import org.jboss.cache.config.ConfigurationException;
10 import org.jboss.cache.config.Dynamic;
11
12 /**
13  * Configuration implementation for {@link LRUPolicy}.
14  * <p/>
15  * If configured via XML, expects the following:
16  * <p/>
17  * <pre>
18  * <region name="/maxAgeTest/">
19  * <attribute name="maxNodes">10000</attribute>
20  * <attribute name="timeToLiveSeconds">8</attribute>
21  * <attribute name="maxAgeSeconds">10</attribute>
22  * </region>
23  * </pre>
24  *
25  * @author Daniel Huang (dhuang@jboss.org)
26  * @version $Revision: 1.5 $
27  */

28 public class LRUConfiguration extends EvictionPolicyConfigBase
29 {
30    /** The serialVersionUID */
31    private static final long serialVersionUID = -3426716488271559729L;
32
33    @Dynamic
34    private int timeToLiveSeconds;
35    @Dynamic
36    private int maxAgeSeconds;
37
38    public LRUConfiguration()
39    {
40       setEvictionPolicyClassName();
41       // Force config of ttls
42
setTimeToLiveSeconds(-1);
43    }
44
45    @Override JavaDoc
46    protected void setEvictionPolicyClassName()
47    {
48       setEvictionPolicyClass(LRUPolicy.class.getName());
49    }
50
51    
52    public int getTimeToLiveSeconds()
53    {
54       return timeToLiveSeconds;
55    }
56
57    public void setTimeToLiveSeconds(int timeToLiveSeconds)
58    {
59       testImmutability("timeToLiveSeconds");
60       this.timeToLiveSeconds = timeToLiveSeconds;
61    }
62
63    public int getMaxAgeSeconds()
64    {
65       return maxAgeSeconds;
66    }
67
68    public void setMaxAgeSeconds(int maxAgeSeconds)
69    {
70       testImmutability("maxAgeSeconds");
71       this.maxAgeSeconds = maxAgeSeconds;
72    }
73    
74    /**
75     * Requires a positive timeToLiveSeconds value or ConfigurationException
76     * is thrown.
77     */

78    @Override JavaDoc
79    public void validate() throws ConfigurationException
80    {
81       if (timeToLiveSeconds < 0)
82          throw new ConfigurationException("timeToLiveSeconds not configured");
83    }
84
85    public String JavaDoc toString()
86    {
87       StringBuffer JavaDoc str = new StringBuffer JavaDoc();
88       str.append("LRUConfiguration: timeToLiveSeconds = ").append(getTimeToLiveSeconds()).append(" maxAgeSeconds =");
89       str.append(getMaxAgeSeconds()).append(" maxNodes =").append(getMaxNodes());
90       return str.toString();
91    }
92
93    @Override JavaDoc
94    public boolean equals(Object JavaDoc obj)
95    {
96       if (obj instanceof LRUConfiguration && super.equals(obj))
97       {
98          LRUConfiguration other = (LRUConfiguration) obj;
99          return maxAgeSeconds == other.maxAgeSeconds
100                   && timeToLiveSeconds == other.timeToLiveSeconds;
101       }
102       return false;
103    }
104
105    @Override JavaDoc
106    public int hashCode()
107    {
108       int result = super.hashCode();
109       result = 31 * result + maxAgeSeconds;
110       result = 31 * result + timeToLiveSeconds;
111       return result;
112    }
113
114    @Override JavaDoc
115    public void reset()
116    {
117       super.reset();
118       setTimeToLiveSeconds(-1);
119    }
120    
121 }
122
Popular Tags