]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/interestset/MapInterestSet.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / interestset / MapInterestSet.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.accessor.interestset;
13
14 import java.util.Map;
15 import java.util.TreeMap;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.annotations.Optional;
19 import org.simantics.databoard.binding.mutable.Variant;
20 import org.simantics.databoard.type.MapType;
21
22 /**
23  * Interest set of a Map Type. 
24  *
25  * @see MapType
26  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27  */
28 public class MapInterestSet extends InterestSet {
29         
30         // Notifications
31         /** Interested in the notifications of entry assignments, add and removal (excludes the value) */
32         public boolean notification; // Assignment Notification
33         /** Interested in the notifications of entry assignments by key (excludes the value). Sorted array of keys */  
34         public @Optional Variant[] notifications;
35         
36         // Value Assignments
37         /** Interested in new values of entry assignments (includes notification, excludes composite changes)*/ 
38         public boolean value; // Assignments
39         /** Interested in new values of entry assignments by key (includes notification, excludes composite changes). Sorted array of keys */
40         public @Optional Variant[] values;
41         
42         // Content
43         /** Interest in the contents of values (includes assignment notification and new values) */ 
44         public @Optional InterestSet componentInterest; // Values
45         /** Interest in the contents of values by key (includes assignment notification and new values) */ 
46         public @Optional Map<Variant, InterestSet> componentInterests;
47
48         public MapInterestSet() {}
49         
50         /**
51          * Create a map interest set
52          * 
53          * @param notification interested in add, remove and change notifications of entry assignments 
54          * @param notifications interested in add, remove and change to assignment of all specific entries
55          * @param value interested in add, remove and change to assignment of all entries, incl. new values
56          * @param values interested in add, remove and change to assignment of all specific entries, incl. new values
57          * @param componentInterest Interest of the changes of values and their sub-value changes of all entries
58          * @param componentInterests Interest of the changes of values and their sub-value changes of specific entries
59          */
60         public MapInterestSet(boolean notification, Variant[] notifications, boolean value, Variant[] values, InterestSet componentInterest, Map<Variant, InterestSet> componentInterests) {
61                 this.notification = notification;
62                 this.notifications = notifications;
63                 this.value = value;
64                 this.values = values;
65                 this.componentInterest = componentInterest;
66                 this.componentInterests = componentInterests;
67         }
68
69         /**
70          * Returns true, if interested in notification of entry add, removal or replacement of container. 
71          * This doesn't include interest in the new value 
72          * 
73          * @return true if interested in notifications
74          */
75         public boolean inNotifications() {
76                 return notification | value | (componentInterest!=null);
77         }
78
79         /**
80          * Returns true, if interested in notification of entry add, removal or replacement of container of
81          * specific keys. This doesn't include interest in the new value.
82          * 
83          * @return true if interested in notifications of a specific entry
84          */
85         public boolean inNotificationsOf(Variant key) {
86                 if (notification || value) return true;
87
88                 if (notifications!=null) {
89                         for (int i=0; i<notifications.length; i++) {
90                                 if (notifications[i] == key) return true;
91                         }
92                 }               
93                 
94                 if (values!=null) {
95                         for (int i=0; i<values.length; i++) {
96                                 if (values[i] == key) return true;
97                         }
98                 }               
99                 
100                 if (componentInterests!=null) {
101                         InterestSet compositeInterest = componentInterests.get(key);
102                         return compositeInterest!=null;
103                 }
104                 return false;
105         }
106         
107         /**
108          * Returns true, if interested in notifications and values of entry assignments. 
109          * 
110          * @return true, if interested in values of entry assignments.
111          */
112         public boolean inValues() {
113                 return value | (componentInterest!=null);
114         }
115
116         /**
117          * Returns true, if interested in notifications and values of entry assignments of 
118          * <code>key</code>.
119          * 
120          * @return true, if interested in values of specific entry
121          */
122         public boolean inValuesOf(Variant key) {
123                 if (value | componentInterest!=null) return true;
124
125                 if (values!=null) {
126                         for (int i=0; i<values.length; i++) {
127                                 if (values[i] == key) return true;
128                         }
129                 }
130                 
131                 if (componentInterests!=null?componentInterests.containsKey(key):false) return true;
132                 return false;
133         }
134
135         /**
136          * Get the interest of component type of every value.  
137          * 
138          * @return interest of component or <code>null</code>
139          */
140         public InterestSet getComponentInterest() {
141                 return componentInterest;
142         }
143         
144         /**
145          * Get the interest of component type of entries of <code>key</code>.
146          * 
147          * @return interest of component or <code>null</code>
148          */
149         public InterestSet getComponentInterest(Variant key) {
150                 if (componentInterests==null) return null;
151                 return componentInterests.get(key);             
152         }
153         
154         /**
155          * Add key specific interest. Map is created if null.
156          * 
157          * @param key
158          * @param cis
159          */
160         public void addCompositeInterest(Variant key, InterestSet cis) {
161                 if (componentInterests == null) componentInterests = new TreeMap<Variant, InterestSet>( Bindings.VARIANT );
162                 componentInterests.put(key, cis);
163         }
164         
165 }
166