]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.commands/src/org/simantics/scl/commands/internal/checker/Checker.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.commands / src / org / simantics / scl / commands / internal / checker / Checker.java
1 package org.simantics.scl.commands.internal.checker;\r
2 \r
3 import java.util.Map;\r
4 \r
5 import org.simantics.scl.compiler.elaboration.modules.SCLValue;\r
6 import org.simantics.scl.compiler.top.ValueNotFound;\r
7 import org.simantics.scl.compiler.types.Type;\r
8 import org.simantics.scl.compiler.types.Types;\r
9 import org.simantics.scl.compiler.types.exceptions.MatchException;\r
10 import org.simantics.scl.compiler.types.util.MultiFunction;\r
11 import org.simantics.scl.osgi.SCLOsgi;\r
12 \r
13 /**\r
14  * Checks given parameters\r
15  * \r
16  * @author Hannu Niemistö\r
17  */\r
18 public class Checker {\r
19     Object check;\r
20     CheckItem item;\r
21     \r
22     private Checker(Object check, CheckItem item) {\r
23         this.check = check;\r
24         this.item = item;\r
25     }\r
26     \r
27     public boolean check(Object[] parameters) {\r
28         return item.check(check, parameters, 0);\r
29     }\r
30 \r
31     private static CheckItem checkItemForType(Type type) throws MatchException {\r
32         if(type == Types.BOOLEAN)\r
33             return BooleanCheckItem.INSTANCE;\r
34         if(Types.isFunction(type)) {\r
35             MultiFunction mfun = Types.matchFunction(type, 1);\r
36             return new FunctionCheckItem(mfun.parameterTypes[0], checkItemForType(mfun.returnType));\r
37         }\r
38         else if(Types.isApply(Types.MAYBE, 1, type)) {\r
39             Type componentType = Types.matchApply(Types.MAYBE, type);\r
40             return new MaybeCheckItem(checkItemForType(componentType));\r
41         }\r
42         else\r
43             throw new MatchException();\r
44     }\r
45     \r
46     public static Checker create(String name) {\r
47         try {\r
48             SCLValue checkRef = SCLOsgi.MODULE_REPOSITORY.getValueRef(name);\r
49             Object check = SCLOsgi.MODULE_REPOSITORY.getValue(name);\r
50             return new Checker(check, checkItemForType(checkRef.getType()));\r
51         } catch(ValueNotFound e) {\r
52             // If we don't find a check, it always succeeds\r
53             return new Checker(Boolean.TRUE, BooleanCheckItem.INSTANCE);\r
54         } catch (MatchException e) {\r
55             // Should not happens\r
56             e.printStackTrace();\r
57             return new Checker(Boolean.FALSE, BooleanCheckItem.INSTANCE);\r
58         }\r
59     }\r
60 }\r