]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/interestset/ArrayInterestSet.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / interestset / ArrayInterestSet.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
16 import org.simantics.databoard.annotations.Optional;
17 import org.simantics.databoard.type.ArrayType;
18
19 /**
20  * Interest set of an Array Type.  
21  * 
22  * @see ArrayType
23  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
24  */
25 public class ArrayInterestSet extends InterestSet {
26         
27         public static final ArrayInterestSet MONITOR_EVERYTHING = new ArrayInterestSet(true, null, true, null, null, null);
28         
29         // Notifications
30         /** Interested in notification of element assignments, add and removal (excludes the value) */
31         public boolean notification;
32         /** Interested in notifications to elements by index, (excludes the value). Sorted array of indices. */  
33         public @Optional int[] notifications;
34         
35         // Value Assignments
36         /** Interested in all notification and value assignments */ 
37         public boolean value;
38         /** Interested in values by element (includes change). Sorted array of indices */
39         public @Optional int[] values;
40         
41         // Content
42         /** Component Interest of the contents of values. Incl. interest in change and value. <code>null</code> if no interest. */ 
43         public @Optional InterestSet componentInterest;
44         /** Component Interests of the contents of values of specific elements (incl. interest in change and value), <code>null</code> if no interest. */ 
45         public @Optional Map<Integer, InterestSet> componentInterests;
46         // TODO Make TreeMap
47
48         /**
49          * Create an array interest set
50          * 
51          * @param notification interested in add, remove and change to assignment of all elements 
52          * @param specificNotifications interested in add, remove and change to assignment of all specific elements
53          * @param values interested in add, remove and change to assignment of all elements, incl. new values
54          * @param specificValues interested in add, remove and change to assignment of all specific elements, incl. new values
55          * @param componentInterest Interest of the changes of values and their sub-value changes of all elements
56          * @param componentInterests Interest of the changes of values and their sub-value changes of specific elements
57          */
58         public ArrayInterestSet(boolean notification, int[] specificNotifications, boolean values, int[] specificValues, InterestSet componentInterest, Map<Integer, InterestSet> componentInterests) {
59                 this.notification = notification;
60                 this.notifications = specificNotifications;
61                 this.value = values;
62                 this.values = specificValues;
63                 this.componentInterest = componentInterest;
64                 this.componentInterests = componentInterests;
65         }
66
67         /**
68          * Returns true, if interested in element assignment, add, removal. 
69          * This excludes interest in the new values.
70          * 
71          * @return true if interested in notifications
72          */
73         public boolean inNotifications() {
74                 return notification | value | (componentInterest!=null);
75         }
76
77         /**
78          * Returns true, if interested in assignment of the element at specific <tt>index</tt>
79          * 
80          * @param elementIndex
81          * @return true if interested in notifications of a specific element
82          */
83         public boolean inNotificationsOf(int elementIndex) {
84                 if (notification || value) return true;
85                 
86                 if (notifications!=null) {
87                         for (int i=0; i<notifications.length; i++) {
88                                 if (notifications[i] == elementIndex) return true;
89                         }
90                 }
91                 
92                 if (values!=null) {
93                         for (int i=0; i<values.length; i++) {
94                                 if (values[i] == elementIndex) return true;
95                         }
96                 }
97                 
98                 if (componentInterests!=null) {
99                         InterestSet compositeInterest = componentInterests.get( elementIndex );
100                         return compositeInterest!=null;
101                 }
102                 return false;
103         }
104         
105         /**
106          * Returns true, if interested in all new value assignments
107          * 
108          * @return true if interested in values of any element
109          */
110         public boolean inValues() {
111                 return value | (componentInterest!=null);
112         }
113
114         /**
115          * Returns true, if interested in value assignment of a element at 
116          * <tt>index</tt>.
117          * 
118          * @param elementIndex
119          * @return true if interested in the values of a specific element
120          */
121         public boolean inValuesOf(int elementIndex) {
122                 if (value | componentInterest!=null) return true;
123                 
124                 if (values!=null) {
125                         for (int i=0; i<values.length; i++) {
126                                 if (values[i] == elementIndex) return true;
127                         }
128                 }
129                 
130                 if (componentInterests!=null?componentInterests.containsKey(elementIndex):false) return true;
131                 return false;
132         }
133
134         /**
135          * Get the interest of the contents of every value 
136          * 
137          * @return component interest set or <code>null</code>
138          */
139         public InterestSet getComponentInterest() {
140                 return componentInterest;
141         }
142         
143         /**
144          * Get composite interest of component value of element at <code>index</code>
145          * 
146          * @param index element index
147          * @return composite interest set or <code>null</code>
148          */
149         public InterestSet getComponentInterest(int index) {
150                 if (componentInterests==null) return null;
151                 return componentInterests.get(index);           
152         }
153         
154 }
155