KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > map > WritableMap


1 /*******************************************************************************
2  * Copyright (c) 2006, 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  * Brad Reynolds - bug 164653
11  *******************************************************************************/

12
13 package org.eclipse.core.databinding.observable.map;
14
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.databinding.observable.Diffs;
23 import org.eclipse.core.databinding.observable.Realm;
24
25 /**
26  *
27  * <p>
28  * This class is thread safe. All state accessing methods must be invoked from
29  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
30  * listeners may be invoked from any thread.
31  * </p>
32  * @since 1.0
33  */

34 public class WritableMap extends ObservableMap {
35
36     /**
37      *
38      */

39     public WritableMap() {
40         this(Realm.getDefault());
41     }
42     
43     /**
44      * @param realm
45      */

46     public WritableMap(Realm realm) {
47         super(realm, new HashMap JavaDoc());
48     }
49
50     /**
51      * Associates the provided <code>value</code> with the <code>key</code>. Must be invoked from the current realm.
52      */

53     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
54         checkRealm();
55         Object JavaDoc result = wrappedMap.put(key, value);
56         if (result==null) {
57             fireMapChange(Diffs.createMapDiffSingleAdd(key, value));
58         } else {
59             fireMapChange(Diffs.createMapDiffSingleChange(key, result, value));
60         }
61         return result;
62     }
63
64     /**
65      * Removes the value with the provide <code>key</code>. Must be invoked from the current realm.
66      */

67     public Object JavaDoc remove(Object JavaDoc key) {
68         checkRealm();
69         Object JavaDoc result = wrappedMap.remove(key);
70         if (result!=null) {
71             fireMapChange(Diffs.createMapDiffSingleRemove(key, result));
72         }
73         return result;
74     }
75
76     /**
77      * Clears the map. Must be invoked from the current realm.
78      */

79     public void clear() {
80         checkRealm();
81         Map JavaDoc copy = new HashMap JavaDoc(wrappedMap.size());
82         copy.putAll(wrappedMap);
83         wrappedMap.clear();
84         fireMapChange(Diffs.createMapDiffRemoveAll(copy));
85     }
86
87     /**
88      * Adds the provided <code>map</code>'s contents to this map. Must be invoked from the current realm.
89      */

90     public void putAll(Map JavaDoc map) {
91         checkRealm();
92         Set JavaDoc addedKeys = new HashSet JavaDoc(map.size());
93         Map JavaDoc changes = new HashMap JavaDoc(map.size());
94         for (Iterator JavaDoc it = map.entrySet().iterator(); it.hasNext();) {
95             Map.Entry JavaDoc entry = (Entry) it.next();
96             Object JavaDoc previousValue = wrappedMap.put(entry.getKey(), entry.getValue());
97             if (previousValue==null) {
98                 addedKeys.add(entry.getKey());
99             } else {
100                 changes.put(entry.getKey(), previousValue);
101             }
102         }
103         fireMapChange(Diffs.createMapDiff(addedKeys, Collections.EMPTY_SET, changes.keySet(), changes, wrappedMap));
104     }
105
106 }
107
Popular Tags