View Javadoc

1   /*   Open Source Java Caching Service
2   *    Copyright (C) 2002 Frank Karlstrøm
3   *    This library is free software; you can redistribute it and/or
4   *    modify it under the terms of the GNU Lesser General Public
5   *    License as published by the Free Software Foundation; either
6   *    version 2.1 of the License, or (at your option) any later version.
7   *
8   *    This library is distributed in the hope that it will be useful,
9   *    but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  *    Lesser General Public License for more details.
12  *
13  *    You should have received a copy of the GNU Lesser General Public
14  *    License along with this library; if not, write to the Free Software
15  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  *    The author can be contacted by email: fjankk@users.sourceforge.net
18  */
19  package org.fjank.jcache;
20  
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import javax.util.jcache.Attributes;
24  import javax.util.jcache.CacheObjectInfo;
25  
26  
27  /**
28   * class wich holds information about an object in the cache. For information
29   * purposes only.
30   *
31   * @author Frank Karlstrøm
32   */
33  final class CacheObjectInfoImpl implements CacheObjectInfo {
34      /** number of references to the object this info accounts for. */
35      private final int refCount;
36  
37      /** number of accesses to the object this info accounts for. */
38      private final int accesses;
39  
40      /** when the object this info accounts for expires. */
41      private long expires;
42  
43      /** the group the object this info accounts for belongs to. */
44      private String group;
45  
46      /** the type of the object 
47       * @todo never assigned.
48       **/
49      
50      private String type;
51  
52      /** the name of the object */
53      private final Object name;
54  
55      /** the region the object belongs in. 
56       * @todo never assigned.
57       * */
58      
59      private Object region;
60  
61      /**
62       * Creates new CacheObjectInfo
63       * @todo the type is hardcoded to Memory object.
64       * @param object the object this info describes.
65       */
66      CacheObjectInfoImpl(final CacheObject object) {
67          CacheGroup ownerGroup = object.getGroup();
68          if ((ownerGroup != null) && (ownerGroup.getName() != null)) {
69              this.group = ownerGroup.getName().toString();
70          }
71          this.name = object.getKey();
72          Attributes attribs = object.getAttributes();
73          if (attribs != null) {
74              long TTL = attribs.getTimeToLive();
75              this.expires = TTL>0 ? TTL * 1000 + attribs.getCreateTime()
76              : -1;
77  
78              //this.expires = (attribs.getTimeToLive()*1000) + attribs.getCreateTime();
79          }
80          this.accesses = object.getAccesses();
81          this.refCount = object.getRefCount();
82          this.region	= object.getRegion();
83          this.type = "Memory object";
84      }
85  
86      /**
87       * returns the region the object resides in.
88       *
89       * @return the region the object resides in.
90       */
91      public String getRegion() {
92          return ((region != null) ? region.toString() : "Default region");
93      }
94  
95      /**
96       * returns the name of the object.
97       *
98       * @return the name of the object.
99       */
100     public String getName() {
101         return name.toString();
102     }
103 
104     /**
105      * returns the type of the object. (Memory object, disk object, group,
106      * region)
107      *
108      * @return the type of the object.
109      */
110     public String getType() {
111         return type;
112     }
113 
114     /**
115      * returns the group the object is associated with.
116      *
117      * @return the group the object is associated with.
118      */
119     public String getGroup() {
120         return ((group != null) ? group : "");
121     }
122 
123     /**
124      * returns the current reference count to the object.
125      *
126      * @return the current reference count to the object.
127      */
128     public int getRefCount() {
129         return refCount;
130     }
131 
132     /**
133      * returns the total number of accesses to this object.
134      *
135      * @return the total number of accesses to this object.
136      */
137     public int getAccesses() {
138         return accesses;
139     }
140 
141     /**
142      * Return the time the object will expire if any.
143      *
144      * @return the time the object will expire if any
145      */
146     public String getExpire() {
147         if(expires!=-1) {
148             SimpleDateFormat form = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
149             Date date = new Date(expires);
150             return form.format(date);
151         }
152         return NEVER_EXPIRES;
153     }
154 
155     /**
156      * returns a string representation of this info.
157      *
158      * @return a string representation of this info.
159      */
160     public String toString() {
161         StringBuffer ret = new StringBuffer("name:" + getName());
162         ret.append(", region:" + getRegion());
163         ret.append(", type:" + getType());
164         if (!getGroup().equals("")) {
165             ret.append(", group:" + getGroup());
166         }
167         ret.append(", refCount:" + getRefCount());
168         ret.append(", accesses:" + getAccesses());
169         ret.append(", expires:" + getExpire());
170         return ret.toString();
171     }
172 }