public static BiFunction<Number, Number, Token<? extends Number>> addition(final FrameContext.Context context) { return (Number l, Number r) -> { if (l instanceof Double || r instanceof Double) { return new Token<>(l.doubleValue() + r.doubleValue(), Double.class, context); } else if (l instanceof Float || r instanceof Float) { return new Token<>(l.floatValue() + r.floatValue(), Float.class, context); } else if (l instanceof Long || r instanceof Long) { return new Token<>(l.longValue() + r.longValue(), Long.class, context); } else { return new Token<>(l.intValue() + r.intValue(), Integer.class, context); } }; }
public Token<? extends Number> evaluate(BiFunction<Number, Number, Token<? extends Number>> function, Pair<Token<? extends Number>, Token<? extends Number>> p) { if (p == null || p.getKey() == null || p.getValue() == null) { throw new IllegalArgumentException(); } final Number l = p.getKey().getValue(); final Number r = p.getValue().getValue(); return function.apply(l == null ? 0 : l, r == null ? 0 : r); }
public void shortCircuit(Iterator<Token<?>> it, FrameContext.Context context) { while (it.hasNext()) { Token<?> token = it.next(); if (token.getUnderlyingType() == ShortCircuitFrame.class && token.getMultiArgContext() == context) { break; } } } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Token<?> token = (Token<?>) o; if (getValue() != null ? !getValue().equals(token.getValue()) : token.getValue() != null) return false; return getUnderlyingType() != null ? getUnderlyingType().equals(token.getUnderlyingType()) : token.getUnderlyingType() == null; }
@Override @SuppressWarnings("unchecked") public void exitNotFunc(StellarParser.NotFuncContext ctx) { final FrameContext.Context context = getArgContext(); expression.tokenDeque.push(new Token<>((tokenDeque, state) -> { Token<Boolean> arg = (Token<Boolean>) popDeque(tokenDeque); Boolean v = Optional.ofNullable(ConversionUtils.convert(arg.getValue(), Boolean.class)).orElse(false); tokenDeque.push(new Token<>(!v, Boolean.class, context)); }, DeferredFunction.class, context)); }
@Override public void exitFunc_args(StellarParser.Func_argsContext ctx) { final FrameContext.Context context = getArgContext(); expression.tokenDeque.push(new Token<>((tokenDeque, state) -> { LinkedList<Object> args = new LinkedList<>(); while (true) { Token<?> token = popDeque(tokenDeque); if (token.getUnderlyingType().equals(FunctionMarker.class)) { break; } else { args.addFirst(token.getValue()); } } tokenDeque.push(new Token<>(args, List.class, context)); }, DeferredFunction.class, context)); }
if (skipElseCount > 0 && token.getUnderlyingType() == ElseExpr.class) { while (it.hasNext()) { token = it.next(); if (token.getUnderlyingType() == EndConditional.class) { break; if (skipMatchClauses && (token.getUnderlyingType() == MatchClauseEnd.class || token.getUnderlyingType() == MatchClauseCheckExpr.class)) { while (it.hasNext()) { token = it.next(); if (token.getUnderlyingType() == MatchClausesEnd.class) { break; (isBoolean(token, curr.getValue()) || isEmptyList(token, curr.getValue())); if(isFalsey){ curr = new Token<>(false, Boolean.class, curr.getMultiArgContext()); instanceDeque.removeFirst(); instanceDeque.addFirst(curr); if (curr != null && curr.getValue() != null && curr.getValue() instanceof Boolean && ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType())) { if (token.getUnderlyingType() == BooleanArg.class) { if (token.getMultiArgContext() != null && token.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_OR && (Boolean) (curr.getValue())) { FrameContext.Context context = curr.getMultiArgContext();
/** * Determine if a token and value is an empty list in the appropriate conditional context * @param token * @param value * @return */ private boolean isEmptyList(Token<?> token, Object value) { if(value != null && isConditionalContext(token.getUnderlyingType())) { if (value instanceof Iterable) { return Iterables.isEmpty((Iterable) value); } else if (value instanceof Map) { return ((Map) value).isEmpty(); } else { return false; } }else { return false; } }
@Override public void exitList_entity(StellarParser.List_entityContext ctx) { final FrameContext.Context context = getArgContext(); expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { LinkedList<Object> args = new LinkedList<>(); while (true) { Token<?> token = popDeque(tokenDeque); if (token.getUnderlyingType().equals(FunctionMarker.class)) { break; } else { args.addFirst(token.getValue()); } } tokenDeque.push(new Token<>(args, List.class, context)); }, DeferredFunction.class, context)); }
@SuppressWarnings("ReferenceEquality") private void exitLambdaVariables() { Token<?> t = expression.tokenDeque.pop(); LinkedList<String> variables = new LinkedList<>(); for (; !expression.tokenDeque.isEmpty() && t != LAMBDA_VARIABLES; t = expression.tokenDeque.pop()) { variables.addFirst(t.getValue().toString()); } expression.tokenDeque.push(new Token<>(variables, List.class, getArgContext())); }
@Override public int hashCode() { int result = getValue() != null ? getValue().hashCode() : 0; result = 31 * result + (getUnderlyingType() != null ? getUnderlyingType().hashCode() : 0); return result; } }
private List<Function<Long, Predicate<Long>>> getPredicates() { LinkedList<Function<Long, Predicate<Long>>> predicates = new LinkedList<>(); while (true) { Token<?> token = stack.pop(); if (token == LIST_MARKER) { break; } else { predicates.addFirst((Function<Long, Predicate<Long>>) token.getValue()); } } return predicates; }
public static BiFunction<Number, Number, Token<? extends Number>> multiplication(final FrameContext.Context context) { return (Number l, Number r) -> { if (l instanceof Double || r instanceof Double) { return new Token<>(l.doubleValue() * r.doubleValue(), Double.class, context); } else if (l instanceof Float || r instanceof Float) { return new Token<>(l.floatValue() * r.floatValue(), Float.class, context); } else if (l instanceof Long || r instanceof Long) { return new Token<>(l.longValue() * r.longValue(), Long.class, context); } else { return new Token<>(l.intValue() * r.intValue(), Integer.class, context); } }; }
@Override public void exitMap_entity(StellarParser.Map_entityContext ctx) { final FrameContext.Context context = getArgContext(); expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { HashMap<Object, Object> args = new HashMap<>(); Object value = null; for (int i = 0; true; i++) { Token<?> token = popDeque(tokenDeque); if (token.getUnderlyingType().equals(FunctionMarker.class)) { break; } else { if (i % 2 == 0) { value = token.getValue(); } else { args.put(token.getValue(), value); } } } tokenDeque.push(new Token<>(args, Map.class, context)); }, DeferredFunction.class, context)); }
/** * We've set a time interval, which is a value along with a unit. * @param ctx */ @Override public void exitTimeInterval(org.apache.metron.profiler.client.window.generated.WindowParser.TimeIntervalContext ctx) { if(checkForException(ctx)) { return; } Token<?> timeUnit = stack.pop(); Token<?> timeDuration = stack.pop(); long duration = ConversionUtils.convert(timeDuration.getValue(), Long.class); TimeUnit unit = (TimeUnit) timeUnit.getValue(); stack.push(new Token<>(unit.toMillis(duration), Long.class)); }
/** * Get function arguments. * @param token The token containing the function arguments. * @return */ @SuppressWarnings("unchecked") private List<Object> getFunctionArguments(final Token<?> token) { if (token.getUnderlyingType().equals(List.class)) { return (List<Object>) token.getValue(); } else { throw new ParseException("Unable to process in clause because " + token.getValue() + " is not a set"); } }
public static BiFunction<Number, Number, Token<? extends Number>> subtraction(final FrameContext.Context context) { return (Number l, Number r) -> { if (l instanceof Double || r instanceof Double) { return new Token<>(l.doubleValue() - r.doubleValue(), Double.class, context); } else if (l instanceof Float || r instanceof Float) { return new Token<>(l.floatValue() - r.floatValue(), Float.class, context); } else if (l instanceof Long || r instanceof Long) { return new Token<>(l.longValue() - r.longValue(), Long.class, context); } else { return new Token<>(l.intValue() - r.intValue(), Integer.class, context); } }; }
@Override public void exitMatchClauseCheckExpr(StellarParser.MatchClauseCheckExprContext ctx) { final FrameContext.Context context = getArgContext(); // if we are validating, and we have a single variable then we will get // a null and we need to protect against that if(ctx.getStart() == ctx.getStop()) { expression.tokenDeque.push(new Token<>((tokenDeque, state) -> { if (tokenDeque.size() == 1 && (tokenDeque.peek().getValue() == null || tokenDeque.peek().getUnderlyingType() == Boolean.class)) { tokenDeque.pop(); tokenDeque.add(new Token<>(false, Boolean.class, getArgContext())); } }, DeferredFunction.class, context)); } expression.tokenDeque.push(new Token<>(new MatchClauseCheckExpr(), MatchClauseCheckExpr.class, getArgContext())); }
@SuppressWarnings({"unchecked","ReferenceEquality"}) private void exitLambda(boolean hasArgs) { final FrameContext.Context context = getArgContext(); Token<?> t = expression.tokenDeque.pop(); final Deque<Token<?>> instanceDeque = new ArrayDeque<>(); for(; !expression.tokenDeque.isEmpty() && t != EXPRESSION_REFERENCE; t = expression.tokenDeque.pop()) { instanceDeque.addLast(t); } final List<String> variables = hasArgs ? (List<String>) instanceDeque.removeLast().getValue() :new ArrayList<>(); expression.tokenDeque.push(new Token<>((tokenDeque, state) -> { LambdaExpression expr = new LambdaExpression(variables, instanceDeque, state); tokenDeque.push(new Token<>(expr, Object.class, context)); }, DeferredFunction.class, context)); }