1 package org.simantics.scl.commands.internal.checker;
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;
14 * Checks given parameters
16 * @author Hannu Niemistö
18 public class Checker {
22 private Checker(Object check, CheckItem item) {
27 public boolean check(Object[] parameters) {
28 return item.check(check, parameters, 0);
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));
38 else if(Types.isApply(Types.MAYBE, 1, type)) {
39 Type componentType = Types.matchApply(Types.MAYBE, type);
40 return new MaybeCheckItem(checkItemForType(componentType));
43 throw new MatchException();
46 public static Checker create(String name) {
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) {
57 return new Checker(Boolean.FALSE, BooleanCheckItem.INSTANCE);