View Javadoc

1   /* 
2    * Created on Oct 28, 2004
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   *
16   * Copyright @2004 the original author or authors.
17   */
18  
19  package org.wanghy.cache.provider.lru;
20  
21  import org.wanghy.cache.AbstractCacheEntry;
22  import org.wanghy.cache.provider.CacheProfile;
23  
24  import org.apache.commons.lang.builder.EqualsBuilder;
25  import org.apache.commons.lang.builder.HashCodeBuilder;
26  import org.apache.commons.lang.builder.ToStringBuilder;
27  
28  /***
29   * <p>
30   * Set of configuration options needed for:
31   * <ul>
32   * <li>Retrieving an entry from a LruCache cache</li>
33   * <li>Storing an object in a LruCache cache</li>
34   * <li>Flushing a cache group</li>
35   * </ul>
36   * </p>
37   * 
38   * @author Alex Ruiz
39   * 
40   * @version $Revision: 1.10 $ $Date: 2005/03/11 12:02:50 $
41   */
42  public class LruCacheProfile implements CacheProfile {
43  
44    /***
45     * Version number of this class.
46     * 
47     * @see java.io.Serializable
48     */
49    private static final long serialVersionUID = 3257567325715772214L;
50  
51    /***
52     * The group the object to cache belongs to.
53     */
54    private String group;
55  
56    /***
57     * How long an entry can stay in the cache (in seconds).
58     */
59    private int refreshPeriod;
60  
61    /***
62     * Constructor.
63     */
64    public LruCacheProfile() {
65      super();
66      this.setRefreshPeriod(AbstractCacheEntry.INDEFINITE_EXPIRY);
67    }
68  
69    /***
70     * Indicates whether some other object is "equal to" this one.
71     * 
72     * @param obj
73     *          the reference object with which to compare.
74     * @return <code>true</code> if this object is the same as the obj argument;
75     *         <code>false</code> otherwise.
76     * @see #hashCode()
77     */
78    public boolean equals(Object obj) {
79      boolean equals = true;
80  
81      if (null == obj || !(obj instanceof LruCacheProfile)) {
82        equals = false;
83      } else if (this != obj) {
84        LruCacheProfile profile = (LruCacheProfile) obj;
85  
86        EqualsBuilder equalsBuilder = new EqualsBuilder();
87        equalsBuilder.append(this.getGroup(), profile.getGroup());
88        equalsBuilder.append(this.getRefreshPeriod(), profile.getRefreshPeriod());
89  
90        equals = equalsBuilder.isEquals();
91      }
92  
93      return equals;
94    }
95  
96    /***
97     * Getter for field <code>{@link #group}</code>.
98     * 
99     * @return the field <code>group</code>.
100    */
101   public final String getGroup() {
102     return this.group;
103   }
104 
105   /***
106    * Getter for field <code>{@link #refreshPeriod}</code>.
107    * 
108    * @return the field <code>refreshPeriod</code>.
109    */
110   public final int getRefreshPeriod() {
111     return this.refreshPeriod;
112   }
113 
114   /***
115    * Returns a hash code value for the object. This method is supported for the
116    * benefit of hashtables such as those provided by
117    * <code>java.util.Hashtable</code>.
118    * 
119    * @return a hash code value for this object.
120    */
121   public int hashCode() {
122     HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(3, 7);
123     hashCodeBuilder.append(this.getGroup());
124     hashCodeBuilder.append(this.getRefreshPeriod());
125 
126     int hashCode = hashCodeBuilder.toHashCode();
127     return hashCode;
128   }
129 
130   /***
131    * Setter for the field <code>{@link #group}</code>.
132    * 
133    * @param group
134    *          the new value to set
135    */
136   public final void setGroup(String group) {
137     this.group = group;
138   }
139 
140   /***
141    * Setter for the field <code>{@link #refreshPeriod}</code>.
142    * 
143    * @param refreshPeriod
144    *          the new value to set
145    */
146   public final void setRefreshPeriod(int refreshPeriod) {
147     this.refreshPeriod = refreshPeriod;
148   }
149 
150   /***
151    * Setter for the field <code>{@link #refreshPeriod}</code>.
152    * 
153    * @param refreshPeriod
154    *          the new value to set
155    */
156   public final void setRefreshPeriod(Integer refreshPeriod) {
157     if (refreshPeriod != null) {
158       this.setRefreshPeriod(refreshPeriod.intValue());
159     }
160   }
161 
162   /***
163    * Returns a string representation of the object. In general, the
164    * <code>toString</code> method returns a string that "textually represents"
165    * this object.
166    * 
167    * @return a string representation of the object.
168    */
169   public String toString() {
170     ToStringBuilder toStringBuilder = new ToStringBuilder(this);
171     toStringBuilder.append("group", this.getGroup());
172     toStringBuilder.append("refreshPeriod", this.getRefreshPeriod());
173 
174     String toString = toStringBuilder.toString();
175     return toString;
176   }
177 
178 }