KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > set > MappedSet


1 /*******************************************************************************
2  * Copyright (c) 2006 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.jface.internal.databinding.provisional.observable.set;
13
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
22 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable;
23 import org.eclipse.jface.internal.databinding.provisional.observable.mapping.IMappingChangeListener;
24 import org.eclipse.jface.internal.databinding.provisional.observable.mapping.IObservableMapping;
25 import org.eclipse.jface.internal.databinding.provisional.observable.mapping.MappingDiff;
26
27 /**
28  * @since 1.0
29  *
30  */

31 public class MappedSet extends ObservableSet {
32
33     private final IObservableMapping wrappedMapping;
34
35     /*
36      * Map from values (range elements) to Integer ref counts
37      */

38     private Map JavaDoc valueCounts = new HashMap JavaDoc();
39
40     private ISetChangeListener domainListener = new ISetChangeListener() {
41         public void handleSetChange(IObservableSet source, SetDiff diff) {
42             Set JavaDoc additions = new HashSet JavaDoc();
43             for (Iterator JavaDoc it = diff.getAdditions().iterator(); it.hasNext();) {
44                 Object JavaDoc added = it.next();
45                 Object JavaDoc mappingValue = wrappedMapping.getMappingValue(added);
46                 if (handleAddition(mappingValue)) {
47                     additions.add(mappingValue);
48                 }
49             }
50             Set JavaDoc removals = new HashSet JavaDoc();
51             for (Iterator JavaDoc it = diff.getRemovals().iterator(); it.hasNext();) {
52                 Object JavaDoc removed = it.next();
53                 Object JavaDoc mappingValue = wrappedMapping.getMappingValue(removed);
54                 if (handleRemoval(mappingValue)) {
55                     removals.add(mappingValue);
56                 }
57             }
58             fireSetChange(Diffs.createSetDiff(additions, removals));
59         }
60     };
61
62     private IMappingChangeListener mappingChangeListener = new IMappingChangeListener() {
63         public void handleMappingValueChange(IObservable source,
64                 MappingDiff diff) {
65             Set JavaDoc affectedElements = diff.getElements();
66             Set JavaDoc additions = new HashSet JavaDoc();
67             Set JavaDoc removals = new HashSet JavaDoc();
68             for (Iterator JavaDoc it = affectedElements.iterator(); it.hasNext();) {
69                 Object JavaDoc element = it.next();
70                 Object JavaDoc oldFunctionValue = diff.getOldMappingValues(element,
71                         new int[] { 0 })[0];
72                 Object JavaDoc newFunctionValue = diff.getNewMappingValues(element,
73                         new int[] { 0 });
74                 if (handleRemoval(oldFunctionValue)) {
75                     removals.add(oldFunctionValue);
76                 }
77                 if (handleAddition(newFunctionValue)) {
78                     additions.add(newFunctionValue);
79                 }
80             }
81             fireSetChange(Diffs.createSetDiff(additions, removals));
82         }
83     };
84
85     private IObservableSet input;
86
87     /**
88      * @param input
89      * @param mapping
90      */

91     public MappedSet(IObservableSet input, IObservableMapping mapping) {
92         super(Collections.EMPTY_SET, mapping.getValueType());
93         setWrappedSet(valueCounts.keySet());
94         this.wrappedMapping = mapping;
95         this.input = input;
96         for (Iterator JavaDoc it = input.iterator(); it.hasNext();) {
97             Object JavaDoc element = it.next();
98             Object JavaDoc functionValue = wrappedMapping.getMappingValue(element);
99             handleAddition(functionValue);
100         }
101         input.addSetChangeListener(domainListener);
102         mapping.addMappingChangeListener(mappingChangeListener);
103     }
104
105     /**
106      * @param mappingValue
107      * @return true if the given mappingValue was an addition
108      */

109     protected boolean handleAddition(Object JavaDoc mappingValue) {
110         Integer JavaDoc count = (Integer JavaDoc) valueCounts.get(mappingValue);
111         if (count == null) {
112             valueCounts.put(mappingValue, new Integer JavaDoc(1));
113             return true;
114         }
115         valueCounts.put(mappingValue, new Integer JavaDoc(count.intValue() + 1));
116         return false;
117     }
118
119     /**
120      * @param mappingValue
121      * @return true if the given mappingValue has been removed
122      */

123     protected boolean handleRemoval(Object JavaDoc mappingValue) {
124         Integer JavaDoc count = (Integer JavaDoc) valueCounts.get(mappingValue);
125         if (count.intValue() <= 1) {
126             valueCounts.remove(mappingValue);
127             return true;
128         }
129         valueCounts.put(mappingValue, new Integer JavaDoc(count.intValue() - 1));
130         return false;
131     }
132
133     public void dispose() {
134         wrappedMapping.removeMappingChangeListener(mappingChangeListener);
135         input.removeSetChangeListener(domainListener);
136     }
137
138     public Object JavaDoc getElementType() {
139         return wrappedMapping.getValueType();
140     }
141
142 }
143
Popular Tags