|
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-2003 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.masterfs;
import org.openide.filesystems.FileObject;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.*;
/**
* Provides caching management for MasterFileObjects, that should be never
* constructed directly (instead should be used method getOrCreate).
*
* Neither children references nor parent reference are kept.
*
* @author Radek Matous
*/
final class Cache {
/** maps */
private Map res2DfoMap;
private static Cache instance;
/***
* @return one shared instance of Cache
*/
static Cache getDefault() {
synchronized (Cache.class) {
if (instance == null) {
instance = new Cache();
instance.res2DfoMap = Collections.synchronizedMap(new WeakHashMap());
}
}
return instance;
}
private Cache() {
}
/**
* Looks up in cache MasterFileObject identified with resPath
* @param resPath identifies MasterFileObject
* @return instance of MasterFileObject if exists in cache or null
*/
MasterFileObject get(final ResourcePath resPath) {
//resPath = getNormalizedPath(resPath);
MasterFileObject retVal = getValidOrInvalid(resPath);
if (retVal != null)
retVal = (retVal.isValid()) ? retVal : null;
return retVal;
}
MasterFileObject getValidOrInvalid(final ResourcePath resPath) {
final Reference ref = (Reference) res2DfoMap.get(resPath.getNormalizedPath());
MasterFileObject retVal = null;
if (ref != null)
retVal = (MasterFileObject) ref.get();
return retVal;
}
/**
* Looks up in cache MasterFileObject identified with resPath. If there
* is no appropriate object in cache, then new one is created, but only if
* delegate can be found
* @param resPath
* @return MasterFileObject or null, if delegate can't be resolved
*/
MasterFileObject getOrCreate(final ResourcePath resPath) {
final MasterFileObject retVal = get(resPath);
if (retVal != null) return retVal;
FileObject delegate = Delegate.resolve(resPath);
return (delegate == null) ? null : getOrCreate(resPath, delegate);
}
/**
* Looks up in cache MasterFileObject identified with resPath. If there
* is no appropriate object in cache, then new one is created, but only if
* delegate can be found
* @param resPath identification of MasterFileObject
* @param delegate
* @return MasterFileObject
*/
MasterFileObject getOrCreate(final ResourcePath resPath, final FileObject delegate) {
MasterFileObject retVal = get(resPath);
if (retVal != null /*&& retVal.isValid ()!retVal.isValid()*/)
return retVal;
retVal = new MasterFileObject(resPath, delegate);
return put(retVal);
}
/**
* @return all objects from cache as enumeration
*/
Enumeration getAll() {
final ArrayList arrayList = new ArrayList();
synchronized (res2DfoMap) {
final Iterator it = res2DfoMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Reference ref = (Reference) entry.getValue();
if (ref != null) {
MasterFileObject hfo = (MasterFileObject) ref.get();
if (hfo != null && hfo.getDelegate().isValid())
arrayList.add(hfo);
}
}
}
final Object[] array = new Object[arrayList.size()];
arrayList.toArray(array);
return org.openide.util.Enumerations.array (array);
}
void clear() {
res2DfoMap.clear();
}
private MasterFileObject put(final MasterFileObject hfo) {
MasterFileObject retVal = hfo;
synchronized (res2DfoMap) {
final MasterFileObject test = get(hfo.getResource());
if (test == null || !test.isValid())
res2DfoMap.put(hfo.getResource().getNormalizedPath(), createReference(hfo));
else
retVal = test;
}
return retVal;
}
private static Reference createReference(final MasterFileObject hfo) {
return new SoftReference(hfo);
//return new WeakReference(hfo);
}
/**
* Replaces original MasterFileObject in cache with new one
* @param oldResPath identifies the old one MasterFileObject in cache
* @param newObject that replaces the original one
*/
void replace(final String oldResPath, final MasterFileObject newObject) {
synchronized (res2DfoMap) {
final MasterFileObject test = get(new ResourcePath(oldResPath));
if (test != null) res2DfoMap.remove(oldResPath);
put(newObject);
}
}
}
|