comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/databinding/observable/map/WritableMap.d @ 95:6208d4f6a277

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