]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/interestset/InterestSet.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / interestset / InterestSet.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 org.simantics.databoard.annotations.Union;
15 import org.simantics.databoard.type.ArrayType;
16 import org.simantics.databoard.type.BooleanType;
17 import org.simantics.databoard.type.ByteType;
18 import org.simantics.databoard.type.Datatype;
19 import org.simantics.databoard.type.DoubleType;
20 import org.simantics.databoard.type.FloatType;
21 import org.simantics.databoard.type.IntegerType;
22 import org.simantics.databoard.type.LongType;
23 import org.simantics.databoard.type.MapType;
24 import org.simantics.databoard.type.OptionalType;
25 import org.simantics.databoard.type.RecordType;
26 import org.simantics.databoard.type.StringType;
27 import org.simantics.databoard.type.UnionType;
28 import org.simantics.databoard.type.VariantType;
29
30 /**
31  * InterestSet is a description about what is monitored in a data model.
32  *
33  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
34  */
35 @Union({BooleanInterestSet.class,
36                 ByteInterestSet.class,
37                 IntegerInterestSet.class,
38                 LongInterestSet.class,
39                 FloatInterestSet.class,
40                 DoubleInterestSet.class,
41                 StringInterestSet.class,
42                 RecordInterestSet.class,
43                 ArrayInterestSet.class,
44                 MapInterestSet.class,
45                 OptionalInterestSet.class,
46                 UnionInterestSet.class,
47             VariantInterestSet.class
48             })
49 public abstract class InterestSet {
50
51 //      private static final String[] EMPTY_ARRAY = new String[0]; 
52         
53         /**
54          * Returns true, if interested in notifications of modifications, this question excludes
55          * an interest to the values.
56          * 
57          * @return true if interested in notifications
58          */
59         public abstract boolean inNotifications();
60         
61         /**
62          * Returns true, if interested in notifications of changes and the new values.
63          * 
64          * @return true if interested in notifications and value
65          */
66         public abstract boolean inValues();
67         
68
69         /**
70          * Create a new interest set 
71          * 
72          * @param type the type to create interest set for
73          * @param notification interested in notification of changes, not the values
74          * @param value interested in values (includes notification)
75          * @param recursive interested in values recursivelu (includes notification)
76          */
77         public static InterestSet newInterestSet(Datatype type, boolean notification, boolean value, boolean recursive) {
78                 
79                 if (type instanceof BooleanType) {
80                         if (recursive) return BooleanInterestSet.MONITOR_EVERYTHING;
81                         return new BooleanInterestSet(notification, value);
82                 }
83                 
84                 if (type instanceof ByteType) {
85                         if (recursive) return ByteInterestSet.MONITOR_EVERYTHING;
86                         return new ByteInterestSet(notification, value);
87                 }
88                 
89                 if (type instanceof IntegerType) {
90                         if (recursive) return IntegerInterestSet.MONITOR_EVERYTHING;
91                         return new IntegerInterestSet(notification, value);
92                 }
93                 
94                 if (type instanceof LongType) {
95                         if (recursive) return LongInterestSet.MONITOR_EVERYTHING;
96                         return new LongInterestSet(notification, value);
97                 }
98                 
99                 if (type instanceof FloatType) {
100                         if (recursive) return FloatInterestSet.MONITOR_EVERYTHING;
101                         return new FloatInterestSet(notification, value);
102                 }
103                 
104                 if (type instanceof DoubleType) {
105                         if (recursive) return DoubleInterestSet.MONITOR_EVERYTHING;
106                         return new DoubleInterestSet(notification, value);
107                 }
108                 
109                 if (type instanceof StringType) {
110                         if (recursive) return StringInterestSet.MONITOR_EVERYTHING;
111                         return new StringInterestSet(notification, value);
112                 }
113                 
114                 if (type instanceof OptionalType) {
115                         OptionalType ot = (OptionalType) type;
116                         InterestSet cis = recursive ? newInterestSet(ot.getComponentType(), notification, value, true) : null;
117                         return new OptionalInterestSet(notification, value, cis );
118                 }
119                 
120                 if (type instanceof RecordType) {
121                         if (!recursive) {
122                                 return new RecordInterestSet(notification, null, value, null, null);
123                         }
124                         RecordType rt = (RecordType) type;
125                         InterestSet[] componentInterests = new InterestSet[rt.getComponentCount()];
126                         for (int i=0; i<componentInterests.length; i++) {
127                                 componentInterests[i] = newInterestSet( rt.getComponentType(i), notification, value, true );
128                         }
129                         return new RecordInterestSet(true, null, true, null, componentInterests);
130                 }
131                 
132                 if (type instanceof UnionType) {
133                         if (!recursive) {
134                                 return new UnionInterestSet(notification, value, null);                         
135                         }
136                         
137                         UnionType ut = (UnionType) type;                        
138                         InterestSet[] componentInterests = new InterestSet[ut.getComponentCount()];
139                         for (int i=0; i<componentInterests.length; i++) {
140                                 componentInterests[i] = newInterestSet( ut.getComponent(i).type, notification, value, true );
141                         }
142                         return new UnionInterestSet(notification, value, componentInterests);
143                 }
144                 
145                 if (type instanceof ArrayType) {
146                         ArrayType at = (ArrayType) type;
147                         InterestSet cis = recursive ? newInterestSet( at.componentType(), notification, value, true ) : null;
148                         return new ArrayInterestSet(notification, null, value, null, cis, null);
149                 }
150                 
151                 if (type instanceof MapType) {
152                         MapType mt = (MapType) type;
153                         InterestSet cis = recursive ? newInterestSet( mt.valueType, notification, value, true ) : null;
154                         return new MapInterestSet(notification, null, value, null, cis, null);
155                 }
156                 
157                 if (type instanceof VariantType) {
158                         return new VariantInterestSet(notification, value, null, recursive);
159                 }
160                 
161                 throw new IllegalArgumentException(type.toSingleLineString());          
162         }
163
164 //      public String[] getCustomEvents() {
165 //              return customEvents;
166 //      }
167
168 //      public void setCustomEvents(String[] customEvents) {
169 //              this.customEvents = customEvents;
170 //      }
171         /** A list of custom event types of interest */
172 //      public String[] customEvents = EMPTY_ARRAY;
173         
174         
175 }
176