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