1 11 package org.eclipse.jface.resource; 12 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 26 abstract class AbstractResourceManager extends ResourceManager { 27 28 31 private HashMap map = null; 32 33 36 private static class RefCount { 37 Object resource; 38 int count = 1; 39 40 RefCount(Object resource) { 41 this.resource = resource; 42 } 43 } 44 45 55 protected abstract Object allocate(DeviceResourceDescriptor descriptor) throws DeviceResourceException; 56 57 66 protected abstract void deallocate(Object resource, DeviceResourceDescriptor descriptor); 67 68 71 public final Object create(DeviceResourceDescriptor descriptor) throws DeviceResourceException { 72 73 if (map == null) { 75 map = new HashMap (); 76 } 77 78 RefCount count = (RefCount)map.get(descriptor); 80 if (count != null) { 81 count.count++; 84 return count.resource; 85 } 86 87 Object resource = allocate(descriptor); 89 90 count = new RefCount(resource); 91 map.put(descriptor, count); 92 93 return resource; 94 } 95 96 99 public final void destroy(DeviceResourceDescriptor descriptor) { 100 if (map == null) { 102 return; 103 } 104 105 RefCount count = (RefCount)map.get(descriptor); 107 if (count != null) { 108 count.count--; 110 if (count.count == 0) { 111 deallocate(count.resource, descriptor); 113 map.remove(descriptor); 114 } 115 } 116 117 if (map.isEmpty()) { 119 map = null; 120 } 121 } 122 123 129 public void dispose() { 130 super.dispose(); 131 132 if (map == null) { 133 return; 134 } 135 136 Collection entries = map.entrySet(); 137 138 for (Iterator iter = entries.iterator(); iter.hasNext();) { 139 Map.Entry next = (Map.Entry ) iter.next(); 140 141 Object key = next.getKey(); 142 RefCount val = (RefCount)next.getValue(); 143 144 deallocate(val.resource, (DeviceResourceDescriptor)key); 145 } 146 147 map = null; 148 } 149 150 153 public Object find(DeviceResourceDescriptor descriptor) { 154 if (map == null) { 155 return null; 156 } 157 RefCount refCount = (RefCount)map.get(descriptor); 158 if (refCount == null) 159 return null; 160 return refCount.resource; 161 } 162 } 163 | Popular Tags |