comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/map/WritableMap.d @ 78:0a55d2d5a946

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