|
| 1 | +package dev.cel.runtime; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import com.google.errorprone.annotations.Immutable; |
| 5 | +import dev.cel.common.values.CelValue; |
| 6 | + |
| 7 | +@Immutable |
| 8 | +public final class CelValueFunctionBinding { |
| 9 | + |
| 10 | + private final String overloadId; |
| 11 | + private final ImmutableList<Class<? extends CelValue>> argTypes; |
| 12 | + private final CelValueFunctionOverload definition; |
| 13 | + |
| 14 | + public String overloadId() { |
| 15 | + return overloadId; |
| 16 | + } |
| 17 | + |
| 18 | + public ImmutableList<Class<? extends CelValue>> argTypes() { |
| 19 | + return argTypes; |
| 20 | + } |
| 21 | + |
| 22 | + public CelValueFunctionOverload definition() { |
| 23 | + return definition; |
| 24 | + } |
| 25 | + |
| 26 | + public static CelValueFunctionBinding from(String overloadId, CelValueFunctionOverload.Nullary impl) { |
| 27 | + return from(overloadId, ImmutableList.of(), (args) -> impl.apply()); |
| 28 | + } |
| 29 | + |
| 30 | + public static <T extends CelValue> CelValueFunctionBinding from( |
| 31 | + String overloadId, Class<T> argType, CelValueFunctionOverload.Unary<T> impl) { |
| 32 | + return from(overloadId, ImmutableList.of(argType), (args) -> impl.apply((T) args[0])); |
| 33 | + } |
| 34 | + |
| 35 | + public static <T1 extends CelValue, T2 extends CelValue> CelValueFunctionBinding from(String overloadId, Class<T1> argType1, Class<T2> argType2, CelValueFunctionOverload.Binary<T1, T2> impl) { |
| 36 | + return from(overloadId, ImmutableList.of(argType1, argType2), (args) -> impl.apply((T1) args[0], (T2) args[1])); |
| 37 | + } |
| 38 | + |
| 39 | + public static <T1 extends CelValue, T2 extends CelValue, T3 extends CelValue> CelValueFunctionBinding from(String overloadId, Class<T1> argType1, Class<T2> argType2, Class<T3> argType3, CelValueFunctionOverload.Ternary<T1, T2, T3> impl) { |
| 40 | + return from(overloadId, ImmutableList.of(argType1, argType2, argType3), (args) -> impl.apply((T1) args[0], (T2) args[1], (T3) args[2])); |
| 41 | + } |
| 42 | + |
| 43 | + public static CelValueFunctionBinding from(String overloadId, ImmutableList<Class<? extends CelValue>> argTypes, CelValueFunctionOverload impl) { |
| 44 | + return new CelValueFunctionBinding(overloadId, argTypes, impl); |
| 45 | + } |
| 46 | + |
| 47 | + public boolean canHandle(CelValue[] arguments) { |
| 48 | + if (argTypes().size() != arguments.length) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + for (int i = 0; i < argTypes().size(); i++) { |
| 53 | + Class<? extends CelValue> paramType = argTypes().get(i); |
| 54 | + CelValue arg = arguments[i]; |
| 55 | + if (!paramType.isInstance(arg)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + private CelValueFunctionBinding(String overloadId, ImmutableList<Class<? extends CelValue>> argTypes, CelValueFunctionOverload definition) { |
| 63 | + this.overloadId = overloadId; |
| 64 | + this.argTypes = argTypes; |
| 65 | + this.definition = definition; |
| 66 | + } |
| 67 | +} |
0 commit comments