KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > map > ComputedObservableMap


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 package org.eclipse.core.databinding.observable.map;
13
14 import java.util.AbstractSet JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.databinding.observable.Diffs;
23 import org.eclipse.core.databinding.observable.set.IObservableSet;
24 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
25 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
26
27 /**
28  * Maps objects to one of their attributes. Tracks changes to the underlying
29  * observable set of objects (keys), as well as changes to attribute values.
30  */

31 public abstract class ComputedObservableMap extends AbstractObservableMap {
32
33     private final IObservableSet keySet;
34
35     private ISetChangeListener setChangeListener = new ISetChangeListener() {
36         public void handleSetChange(SetChangeEvent event) {
37             Set JavaDoc addedKeys = new HashSet JavaDoc(event.diff.getAdditions());
38             Set JavaDoc removedKeys = new HashSet JavaDoc(event.diff.getRemovals());
39             Map JavaDoc oldValues = new HashMap JavaDoc();
40             Map JavaDoc newValues = new HashMap JavaDoc();
41             for (Iterator JavaDoc it = removedKeys.iterator(); it.hasNext();) {
42                 Object JavaDoc removedKey = it.next();
43                 Object JavaDoc oldValue = doGet(removedKey);
44                 unhookListener(removedKey);
45                 if (oldValue != null) {
46                     oldValues.put(removedKey, oldValue);
47                 }
48             }
49             for (Iterator JavaDoc it = addedKeys.iterator(); it.hasNext();) {
50                 Object JavaDoc addedKey = it.next();
51                 hookListener(addedKey);
52                 Object JavaDoc newValue = doGet(addedKey);
53                 newValues.put(addedKey, newValue);
54             }
55             fireMapChange(Diffs.createMapDiff(addedKeys, removedKeys,
56                     Collections.EMPTY_SET, oldValues, newValues));
57         }
58     };
59
60     private Set JavaDoc entrySet = new EntrySet();
61
62     private class EntrySet extends AbstractSet JavaDoc {
63
64         public Iterator JavaDoc iterator() {
65             final Iterator JavaDoc keyIterator = keySet.iterator();
66             return new Iterator JavaDoc() {
67
68                 public boolean hasNext() {
69                     return keyIterator.hasNext();
70                 }
71
72                 public Object JavaDoc next() {
73                     final Object JavaDoc key = keyIterator.next();
74                     return new Map.Entry JavaDoc() {
75
76                         public Object JavaDoc getKey() {
77                             return key;
78                         }
79
80                         public Object JavaDoc getValue() {
81                             return get(getKey());
82                         }
83
84                         public Object JavaDoc setValue(Object JavaDoc value) {
85                             return put(getKey(), value);
86                         }
87                     };
88                 }
89
90                 public void remove() {
91                     keyIterator.remove();
92                 }
93             };
94         }
95
96         public int size() {
97             return keySet.size();
98         }
99
100     }
101
102     /**
103      * @param keySet
104      */

105     public ComputedObservableMap(IObservableSet keySet) {
106         super(keySet.getRealm());
107         this.keySet = keySet;
108         this.keySet.addSetChangeListener(setChangeListener);
109     }
110
111     protected void init() {
112         for (Iterator JavaDoc it = this.keySet.iterator(); it.hasNext();) {
113             Object JavaDoc key = it.next();
114             hookListener(key);
115         }
116     }
117
118     protected final void fireSingleChange(Object JavaDoc key, Object JavaDoc oldValue,
119             Object JavaDoc newValue) {
120         fireMapChange(Diffs.createMapDiffSingleChange(key, oldValue, newValue));
121     }
122
123     public Set JavaDoc entrySet() {
124         return entrySet;
125     }
126     
127     public Set JavaDoc keySet() {
128         return keySet;
129     }
130
131     final public Object JavaDoc get(Object JavaDoc key) {
132         return doGet(key);
133     }
134
135     final public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
136         return doPut(key, value);
137     }
138
139     /**
140      * @param removedKey
141      */

142     protected abstract void unhookListener(Object JavaDoc removedKey);
143
144     /**
145      * @param addedKey
146      */

147     protected abstract void hookListener(Object JavaDoc addedKey);
148
149     /**
150      * @param key
151      * @return the value for the given key
152      */

153     protected abstract Object JavaDoc doGet(Object JavaDoc key);
154
155     /**
156      * @param key
157      * @param value
158      * @return the old value for the given key
159      */

160     protected abstract Object JavaDoc doPut(Object JavaDoc key, Object JavaDoc value);
161 }
162
Popular Tags