Package org.fjank.jcache

Source Code of org.fjank.jcache.CacheObject

/*   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.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import javax.util.jcache.Attributes;
import javax.util.jcache.CacheEvent;
import javax.util.jcache.CacheEventListener;


/**
* Overrides some of the methods for referenceobjects.
*
* @author Frank Karlstr�m
*/
public class CacheObject extends WeakReference {
    /** accesses and refCount is ignored.
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object arg0) {
        if(arg0 instanceof CacheObject) {
            CacheObject obj = (CacheObject) arg0;
            if(!attributes.equals(obj.attributes)) return false;
            if(!group.equals(obj.group))return false;
            if(!key.equals(obj.key))return false;
            if(!region.equals(obj.region))return false;
            if(valid!=obj.valid)return false;
           
            return true;
        }
        return super.equals(arg0);
       
    }
  /**
     * the number of references to this object. (only an indication, this value
     * could be false)
     */
    private int refCount;

    /** indicates wether this object is valid or not. */
    private boolean valid;

    /** the name of this object */
    private final Object key;

    /** number of total accesses to this object. */
    private int accesses;

    /** the group this object belongs to. */
    private final CacheGroup group;
  /** the region this object belongs to. */
  private final CacheRegion region;

    /** the attributes for this object */
    private AttributesImpl attributes;

    /**
     * Creates a new CacheObject object.
     *
     * @param key the name of this object
     * @param referent the actuall object
     * @param group the group this object belongs to
     * @param q the ReferenceQueue this object goes to wneh the GC determines
     *        that this object is garbage.
     */
    public CacheObject(final Object key, final Object referent,
        final CacheGroup group, final CacheRegion region, final ReferenceQueue q) {
        super(((referent instanceof String) ? new String(((String) referent))
                                            : referent), q);
        this.key = key;
        this.group = group;
        this.region = region;
        if(group!=null) {
          setAttributes(group.getAttributes());
        }
    }

    /**
     * returns a boolean indication wether the object needs loading or not.
     *
     * @return a boolean indication wether the object needs loading or not.
     */
    public boolean needsLoading() {
        return needsLoading(NullObject.getInstance());
    }
    boolean needsLoading(final Object arguments) {
        if(arguments!=null && arguments !=NullObject.getInstance()) {
            return true;
        }
        if ((super.get() == NullObject.getInstance())
                && (attributes.getLoader() != null)) {
            return true;
        }
        return false;
    }
    /**
     * destroys this object
     */
    public void destroy() {
        this.attributes = null;
        this.valid = false;
    }

    /**
     * invalidates this object
     */
    public void invalidate() {
        this.valid = false;
        if ((attributes.getLoader() == null) && (group != null)) {
            group.removeMe(this);
        }
       
        // Notify subscribers
        CacheEventListener listener = attributes.getListener();
        if (listener != null) {
      listener.handleEvent(new CacheEventImpl(CacheEvent.OBJECT_INVALIDATED, getKey()));
        }
    }

    /**
     * gets the wrapped real object.
     *
     * @return the wrapped real object.
     */
    public Object get() {
        accesses++;
        refCount++;
        return super.get();
    }

    /**
     * returns a string representation of this object
     *
     * @return a string representation of this object
     */
    public String toString() {
        return "CacheObject {accesses:"+
          accesses
        +", attributes:"+
          attributes
        +", group:"+
          group.getName()
        +", key:"+
          key
        +", refCount:"+
          refCount
        +", region:"+
          region.getName()
        +", valid:"+
          valid
        +"}";
    }

    /**
     * gets the name of this object
     *
     * @return the name of this object
     */
    public Object getKey() {
        return key;
    }

    /**
     * sets the attributes for this object
     *
     * @param attributes the attributes to set.
     */
    public void setAttributes(final AttributesImpl attributes) {
        if (attributes != null) {
            this.attributes = attributes;
        }
    }

    public void setAttributes(Attributes attributes) {
        if(attributes!=null) {
            this.attributes=new AttributesImpl(attributes);
        }
    }

    /**
     * gets the attributes for this object
     *
     * @return the attributes for this object
     */
    public AttributesImpl getAttributes() {
        return attributes;
    }

    /**
     * gets the group this object belongs to
     *
     * @return the group this object belongs to
     */
    public CacheGroup getGroup() {
        return group;
    }

    /**
     * returns the number of total accesses to this cacheobject
     *
     * @return the number of total accesses to this cacheobject
     */
    public int getAccesses() {
        return accesses;
    }

    /**
     * returns an indication on how many object keep an reference to this
     * object. if this number is positive, its only an indication, not a real
     * numer. if its 0, the refcount is 0.
     *
     * @return an indication on how many object keep an reference to this
     *         object.
     */
    public int getRefCount() {
        return refCount;
    }

    /**
     * Resets the refcount.
     */
    public void resetRefCount() {
        refCount = 0;
    }

   

    /**
     * @return
     */
    public CacheRegion getRegion() {
        return region;
    }

    /* (non-Javadoc)
     * @see java.lang.ref.Reference#clear()
     */
    public void clear() {
       //s super.clear();
        System.out.println("cleared:"+key);
    }


}
TOP

Related Classes of org.fjank.jcache.CacheObject

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.