01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.project.internal.interceptor;
16:
17: import java.util.HashMap;
18: import java.util.Map;
19:
20: import net.refractions.udig.catalog.IGeoResource;
21: import net.refractions.udig.project.ILayer;
22: import net.refractions.udig.project.IResourceCachingInterceptor;
23:
24: /**
25: * Caches all resources and returns the cached instance.
26: *
27: * @author Jesse
28: * @since 1.1.0
29: */
30: public class ResourceCacheInterceptor implements
31: IResourceCachingInterceptor {
32:
33: public static final String ID = "net.refractions.udig.project.caching"; //$NON-NLS-1$
34: private Map<Class, Object> resources = new HashMap<Class, Object>();
35:
36: @SuppressWarnings("unchecked")
37: private <T> void registerClasses(Class<T> clazz, Object obj) {
38: if (clazz.getSuperclass() != null
39: && clazz.getSuperclass() != Object.class)
40: registerClasses(clazz.getSuperclass(), obj);
41: for (int i = 0; i < clazz.getInterfaces().length; i++) {
42: registerClasses(clazz.getInterfaces()[i], obj);
43: }
44: resources.put(clazz, obj);
45: }
46:
47: @SuppressWarnings("unchecked")
48: public <T> T get(ILayer layer, Class<T> requestedType) {
49: return (T) resources.get(requestedType);
50: }
51:
52: public <T> boolean isCached(ILayer layer, IGeoResource resource,
53: Class<T> requestedType) {
54: // return resources.containsKey(resource.getClass());
55: return resources.containsKey(requestedType);
56: }
57:
58: public <T> void put(ILayer layer, T resource, Class<T> requestedType) {
59: if (resource != null)
60: registerClasses(resource.getClass(), resource);
61: }
62:
63: }
|