KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > variables > LogicalStructureCache


1 /*******************************************************************************
2  * Copyright (c) 2007 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.debug.internal.ui.views.variables;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.debug.core.ILogicalStructureType;
22 import org.eclipse.debug.core.model.IValue;
23 import org.eclipse.debug.internal.ui.DebugUIPlugin;
24
25 /**
26  * Cache that stores evaluated logical structure values to replace raw values. Cache
27  * should be cleared when a RESUME or TERMINATE event is fired so the structure can be
28  * reevaluated for new values.
29  *
30  * @since 3.3
31  *
32  */

33 public class LogicalStructureCache {
34
35     /**
36      * Maps a ILogicalStructureType to the cache for that type
37      */

38     private Map JavaDoc fCacheForType = new HashMap JavaDoc();
39     
40     /**
41      * Returns the logical value to replace the given value using the specified logical structure.
42      * The value will be retrieved from the cache if possible, or evaluated if not.
43      *
44      * @param type the logical structure type used to evaluate the logical value
45      * @param value the raw value to replace with a logical structure
46      * @return the logical value replacing the raw value or <code>null</code> if there is a problem
47      */

48     public IValue getLogicalStructure(ILogicalStructureType type, IValue value) throws CoreException {
49         synchronized (fCacheForType) {
50             LogicalStructureTypeCache cache = getCacheForType(type);
51             return cache.getLogicalStructure(value);
52         }
53     }
54     
55     /**
56      * Clears the cache of all evaluated values.
57      */

58     public void clear(){
59         synchronized (fCacheForType) {
60             fCacheForType.clear();
61         }
62     }
63     
64     /**
65      * Helper method that returns the cache associated with the given logical structure type.
66      * If there is not cache associated, one is created.
67      *
68      * @param type the logical structure type to get the cache for
69      * @return the cache associated with the logical structure type
70      */

71     protected LogicalStructureTypeCache getCacheForType(ILogicalStructureType type){
72         LogicalStructureTypeCache cache = (LogicalStructureTypeCache)fCacheForType.get(type);
73         if (cache == null){
74             cache = new LogicalStructureTypeCache(type);
75             fCacheForType.put(type, cache);
76         }
77         return cache;
78     }
79     
80     /**
81      * Inner class that caches the known and pending values for a given logical
82      * structure type.
83      */

84     class LogicalStructureTypeCache{
85         
86         private ILogicalStructureType fType;
87         
88         /**
89          * Maps a raw IValue to its calculated logical IValue
90          */

91         private Map JavaDoc fKnownValues = new HashMap JavaDoc();
92         
93         /**
94          * Set of raw IValues that logical values are currently being evaluated for.
95          */

96         private Set JavaDoc fPendingValues = new HashSet JavaDoc();
97         
98         public LogicalStructureTypeCache(ILogicalStructureType type){
99             fType = type;
100         }
101         
102         /**
103          * Returns the logical structure value for the given raw value. If the value has been evaluated
104          * the cached value is returned, otherwise the thread waits until the value is evaluated.
105          *
106          * @param value the raw value
107          * @return the logical value
108          * @exception CoreException if an error occurs computing the value
109          */

110         public IValue getLogicalStructure(IValue value) throws CoreException {
111             // Check if the value has already been evaluated
112
synchronized (fKnownValues) {
113                 IValue logical = (IValue)fKnownValues.get(value);
114                 if (logical != null){
115                     return logical;
116                 }
117             }
118             // Check if the logical structure is currently being evaluated
119
synchronized (fPendingValues) {
120                 if (fPendingValues.contains(value)){
121                     try {
122                         fPendingValues.wait();
123                         return getLogicalStructure(value);
124                     } catch (InterruptedException JavaDoc e) {
125                         throw new CoreException(new Status(IStatus.CANCEL, DebugUIPlugin.getUniqueIdentifier(),
126                                 VariablesViewMessages.LogicalStructureCache_0, e));
127                     }
128                 } else {
129                     fPendingValues.add(value);
130                 }
131             }
132             // Start the evaluation to get the logical structure
133
try {
134                 IValue result = fType.getLogicalStructure(value);
135                 synchronized (fKnownValues) {
136                     fKnownValues.put(value, result);
137                 }
138                 synchronized (fPendingValues) {
139                     fPendingValues.remove(value);
140                     fPendingValues.notifyAll();
141                 }
142                 return result;
143             } catch (CoreException e) {
144                 synchronized (fPendingValues) {
145                     fPendingValues.remove(value);
146                     fPendingValues.notifyAll();
147                 }
148                 throw e;
149             }
150         }
151         
152     }
153 }
154
Popular Tags