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 * Brad Reynolds - bug 164653
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module org.eclipse.core.databinding.observable.map.ObservableMap;
|
81
|
14 import org.eclipse.core.databinding.observable.map.MapDiff;
|
|
15 import org.eclipse.core.databinding.observable.map.MapChangeEvent;
|
|
16 import org.eclipse.core.databinding.observable.map.IMapChangeListener;
|
|
17 import org.eclipse.core.databinding.observable.map.IObservableMap;
|
78
|
18
|
|
19 import java.lang.all;
|
|
20
|
|
21 import java.util.Collection;
|
|
22 import java.util.Map;
|
|
23 import java.util.Set;
|
|
24
|
85
|
25 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
26 import org.eclipse.core.databinding.observable.IStaleListener;
|
78
|
27 import org.eclipse.core.databinding.observable.AbstractObservable;
|
|
28 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
29 import org.eclipse.core.databinding.observable.Realm;
|
|
30
|
|
31 /**
|
|
32 *
|
|
33 * <p>
|
|
34 * This class is thread safe. All state accessing methods must be invoked from
|
|
35 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
36 * listeners may be invoked from any thread.
|
|
37 * </p>
|
|
38 * @since 1.0
|
|
39 */
|
|
40 public class ObservableMap : AbstractObservable , IObservableMap {
|
85
|
41 // DWT start: java.util.Map additional methods
|
|
42 public bool containsKey(String key) {
|
|
43 return containsKey(stringcast(key));
|
|
44 }
|
|
45 public Object get(String key){
|
|
46 return get(stringcast(key));
|
|
47 }
|
|
48 public Object put(String key, Object value){
|
|
49 return put(stringcast(key), value);
|
|
50 }
|
|
51 public Object put(Object key, String value){
|
|
52 return put(key, stringcast(value));
|
|
53 }
|
|
54 public Object put(String key, String value){
|
|
55 return put(stringcast(key), stringcast(value));
|
|
56 }
|
|
57 public Object remove(String key){
|
|
58 return remove(stringcast(key));
|
|
59 }
|
|
60 public int opApply (int delegate(ref Object value) dg){
|
|
61 foreach( entry; entrySet() ){
|
|
62 auto me = cast(Map.Entry)entry;
|
|
63 auto v = me.getValue();
|
|
64 int res = dg( v );
|
|
65 if( res ) return res;
|
|
66 }
|
|
67 return 0;
|
|
68 }
|
|
69 public int opApply (int delegate(ref Object key, ref Object value) dg){
|
|
70 foreach( entry; entrySet() ){
|
|
71 auto me = cast(Map.Entry)entry;
|
|
72 auto k = me.getKey();
|
|
73 auto v = me.getValue();
|
|
74 int res = dg( k, v );
|
|
75 if( res ) return res;
|
|
76 }
|
|
77 return 0;
|
|
78 }
|
|
79 // DWT end: java.util.Map additional methods
|
|
80 // DWT start reimpl super meths
|
|
81 public override Realm getRealm() {
|
|
82 return super.getRealm();
|
|
83 }
|
|
84 public override void addChangeListener(IChangeListener listener) {
|
|
85 super.addChangeListener(listener);
|
|
86 }
|
|
87 public override void addStaleListener(IStaleListener listener) {
|
|
88 super.addStaleListener(listener);
|
|
89 }
|
|
90 public override void removeChangeListener(IChangeListener listener) {
|
|
91 super.removeChangeListener(listener);
|
|
92 }
|
|
93 public override void removeStaleListener(IStaleListener listener) {
|
|
94 super.removeStaleListener(listener);
|
|
95 }
|
|
96 public override hash_t toHash(){
|
|
97 return super.toHash();
|
|
98 }
|
|
99 public equals_t opEquals( Object o){
|
|
100 if( ObservableMap other = cast(ObservableMap)o){
|
|
101 return cast(equals_t)entrySet().opEquals( cast(Object) other.entrySet() );
|
|
102 }
|
|
103 return false;
|
|
104 }
|
|
105 // DWT end reimpl super meths
|
78
|
106
|
|
107 protected Map wrappedMap;
|
|
108
|
|
109 private bool stale = false;
|
|
110
|
|
111 /**
|
|
112 * @param wrappedMap
|
|
113 */
|
|
114 public this(Map wrappedMap) {
|
|
115 this(Realm.getDefault(), wrappedMap);
|
|
116 }
|
|
117
|
|
118 /**
|
|
119 * @param realm
|
|
120 * @param wrappedMap
|
|
121 */
|
|
122 public this(Realm realm, Map wrappedMap) {
|
|
123 super(realm);
|
|
124 this.wrappedMap = wrappedMap;
|
|
125 }
|
|
126
|
|
127 public synchronized void addMapChangeListener(IMapChangeListener listener) {
|
|
128 addListener(MapChangeEvent.TYPE, listener);
|
|
129 }
|
|
130
|
|
131 public synchronized void removeMapChangeListener(IMapChangeListener listener) {
|
|
132 removeListener(MapChangeEvent.TYPE, listener);
|
|
133 }
|
|
134
|
|
135 protected void getterCalled() {
|
|
136 ObservableTracker.getterCalled(this);
|
|
137 }
|
|
138
|
|
139 protected void fireMapChange(MapDiff diff) {
|
|
140 checkRealm();
|
|
141
|
|
142 // fire general change event first
|
|
143 super.fireChange();
|
|
144
|
|
145 fireEvent(new MapChangeEvent(this, diff));
|
|
146 }
|
|
147
|
|
148 public bool containsKey(Object key) {
|
|
149 getterCalled();
|
|
150 return wrappedMap.containsKey(key);
|
|
151 }
|
|
152
|
|
153 public bool containsValue(Object value) {
|
|
154 getterCalled();
|
|
155 return wrappedMap.containsValue(value);
|
|
156 }
|
|
157
|
|
158 public Set entrySet() {
|
|
159 getterCalled();
|
|
160 return wrappedMap.entrySet();
|
|
161 }
|
|
162
|
|
163 public Object get(Object key) {
|
|
164 getterCalled();
|
|
165 return wrappedMap.get(key);
|
|
166 }
|
|
167
|
|
168 public bool isEmpty() {
|
|
169 getterCalled();
|
|
170 return wrappedMap.isEmpty();
|
|
171 }
|
|
172
|
|
173 public Set keySet() {
|
|
174 getterCalled();
|
|
175 return wrappedMap.keySet();
|
|
176 }
|
|
177
|
|
178 public int size() {
|
|
179 getterCalled();
|
|
180 return wrappedMap.size();
|
|
181 }
|
|
182
|
|
183 public Collection values() {
|
|
184 getterCalled();
|
|
185 return wrappedMap.values();
|
|
186 }
|
|
187
|
|
188 /**
|
|
189 * Returns the stale state. Must be invoked from the current realm.
|
|
190 *
|
|
191 * @return stale state
|
|
192 */
|
|
193 public bool isStale() {
|
|
194 checkRealm();
|
|
195 return stale;
|
|
196 }
|
|
197
|
|
198 /**
|
|
199 * Sets the stale state. Must be invoked from the current realm.
|
|
200 *
|
|
201 * @param stale
|
|
202 * The stale state to set. This will fire a stale event if the
|
|
203 * given bool is true and this observable set was not already
|
|
204 * stale.
|
|
205 */
|
|
206 public void setStale(bool stale) {
|
|
207 checkRealm();
|
|
208 bool wasStale = this.stale;
|
|
209 this.stale = stale;
|
|
210 if (!wasStale && stale) {
|
|
211 fireStale();
|
|
212 }
|
|
213 }
|
|
214
|
|
215 public Object put(Object key, Object value) {
|
|
216 throw new UnsupportedOperationException();
|
|
217 }
|
|
218
|
|
219 public Object remove(Object key) {
|
|
220 throw new UnsupportedOperationException();
|
|
221 }
|
|
222
|
|
223 public void clear() {
|
|
224 throw new UnsupportedOperationException();
|
|
225 }
|
|
226
|
|
227 public void putAll(Map arg0) {
|
|
228 throw new UnsupportedOperationException();
|
|
229 }
|
|
230
|
|
231 public synchronized void dispose() {
|
|
232 super.dispose();
|
|
233 }
|
|
234 }
|