comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/databinding/observable/map/ObservableMap.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, 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 * Matthew Hall - bug 245183
12 *******************************************************************************/
13
14 package org.eclipse.core.databinding.observable.map;
15
16 import java.util.Collection;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.eclipse.core.databinding.observable.AbstractObservable;
21 import org.eclipse.core.databinding.observable.ObservableTracker;
22 import org.eclipse.core.databinding.observable.Realm;
23
24 /**
25 *
26 * <p>
27 * This class is thread safe. All state accessing methods must be invoked from
28 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
29 * listeners may be invoked from any thread.
30 * </p>
31 * @since 1.0
32 */
33 public class ObservableMap : AbstractObservable , IObservableMap {
34
35 protected Map wrappedMap;
36
37 private bool stale = false;
38
39 /**
40 * @param wrappedMap
41 */
42 public this(Map wrappedMap) {
43 this(Realm.getDefault(), wrappedMap);
44 }
45
46 /**
47 * @param realm
48 * @param wrappedMap
49 */
50 public this(Realm realm, Map wrappedMap) {
51 super(realm);
52 this.wrappedMap = wrappedMap;
53 }
54
55 public synchronized void addMapChangeListener(IMapChangeListener listener) {
56 addListener(MapChangeEvent.TYPE, listener);
57 }
58
59 public synchronized void removeMapChangeListener(IMapChangeListener listener) {
60 removeListener(MapChangeEvent.TYPE, listener);
61 }
62
63 protected void getterCalled() {
64 ObservableTracker.getterCalled(this);
65 }
66
67 protected void fireMapChange(MapDiff diff) {
68 checkRealm();
69
70 // fire general change event first
71 super.fireChange();
72
73 fireEvent(new MapChangeEvent(this, diff));
74 }
75
76 public bool containsKey(Object key) {
77 getterCalled();
78 return wrappedMap.containsKey(key);
79 }
80
81 public bool containsValue(Object value) {
82 getterCalled();
83 return wrappedMap.containsValue(value);
84 }
85
86 public Set entrySet() {
87 getterCalled();
88 return wrappedMap.entrySet();
89 }
90
91 public Object get(Object key) {
92 getterCalled();
93 return wrappedMap.get(key);
94 }
95
96 public bool isEmpty() {
97 getterCalled();
98 return wrappedMap.isEmpty();
99 }
100
101 public Set keySet() {
102 getterCalled();
103 return wrappedMap.keySet();
104 }
105
106 public int size() {
107 getterCalled();
108 return wrappedMap.size();
109 }
110
111 public Collection values() {
112 getterCalled();
113 return wrappedMap.values();
114 }
115
116 /**
117 * Returns the stale state. Must be invoked from the current realm.
118 *
119 * @return stale state
120 */
121 public bool isStale() {
122 checkRealm();
123 return stale;
124 }
125
126 /**
127 * Sets the stale state. Must be invoked from the current realm.
128 *
129 * @param stale
130 * The stale state to set. This will fire a stale event if the
131 * given bool is true and this observable set was not already
132 * stale.
133 */
134 public void setStale(bool stale) {
135 checkRealm();
136 bool wasStale = this.stale;
137 this.stale = stale;
138 if (!wasStale && stale) {
139 fireStale();
140 }
141 }
142
143 public Object put(Object key, Object value) {
144 throw new UnsupportedOperationException();
145 }
146
147 public Object remove(Object key) {
148 throw new UnsupportedOperationException();
149 }
150
151 public void clear() {
152 throw new UnsupportedOperationException();
153 }
154
155 public void putAll(Map arg0) {
156 throw new UnsupportedOperationException();
157 }
158
159 public synchronized void dispose() {
160 super.dispose();
161 }
162
163 public override equals_t opEquals(Object obj) {
164 getterCalled();
165
166 if (obj is this)
167 return true;
168 if (obj is null)
169 return false;
170 if (getClass() is obj.getClass()) {
171 return wrappedMap.equals((cast(ObservableMap) obj).wrappedMap);
172 }
173
174 return wrappedMap.equals(obj);
175 }
176
177 public override hash_t toHash() {
178 getterCalled();
179
180 return wrappedMap.hashCode();
181 }
182 }