KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > Cache


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.team.internal.core;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.team.core.ICache;
18 import org.eclipse.team.core.ICacheListener;
19
20 /**
21  * A synchronize operation context that supports caching of
22  * properties relevant to the operation and the registering of
23  * dispose listeners.
24  *
25  * @see org.eclipse.team.core.ICache
26  * @since 3.2
27  */

28 public class Cache implements ICache {
29
30     Map JavaDoc properties;
31     ListenerList listeners;
32     
33     /* (non-Javadoc)
34      * @see org.eclipse.team.ui.mapping.ISynchronizeOperationContext#addProperty(java.lang.String, java.lang.Object)
35      */

36     public synchronized void put(String JavaDoc name, Object JavaDoc value) {
37         if (properties == null) {
38             properties = new HashMap JavaDoc();
39         }
40         properties.put(name, value);
41     }
42
43     public synchronized Object JavaDoc get(String JavaDoc name) {
44         if (properties == null)
45             return null;
46         return properties.get(name);
47     }
48
49     /* (non-Javadoc)
50      * @see org.eclipse.team.ui.mapping.ISynchronizeOperationContext#removeProperty(java.lang.String)
51      */

52     public synchronized void remove(String JavaDoc name) {
53         if (properties != null)
54             properties.remove(name);
55         if (properties.isEmpty()) {
56             properties = null;
57         }
58         
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.team.ui.mapping.ISynchronizeOperationContext#addDisposeListener(org.eclipse.team.ui.mapping.IDisposeListener)
63      */

64     public synchronized void addCacheListener(ICacheListener listener) {
65         if (listeners == null)
66             listeners = new ListenerList(ListenerList.IDENTITY);
67         listeners.add(listener);
68         
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.team.ui.mapping.ISynchronizeOperationContext#removeDisposeListener(org.eclipse.team.ui.mapping.IDisposeListener)
73      */

74     public synchronized void removeDisposeListener(ICacheListener listener) {
75         removeCacheListener(listener);
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.team.core.ICache#removeCacheListener(org.eclipse.team.core.ICacheListener)
80      */

81     public synchronized void removeCacheListener(ICacheListener listener) {
82         if (listeners != null)
83             listeners.remove(listener);
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.team.ui.mapping.ISynchronizationContext#dispose()
88      */

89     public void dispose() {
90         if (listeners != null) {
91             Object JavaDoc[] allListeners = listeners.getListeners();
92             for (int i = 0; i < allListeners.length; i++) {
93                 final Object JavaDoc listener = allListeners[i];
94                 SafeRunner.run(new ISafeRunnable(){
95                     public void run() throws Exception JavaDoc {
96                         ((ICacheListener)listener).cacheDisposed(Cache.this);
97                     }
98                     public void handleException(Throwable JavaDoc exception) {
99                         // Ignore since the platform logs the error
100

101                     }
102                 });
103             }
104         }
105         properties = null;
106     }
107     
108 }
109
Popular Tags