alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.mdr.persistence.btreeimpl.btreestorage;

import java.lang.ref.*;

import org.netbeans.mdr.persistence.*;

/** A member of the MDR cache.  This is a soft reference to an object
* identified by a MOFID.
*/
//class CacheReference extends SoftReference {
// BHM - lets try more memory friendly 
class CacheReference extends WeakReference {

    /* The MOF ID of the object we reference */
    private Object key;
    
    private Object hardRef;

    /* Whether the reference is currently in the cache */
    // boolean inCache;

    /** Create a reference 
    * @param m the MOF ID of the referenced object
    * @param o the referenced object
    * @param q our reference queue
    */
    CacheReference(Object m, Object o, ReferenceQueue q) {
        super(o, q);
        key = m;
    }

    /** A CacheReference is represented by the same string as its MOF ID */
    public String toString() {
        return key.toString();
    }

    /** A CacheReference hashes to the same number as its MOF ID */
    public int hashCode() {
        return key.hashCode();
    }

    /** Two CacheReferences are equal if their MOF IDs are equal */
    public boolean equals(Object o) {
        if (!(o instanceof CacheReference))
            return false;

        CacheReference cr = (CacheReference)o;
        return cr.key.equals(key);
    }
    
    /** Calling this method makes the reference to be a hard reference
     */
    public void harden() {
        hardRef = get();
    }
    
    /** Calling this method makes the reference weak again
     */
    public void weaken() {
        hardRef = null;
    }
    
    /** Returns true if the reference is hard
     */
    public boolean isHard() {
        return hardRef != null;
    }
    
    
    /** Overriden clear to weaken possibly hardened reference
     */
    public void clear() {
        weaken();
        key = null;
        super.clear();
    }
    
    
    /** Returns the key of the referece */
    Object getKey() {
        return key;
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.