1 19 20 package org.netbeans.modules.masterfs; 21 22 import org.openide.filesystems.FileObject; 23 24 import java.lang.ref.Reference ; 25 import java.lang.ref.SoftReference ; 26 import java.lang.ref.WeakReference ; 27 import java.util.*; 28 29 37 final class Cache { 38 39 private Map res2DfoMap; 40 private static Cache instance; 41 42 45 static Cache getDefault() { 46 synchronized (Cache.class) { 47 if (instance == null) { 48 instance = new Cache(); 49 instance.res2DfoMap = Collections.synchronizedMap(new WeakHashMap()); 50 } 51 } 52 return instance; 53 } 54 55 private Cache() { 56 } 57 58 63 MasterFileObject get(final ResourcePath resPath) { 64 MasterFileObject retVal = getValidOrInvalid(resPath); 66 67 if (retVal != null) 68 retVal = (retVal.isValid()) ? retVal : null; 69 70 return retVal; 71 } 72 73 MasterFileObject getValidOrInvalid(final ResourcePath resPath) { 74 final Reference ref = (Reference ) res2DfoMap.get(resPath.getNormalizedPath()); 75 MasterFileObject retVal = null; 76 if (ref != null) 77 retVal = (MasterFileObject) ref.get(); 78 return retVal; 79 } 80 81 88 MasterFileObject getOrCreate(final ResourcePath resPath) { 89 final MasterFileObject retVal = get(resPath); 90 if (retVal != null) return retVal; 91 92 FileObject delegate = Delegate.resolve(resPath); 93 return (delegate == null) ? null : getOrCreate(resPath, delegate); 94 } 95 96 104 MasterFileObject getOrCreate(final ResourcePath resPath, final FileObject delegate) { 105 MasterFileObject retVal = getValidOrInvalid(resPath); 106 boolean isRetValValid = (retVal != null && !retVal.isValid()) ? false : true; 107 108 if (retVal != null && isRetValValid) { 109 return retVal; 110 } 111 112 MasterFileObject nRetVal = new MasterFileObject(resPath, delegate); 113 if (nRetVal.isValid()) { 114 retVal = nRetVal; 115 put(retVal); 116 } 117 118 return retVal; 119 } 120 121 124 Enumeration getAll() { 125 final ArrayList arrayList = new ArrayList(); 126 synchronized (res2DfoMap) { 127 final Iterator it = res2DfoMap.entrySet().iterator(); 128 while (it.hasNext()) { 129 Map.Entry entry = (Map.Entry) it.next(); 130 Reference ref = (Reference ) entry.getValue(); 131 if (ref != null) { 132 MasterFileObject hfo = (MasterFileObject) ref.get(); 133 if (hfo != null && hfo.getDelegate().isValid()) 134 arrayList.add(hfo); 135 } 136 } 137 } 138 final Object [] array = new Object [arrayList.size()]; 139 arrayList.toArray(array); 140 return org.openide.util.Enumerations.array (array); 141 } 142 143 void clear() { 144 res2DfoMap.clear(); 145 } 146 147 private MasterFileObject put(final MasterFileObject hfo) { 148 MasterFileObject retVal = hfo; 149 synchronized (res2DfoMap) { 150 final MasterFileObject test = get(hfo.getResource()); 151 if (test == null || !test.isValid()) { 152 res2DfoMap.remove(hfo.getResource().getNormalizedPath()); 153 res2DfoMap.put(hfo.getResource().getNormalizedPath(), createReference(hfo)); 154 } else { 155 retVal = test; 156 } 157 } 158 return retVal; 159 } 160 161 private static Reference createReference(final MasterFileObject hfo) { 162 return new SoftReference (hfo); 163 } 165 166 171 void replace(final String oldResPath, final MasterFileObject newObject) { 172 synchronized (res2DfoMap) { 173 final MasterFileObject test = getValidOrInvalid(new ResourcePath(oldResPath)); 174 if (test != null) res2DfoMap.remove(oldResPath); 175 put(newObject); 176 } 177 } 178 179 180 } 181 | Popular Tags |