/* Open Source Java Caching Service
* Copyright (C) 2002 Frank Karlstr�m
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The author can be contacted by email: fjankk@users.sourceforge.net
*/
package org.fjank.jcache;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.util.jcache.Attributes;
import javax.util.jcache.CacheObjectInfo;
/**
* class wich holds information about an object in the cache. For information
* purposes only.
*
* @author Frank Karlstr�m
*/
final class CacheObjectInfoImpl implements CacheObjectInfo {
/** number of references to the object this info accounts for. */
private final int refCount;
/** number of accesses to the object this info accounts for. */
private final int accesses;
/** when the object this info accounts for expires. */
private long expires;
/** the group the object this info accounts for belongs to. */
private String group;
/** the type of the object
* @todo never assigned.
**/
private String type;
/** the name of the object */
private final Object name;
/** the region the object belongs in.
* @todo never assigned.
* */
private Object region;
/**
* Creates new CacheObjectInfo
* @todo the type is hardcoded to Memory object.
* @param object the object this info describes.
*/
CacheObjectInfoImpl(final CacheObject object) {
CacheGroup ownerGroup = object.getGroup();
if ((ownerGroup != null) && (ownerGroup.getName() != null)) {
this.group = ownerGroup.getName().toString();
}
this.name = object.getKey();
Attributes attribs = object.getAttributes();
if (attribs != null) {
long TTL = attribs.getTimeToLive();
this.expires = TTL>0 ? TTL * 1000 + attribs.getCreateTime()
: -1;
//this.expires = (attribs.getTimeToLive()*1000) + attribs.getCreateTime();
}
this.accesses = object.getAccesses();
this.refCount = object.getRefCount();
this.region = object.getRegion();
this.type = "Memory object";
}
/**
* returns the region the object resides in.
*
* @return the region the object resides in.
*/
public String getRegion() {
return ((region != null) ? region.toString() : "Default region");
}
/**
* returns the name of the object.
*
* @return the name of the object.
*/
public String getName() {
return name.toString();
}
/**
* returns the type of the object. (Memory object, disk object, group,
* region)
*
* @return the type of the object.
*/
public String getType() {
return type;
}
/**
* returns the group the object is associated with.
*
* @return the group the object is associated with.
*/
public String getGroup() {
return ((group != null) ? group : "");
}
/**
* returns the current reference count to the object.
*
* @return the current reference count to the object.
*/
public int getRefCount() {
return refCount;
}
/**
* returns the total number of accesses to this object.
*
* @return the total number of accesses to this object.
*/
public int getAccesses() {
return accesses;
}
/**
* Return the time the object will expire if any.
*
* @return the time the object will expire if any
*/
public String getExpire() {
if(expires!=-1) {
SimpleDateFormat form = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = new Date(expires);
return form.format(date);
}
return NEVER_EXPIRES;
}
/**
* returns a string representation of this info.
*
* @return a string representation of this info.
*/
public String toString() {
StringBuffer ret = new StringBuffer("name:" + getName());
ret.append(", region:" + getRegion());
ret.append(", type:" + getType());
if (!getGroup().equals("")) {
ret.append(", group:" + getGroup());
}
ret.append(", refCount:" + getRefCount());
ret.append(", accesses:" + getAccesses());
ret.append(", expires:" + getExpire());
return ret.toString();
}
}