View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of 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,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.jcache;
18  
19  import net.sf.ehcache.Element;
20  import net.sf.jsr107cache.CacheEntry;
21  
22  /**
23   * An implementation of CacheEntry.
24   *
25   * A CacheEntry is metadata about an entry in the cache. It does not include the value.
26   *                   
27   * @author Greg Luck
28   * @version $Id: JCacheEntry.java 512 2007-07-10 09:18:45Z gregluck $
29   */
30  public class JCacheEntry implements CacheEntry {
31      private Element element;
32  
33      /**
34       * Constructor
35       *
36       * @param element an element from Ehcache
37       */
38      public JCacheEntry(Element element) {
39          this.element = element;
40      }
41  
42      /**
43       * Returns the key corresponding to this entry.
44       *
45       * @return the key corresponding to this entry.
46       * @throws IllegalStateException implementations may, but are not
47       *                               required to, throw this exception if the entry has been
48       *                               removed from the backing map
49       */
50      public Object getKey() throws IllegalStateException {
51          if (element != null) {
52              return element.getObjectKey();
53          } else {
54              return null;
55          }
56      }
57  
58      /**
59       * Returns the value corresponding to this entry.  If the mapping
60       * has been removed from the backing map (by the iterator's
61       * <tt>remove</tt> operation), the results of this call are undefined.
62       *
63       * @return the value corresponding to this entry.
64       * @throws IllegalStateException implementations may, but are not
65       *                               required to, throw this exception if the entry has been
66       *                               removed from the backing map
67       */
68      public Object getValue() throws IllegalStateException {
69          if (element != null) {
70              return element.getObjectValue();
71          } else {
72              return null;
73          }
74      }
75  
76      /**
77       * Replaces the value corresponding to this entry with the specified
78       * value (optional operation).  (Writes through to the map.)  The
79       * behavior of this call is undefined if the mapping has already been
80       * removed from the map (by the iterator's <tt>remove</tt> operation).
81       *
82       * @param value new value to be stored in this entry.
83       * @return old value corresponding to the entry.
84       * @throws UnsupportedOperationException if the <tt>put</tt> operation
85       *                                       is not supported by the backing map.
86       */
87      public Object setValue(Object value) throws UnsupportedOperationException {
88          throw new UnsupportedOperationException("Ehcache does not support modification of Elements. They are immutable.");
89      }
90  
91      /**
92       * This implementation does not have a notion of cost. Accordingly, 0 is always returned.
93       *
94       * @return 0
95       */
96      public long getCost() {
97          return 0;
98      }
99  
100     /**
101      * Gets the creationTime attribute of the ElementAttributes object.
102      *
103      * @return The creationTime value
104      */
105     public long getCreationTime() {
106         if (element != null) {
107             return element.getCreationTime();
108         } else {
109             return 0;
110         }
111     }
112 
113     /**
114      * Returns the expiration time based on time to live. If this element also has a time to idle setting, the expiry
115      * time will vary depending on whether the element is accessed.
116      *
117      * @return the time to expiration
118      */
119     public long getExpirationTime() {
120         if (element != null) {
121             return element.getExpirationTime();
122         } else {
123             return 0;
124         }
125     }
126 
127     /**
128      * Gets the hit count on this element. A new element has a hit count of 0.
129      * @return the number of hits this Element has had. If the Element was updated, the hit count is reset to 0.
130      */
131     public int getHits() {
132         if (element != null) {
133             return (int) element.getHitCount();
134         } else {
135             return 0;
136         }
137     }
138 
139     /**
140      * Gets the last access time.
141      * Access means a get. So a newly created {@link Element}
142      * will have a last access time equal to its create time.
143      */
144     public long getLastAccessTime() {
145         if (element != null) {
146             return element.getLastAccessTime();
147         } else {
148             return 0;
149         }
150     }
151 
152     /**
153      * Updated means there was an existing Element that was replaced with a new one, for the same key. If an update
154      * occurs, then the lastUpdateTime will be non-zero.
155      * @return 0 if never updated, otherwise the time the update occurred.
156      */
157     public long getLastUpdateTime() {
158         if (element != null) {
159             return element.getLastUpdateTime();
160         } else {
161             return 0;
162         }
163     }
164 
165     /**
166      * Returns the version of the Element. This is based on timestamps.
167      * @return 0 if the first version, otherwise the timestamp when the version was created.
168      */
169     public long getVersion() {
170         if (element != null) {
171             return element.getVersion();
172         } else {
173             return 0;
174         }
175 
176     }
177 
178     /**
179      * An Element is 'valid' if it is unexpired.
180      * @return true if unexpired, otherwise false.
181      */
182     public boolean isValid() {
183         if (element != null) {
184             return !element.isExpired();
185         } else {
186             return false;
187         }
188     }
189 }
190