1 12 13 package org.eclipse.core.databinding.observable.map; 14 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.databinding.observable.Diffs; 23 import org.eclipse.core.databinding.observable.Realm; 24 25 34 public class WritableMap extends ObservableMap { 35 36 39 public WritableMap() { 40 this(Realm.getDefault()); 41 } 42 43 46 public WritableMap(Realm realm) { 47 super(realm, new HashMap ()); 48 } 49 50 53 public Object put(Object key, Object value) { 54 checkRealm(); 55 Object 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 67 public Object remove(Object key) { 68 checkRealm(); 69 Object result = wrappedMap.remove(key); 70 if (result!=null) { 71 fireMapChange(Diffs.createMapDiffSingleRemove(key, result)); 72 } 73 return result; 74 } 75 76 79 public void clear() { 80 checkRealm(); 81 Map copy = new HashMap (wrappedMap.size()); 82 copy.putAll(wrappedMap); 83 wrappedMap.clear(); 84 fireMapChange(Diffs.createMapDiffRemoveAll(copy)); 85 } 86 87 90 public void putAll(Map map) { 91 checkRealm(); 92 Set addedKeys = new HashSet (map.size()); 93 Map changes = new HashMap (map.size()); 94 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 95 Map.Entry entry = (Entry) it.next(); 96 Object 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 |