KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > resource > AbstractResourceManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.resource;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * Abstract implementation of ResourceManager. Maintains reference counts for all previously
20  * allocated SWT resources. Delegates to the abstract method allocate(...) the first time a resource
21  * is referenced and delegates to the abstract method deallocate(...) the last time a reference is
22  * removed.
23  *
24  * @since 3.1
25  */

26 abstract class AbstractResourceManager extends ResourceManager {
27
28     /**
29      * Map of ResourceDescriptor onto RefCount. (null when empty)
30      */

31     private HashMap JavaDoc map = null;
32     
33     /**
34      * Holds a reference count for a previously-allocated resource
35      */

36     private static class RefCount {
37         Object JavaDoc resource;
38         int count = 1;
39         
40         RefCount(Object JavaDoc resource) {
41             this.resource = resource;
42         }
43     }
44     
45     /**
46      * Called the first time a resource is requested. Should allocate and return a resource
47      * of the correct type.
48      *
49      * @since 3.1
50      *
51      * @param descriptor identifier for the resource to allocate
52      * @return the newly allocated resource
53      * @throws DeviceResourceException Thrown when allocation of an SWT device resource fails
54      */

55     protected abstract Object JavaDoc allocate(DeviceResourceDescriptor descriptor) throws DeviceResourceException;
56
57     /**
58      * Called the last time a resource is dereferenced. Should release any resources reserved by
59      * allocate(...).
60      *
61      * @since 3.1
62      *
63      * @param resource resource being deallocated
64      * @param descriptor identifier for the resource
65      */

66     protected abstract void deallocate(Object JavaDoc resource, DeviceResourceDescriptor descriptor);
67     
68     /* (non-Javadoc)
69      * @see ResourceManager#create(DeviceResourceDescriptor)
70      */

71     public final Object JavaDoc create(DeviceResourceDescriptor descriptor) throws DeviceResourceException {
72
73         // Lazily allocate the map
74
if (map == null) {
75             map = new HashMap JavaDoc();
76         }
77         
78         // Get the current reference count
79
RefCount count = (RefCount)map.get(descriptor);
80         if (count != null) {
81             // If this resource already exists, increment the reference count and return
82
// the existing resource.
83
count.count++;
84             return count.resource;
85         }
86         
87         // Allocate and return a new resource (with ref count = 1)
88
Object JavaDoc resource = allocate(descriptor);
89         
90         count = new RefCount(resource);
91         map.put(descriptor, count);
92         
93         return resource;
94     }
95
96     /* (non-Javadoc)
97      * @see ResourceManager#destroy(DeviceResourceDescriptor)
98      */

99     public final void destroy(DeviceResourceDescriptor descriptor) {
100         // If the map is empty (null) then there are no resources to dispose
101
if (map == null) {
102             return;
103         }
104         
105         // Find the existing resource
106
RefCount count = (RefCount)map.get(descriptor);
107         if (count != null) {
108             // If the resource exists, decrement the reference count.
109
count.count--;
110             if (count.count == 0) {
111                 // If this was the last reference, deallocate it.
112
deallocate(count.resource, descriptor);
113                 map.remove(descriptor);
114             }
115         }
116         
117         // Null out the map when empty to save a small amount of memory
118
if (map.isEmpty()) {
119             map = null;
120         }
121     }
122
123     /**
124      * Deallocates any resources allocated by this registry that have not yet been
125      * deallocated.
126      *
127      * @since 3.1
128      */

129     public void dispose() {
130         super.dispose();
131         
132         if (map == null) {
133             return;
134         }
135         
136         Collection JavaDoc entries = map.entrySet();
137
138         for (Iterator JavaDoc iter = entries.iterator(); iter.hasNext();) {
139             Map.Entry JavaDoc next = (Map.Entry JavaDoc) iter.next();
140             
141             Object JavaDoc key = next.getKey();
142             RefCount val = (RefCount)next.getValue();
143             
144             deallocate(val.resource, (DeviceResourceDescriptor)key);
145         }
146         
147         map = null;
148     }
149     
150     /* (non-Javadoc)
151      * @see org.eclipse.jface.resource.ResourceManager#find(org.eclipse.jface.resource.DeviceResourceDescriptor)
152      */

153     public Object JavaDoc 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