]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/util/DefaultValue.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / util / DefaultValue.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.binding.util;
13
14 import java.util.Map;
15 import java.util.WeakHashMap;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.binding.ArrayBinding;
19 import org.simantics.databoard.binding.Binding;
20 import org.simantics.databoard.binding.Binding.Visitor;
21 import org.simantics.databoard.binding.BooleanBinding;
22 import org.simantics.databoard.binding.ByteBinding;
23 import org.simantics.databoard.binding.DoubleBinding;
24 import org.simantics.databoard.binding.FloatBinding;
25 import org.simantics.databoard.binding.IntegerBinding;
26 import org.simantics.databoard.binding.LongBinding;
27 import org.simantics.databoard.binding.MapBinding;
28 import org.simantics.databoard.binding.OptionalBinding;
29 import org.simantics.databoard.binding.RecordBinding;
30 import org.simantics.databoard.binding.StringBinding;
31 import org.simantics.databoard.binding.UnionBinding;
32 import org.simantics.databoard.binding.VariantBinding;
33 import org.simantics.databoard.binding.error.BindingException;
34 import org.simantics.databoard.binding.error.RuntimeBindingException;
35 import org.simantics.databoard.type.ArrayType;
36 import org.simantics.databoard.type.ByteType;
37 import org.simantics.databoard.type.DoubleType;
38 import org.simantics.databoard.type.FloatType;
39 import org.simantics.databoard.type.IntegerType;
40 import org.simantics.databoard.type.LongType;
41 import org.simantics.databoard.type.StringType;
42 import org.simantics.databoard.util.Range;
43
44 /**
45  * Visitor that creates a default instance of a DataType.
46  * This visitor may throw RuntimeBindingException. 
47  * 
48  * Type                     Value
49  * ------------------------------------------------------
50  * Boolean                  false
51  * Byte, Integer, Long      0 or min limit
52  * Float, Double            0.0 or min limit
53  * String                   ""
54  * Optional                 *novalue*
55  * Union                    tag 0
56  * Record                   each field with default value
57  * Array                    min range number of elements
58  * Map                      no entries
59  * Variant                  {} : void
60  * 
61  * 
62  * TODO Create String according to the pattern
63  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
64  */
65 public class DefaultValue implements Visitor<Object> {
66
67         /** Map of default values already created. Used to link back to recursive records */
68         Map<Binding, Object> map = new WeakHashMap<Binding, Object>(1); 
69         
70         @Override
71         public Object visit(ArrayBinding b) {
72                 Object result = null;
73                 
74                 ArrayType at = b.type();
75                 Range r = at.getLength();
76                 int min = (r!=null) ? r.getLower().getValue().intValue() : 0;
77                         
78                 if (min>0) {
79                         Binding componentBinding = b.getComponentBinding();
80                         Object[] array = new Object[min];
81                         for (int i=0; i<min; i++) {
82                                 array[i] = componentBinding.accept(this);
83                         }
84                         result = b.createUnchecked(array);
85                 } else {
86                         result = b.createUnchecked();
87                 }               
88                 return result;
89         }
90
91         @Override
92         public Object visit(BooleanBinding b) {
93                 Object result = b.createUnchecked(Boolean.FALSE);
94                 return result;
95         }
96
97         @Override
98         public Object visit(DoubleBinding b) {
99                 DoubleType type = b.type();
100                 Range range = type.getRange();
101                 Number lowValue = range==null ? null : range.getLower().getValue();
102                 return b.createUnchecked( lowValue == null ? 0 : lowValue.doubleValue() );
103         }
104
105         @Override
106         public Object visit(FloatBinding b) {
107                 FloatType type = b.type();
108                 Range range = type.getRange();
109                 Number lowValue = range==null ? null : range.getLower().getValue();
110                 return b.createUnchecked( lowValue == null ? 0 : lowValue.floatValue() );
111         }
112
113         @Override
114         public Object visit(IntegerBinding b) {
115                 IntegerType type = b.type();
116                 Range range = type.getRange();
117                 Number lowValue = range==null ? null : range.getLower().getValue();
118                 return b.createUnchecked( lowValue == null ? 0 : lowValue.intValue() );
119         }
120
121         @Override
122         public Object visit(ByteBinding b) {
123                 ByteType type = b.type();
124                 Range range = type.getRange();
125                 Number lowValue = range==null ? null : range.getLower().getValue();
126                 return b.createUnchecked( lowValue == null ? 0 : lowValue.intValue() );
127         }
128
129         @Override
130         public Object visit(LongBinding b) {
131                 LongType type = b.type();
132                 Range range = type.getRange();
133                 Number lowValue = range==null ? null : range.getLower().getValue();
134                 return b.createUnchecked( lowValue == null ? 0 : lowValue.longValue() );
135         }
136
137         @Override
138         public Object visit(OptionalBinding b) {
139                 return b.createNoValueUnchecked();
140         }
141
142         @Override
143         public Object visit(RecordBinding b) {
144                 try {
145                         Object result = null;
146                         if (b.type().isReferable()) {
147                                 result = map.get(b);
148                                 if (result!=null) return result;
149                         }
150                         Object[] values = new Object[ b.getComponentCount() ];
151                         result = b.createPartial();
152                         if (b.type().isReferable()) map.put(b, result);
153                         for (int i=0; i<values.length; i++) {
154                                 Binding cb = b.getComponentBinding(i);                  
155                                 values[i] = cb.accept(this);
156                         }
157                         b.setComponents(result, values);
158                         return result;
159                 } catch (BindingException e) {
160                         throw new RuntimeBindingException(e);
161                 }
162         }
163
164         @Override
165         public Object visit(StringBinding b) {
166                 Object result = null;
167                 StringType st = b.type();
168                 if (st.getPattern() != null) {
169                         // TODO Create a string that is valid to the pattern
170                         result = b.createUnchecked("");                 
171                 } else {
172                         result = b.createUnchecked("");
173                 }
174                 return result;
175         }
176
177         @Override
178         public Object visit(UnionBinding b) {
179                 int tag = 0;
180                 Binding componentBinding = b.getComponentBinding(tag);
181                 Object tag0defaultValue = componentBinding.accept(this);
182                 return b.createUnchecked(tag, tag0defaultValue);
183         }
184
185         
186         @Override
187         public Object visit(VariantBinding b) {
188                 Binding voidBinding = Bindings.getBindingUnchecked(void.class);
189                 Object voidValue = null;
190                 return b.createUnchecked(voidBinding, voidValue);
191         }
192
193         @Override
194         public Object visit(MapBinding b) {
195                 return b.createUnchecked();
196         }
197     
198 }
199