78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006-2008 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 * Matthew Hall - bug 184830
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module org.eclipse.core.databinding.observable.map.WritableMap;
|
81
|
15 import org.eclipse.core.databinding.observable.map.ObservableMap;
|
78
|
16
|
|
17 import java.lang.all;
|
|
18
|
|
19 import java.util.Collections;
|
|
20 import java.util.HashMap;
|
|
21 import java.util.HashSet;
|
|
22 import java.util.Iterator;
|
|
23 import java.util.Map;
|
|
24 import java.util.Set;
|
|
25
|
|
26 import org.eclipse.core.databinding.observable.Diffs;
|
|
27 import org.eclipse.core.databinding.observable.Realm;
|
|
28 import org.eclipse.core.internal.databinding.Util;
|
|
29
|
|
30 /**
|
|
31 *
|
|
32 * <p>
|
|
33 * This class is thread safe. All state accessing methods must be invoked from
|
|
34 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
35 * listeners may be invoked from any thread.
|
|
36 * </p>
|
|
37 * @since 1.0
|
|
38 */
|
|
39 public class WritableMap : ObservableMap {
|
|
40
|
|
41 /**
|
|
42 * Constructs a new WritableMap on the default realm.
|
|
43 */
|
|
44 public this() {
|
|
45 this(Realm.getDefault());
|
|
46 }
|
|
47
|
|
48 /**
|
|
49 * Constructs a new WritableMap on the given realm.
|
|
50 *
|
|
51 * @param realm
|
|
52 * the realm
|
|
53 */
|
|
54 public this(Realm realm) {
|
|
55 super(realm, new HashMap());
|
|
56 }
|
|
57
|
|
58 /**
|
|
59 * Associates the provided <code>value</code> with the <code>key</code>. Must be invoked from the current realm.
|
|
60 */
|
|
61 public Object put(Object key, Object value) {
|
|
62 checkRealm();
|
|
63 Object result = wrappedMap.put(key, value);
|
|
64 if (!Util.equals(result, value)) {
|
|
65 if (resultisnull) {
|
|
66 fireMapChange(Diffs.createMapDiffSingleAdd(key, value));
|
|
67 } else {
|
|
68 fireMapChange(Diffs.createMapDiffSingleChange(key, result,
|
|
69 value));
|
|
70 }
|
|
71 }
|
|
72 return result;
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * Removes the value with the provide <code>key</code>. Must be invoked from the current realm.
|
|
77 */
|
|
78 public Object remove(Object key) {
|
|
79 checkRealm();
|
|
80 Object result = wrappedMap.remove(key);
|
|
81 if (result !is null) {
|
|
82 fireMapChange(Diffs.createMapDiffSingleRemove(key, result));
|
|
83 }
|
|
84 return result;
|
|
85 }
|
|
86
|
|
87 /**
|
|
88 * Clears the map. Must be invoked from the current realm.
|
|
89 */
|
|
90 public void clear() {
|
|
91 checkRealm();
|
|
92 if (!isEmpty()) {
|
|
93 Map copy = new HashMap(wrappedMap);
|
|
94 wrappedMap.clear();
|
|
95 fireMapChange(Diffs.createMapDiffRemoveAll(copy));
|
|
96 }
|
|
97 }
|
|
98
|
|
99 /**
|
|
100 * Adds the provided <code>map</code>'s contents to this map. Must be invoked from the current realm.
|
|
101 */
|
|
102 public void putAll(Map map) {
|
|
103 checkRealm();
|
|
104 Set addedKeys = new HashSet(map.size());
|
|
105 Map changes = new HashMap(map.size());
|
|
106 for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
|
|
107 Map.Entry entry = cast(Entry) it.next();
|
|
108 Object previousValue = wrappedMap.put(entry.getKey(), entry.getValue());
|
|
109 if (previousValueisnull) {
|
|
110 addedKeys.add(entry.getKey());
|
|
111 } else {
|
|
112 changes.put(entry.getKey(), previousValue);
|
|
113 }
|
|
114 }
|
|
115 if (!addedKeys.isEmpty() || !changes.isEmpty()) {
|
|
116 fireMapChange(Diffs.createMapDiff(addedKeys, Collections.EMPTY_SET, changes.keySet(), changes, wrappedMap));
|
|
117 }
|
|
118 }
|
|
119
|
|
120 }
|