]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.scl.commands/src/org/simantics/scl/commands/internal/checker/Checker.java b/bundles/org.simantics.scl.commands/src/org/simantics/scl/commands/internal/checker/Checker.java
new file mode 100644 (file)
index 0000000..7003431
--- /dev/null
@@ -0,0 +1,60 @@
+package org.simantics.scl.commands.internal.checker;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.scl.compiler.elaboration.modules.SCLValue;\r
+import org.simantics.scl.compiler.top.ValueNotFound;\r
+import org.simantics.scl.compiler.types.Type;\r
+import org.simantics.scl.compiler.types.Types;\r
+import org.simantics.scl.compiler.types.exceptions.MatchException;\r
+import org.simantics.scl.compiler.types.util.MultiFunction;\r
+import org.simantics.scl.osgi.SCLOsgi;\r
+\r
+/**\r
+ * Checks given parameters\r
+ * \r
+ * @author Hannu Niemistö\r
+ */\r
+public class Checker {\r
+    Object check;\r
+    CheckItem item;\r
+    \r
+    private Checker(Object check, CheckItem item) {\r
+        this.check = check;\r
+        this.item = item;\r
+    }\r
+    \r
+    public boolean check(Object[] parameters) {\r
+        return item.check(check, parameters, 0);\r
+    }\r
+\r
+    private static CheckItem checkItemForType(Type type) throws MatchException {\r
+        if(type == Types.BOOLEAN)\r
+            return BooleanCheckItem.INSTANCE;\r
+        if(Types.isFunction(type)) {\r
+            MultiFunction mfun = Types.matchFunction(type, 1);\r
+            return new FunctionCheckItem(mfun.parameterTypes[0], checkItemForType(mfun.returnType));\r
+        }\r
+        else if(Types.isApply(Types.MAYBE, 1, type)) {\r
+            Type componentType = Types.matchApply(Types.MAYBE, type);\r
+            return new MaybeCheckItem(checkItemForType(componentType));\r
+        }\r
+        else\r
+            throw new MatchException();\r
+    }\r
+    \r
+    public static Checker create(String name) {\r
+        try {\r
+            SCLValue checkRef = SCLOsgi.MODULE_REPOSITORY.getValueRef(name);\r
+            Object check = SCLOsgi.MODULE_REPOSITORY.getValue(name);\r
+            return new Checker(check, checkItemForType(checkRef.getType()));\r
+        } catch(ValueNotFound e) {\r
+            // If we don't find a check, it always succeeds\r
+            return new Checker(Boolean.TRUE, BooleanCheckItem.INSTANCE);\r
+        } catch (MatchException e) {\r
+            // Should not happens\r
+            e.printStackTrace();\r
+            return new Checker(Boolean.FALSE, BooleanCheckItem.INSTANCE);\r
+        }\r
+    }\r
+}\r