78
|
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 *******************************************************************************/
|
|
11
|
|
12 module org.eclipse.core.databinding.observable.map.ComputedObservableMap;
|
81
|
13 import org.eclipse.core.databinding.observable.map.AbstractObservableMap;
|
78
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.util.AbstractSet;
|
|
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.set.IObservableSet;
|
|
27 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
|
|
28 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
|
|
29
|
|
30 /**
|
|
31 * Maps objects to one of their attributes. Tracks changes to the underlying
|
|
32 * observable set of objects (keys), as well as changes to attribute values.
|
|
33 */
|
|
34 public abstract class ComputedObservableMap : AbstractObservableMap {
|
|
35
|
85
|
36 private final IObservableSet fkeySet;
|
78
|
37
|
85
|
38 private ISetChangeListener setChangeListener;
|
|
39 class SetChangeListener : ISetChangeListener {
|
78
|
40 public void handleSetChange(SetChangeEvent event) {
|
|
41 Set addedKeys = new HashSet(event.diff.getAdditions());
|
|
42 Set removedKeys = new HashSet(event.diff.getRemovals());
|
|
43 Map oldValues = new HashMap();
|
|
44 Map newValues = new HashMap();
|
|
45 for (Iterator it = removedKeys.iterator(); it.hasNext();) {
|
|
46 Object removedKey = it.next();
|
|
47 Object oldValue = doGet(removedKey);
|
|
48 unhookListener(removedKey);
|
|
49 if (oldValue !is null) {
|
|
50 oldValues.put(removedKey, oldValue);
|
|
51 }
|
|
52 }
|
|
53 for (Iterator it = addedKeys.iterator(); it.hasNext();) {
|
|
54 Object addedKey = it.next();
|
|
55 hookListener(addedKey);
|
|
56 Object newValue = doGet(addedKey);
|
|
57 newValues.put(addedKey, newValue);
|
|
58 }
|
|
59 fireMapChange(Diffs.createMapDiff(addedKeys, removedKeys,
|
|
60 Collections.EMPTY_SET, oldValues, newValues));
|
|
61 }
|
|
62 };
|
|
63
|
85
|
64 private Set fentrySet;
|
78
|
65
|
|
66 private class EntrySet : AbstractSet {
|
|
67
|
|
68 public Iterator iterator() {
|
|
69 return new class() Iterator {
|
|
70
|
|
71 final Iterator keyIterator;
|
|
72 this(){
|
85
|
73 keyIterator = fkeySet.iterator();
|
78
|
74 }
|
|
75
|
|
76 public bool hasNext() {
|
|
77 return keyIterator.hasNext();
|
|
78 }
|
|
79
|
|
80 public Object next() {
|
|
81 return new class() Map.Entry {
|
|
82 Object key;
|
|
83 this(){
|
|
84 key = keyIterator.next();
|
|
85 }
|
|
86
|
|
87 public Object getKey() {
|
|
88 return key;
|
|
89 }
|
|
90
|
|
91 public Object getValue() {
|
|
92 return get(getKey());
|
|
93 }
|
|
94
|
|
95 public Object setValue(Object value) {
|
|
96 return put(getKey(), value);
|
|
97 }
|
85
|
98
|
|
99 public override equals_t opEquals(Object o){
|
|
100 implMissing( __FILE__, __LINE__ );
|
|
101 return 0;
|
|
102 }
|
|
103 public override hash_t toHash(){
|
|
104 implMissing( __FILE__, __LINE__ );
|
|
105 return 0;
|
|
106 }
|
|
107
|
78
|
108 };
|
|
109 }
|
|
110
|
|
111 public void remove() {
|
|
112 keyIterator.remove();
|
|
113 }
|
|
114 };
|
|
115 }
|
|
116
|
|
117 public int size() {
|
85
|
118 return fkeySet.size();
|
78
|
119 }
|
|
120
|
85
|
121 public override String toString(){
|
|
122 return super.toString();
|
|
123 }
|
78
|
124 }
|
|
125
|
|
126 /**
|
|
127 * @param keySet
|
|
128 */
|
85
|
129 public this(IObservableSet _keySet) {
|
|
130 setChangeListener = new SetChangeListener();
|
|
131 fentrySet = new EntrySet();
|
|
132 super(_keySet.getRealm());
|
|
133 this.fkeySet = _keySet;
|
|
134 this.fkeySet.addSetChangeListener(setChangeListener);
|
78
|
135 }
|
|
136
|
|
137 protected void init() {
|
85
|
138 for (Iterator it = this.fkeySet.iterator(); it.hasNext();) {
|
78
|
139 Object key = it.next();
|
|
140 hookListener(key);
|
|
141 }
|
|
142 }
|
|
143
|
|
144 protected final void fireSingleChange(Object key, Object oldValue,
|
|
145 Object newValue) {
|
|
146 fireMapChange(Diffs.createMapDiffSingleChange(key, oldValue, newValue));
|
|
147 }
|
|
148
|
|
149 public Set entrySet() {
|
85
|
150 return fentrySet;
|
78
|
151 }
|
|
152
|
|
153 public Set keySet() {
|
85
|
154 return fkeySet;
|
78
|
155 }
|
|
156
|
|
157 final public Object get(Object key) {
|
|
158 return doGet(key);
|
|
159 }
|
|
160
|
|
161 final public Object put(Object key, Object value) {
|
|
162 return doPut(key, value);
|
|
163 }
|
|
164
|
|
165 /**
|
|
166 * @param removedKey
|
|
167 */
|
|
168 protected abstract void unhookListener(Object removedKey);
|
|
169
|
|
170 /**
|
|
171 * @param addedKey
|
|
172 */
|
|
173 protected abstract void hookListener(Object addedKey);
|
|
174
|
|
175 /**
|
|
176 * @param key
|
|
177 * @return the value for the given key
|
|
178 */
|
|
179 protected abstract Object doGet(Object key);
|
|
180
|
|
181 /**
|
|
182 * @param key
|
|
183 * @param value
|
|
184 * @return the old value for the given key
|
|
185 */
|
|
186 protected abstract Object doPut(Object key, Object value);
|
|
187 }
|