Class LogicExpression<E>

  • Type Parameters:
    E - the type of the base expressions
    All Implemented Interfaces:
    com.google.common.base.Predicate<E>, java.util.function.Predicate<E>

    public class LogicExpression<E>
    extends java.lang.Object
    implements com.google.common.base.Predicate<E>
    A logic expression engine that operates over user specified objects.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected LogicExpression​(java.util.List<Expression<E>> expressions)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean apply​(E entity)  
      static <E> Expression.Apply<E> buildAst​(java.util.List<Expression<E>> rpn)
      Compile a rpn list of tokens into an expression tree.
      static <E> LogicExpression<E> compile​(java.lang.String input, com.google.common.base.Function<java.lang.String,​Expression.Arg<E>> factoryDelegate)
      Helper factory method to instantiate a LogicExpression.
      static <E> LogicExpression<E> compile​(java.util.List<Expression<E>> expressions)
      Compile an infix list of tokens into an expression tree.
      java.util.List<java.lang.String> getArgs()
      Return a list of the arguments contained in the expression.
      private void getArgs​(Expression.Apply<?> apply, java.util.List<java.lang.String> args)
      Private helper method to recursively find arguments.
      boolean isEmpty()
      If the expression is empty, it returns true for all inputs.
      static void main​(java.lang.String[] args)
      Iteractively interpret logic statements from stdin such as "true | (true & false)".
      java.util.List<Expression<E>> rpn​(java.util.List<Expression<E>> tokens)
      Converts an infix logic representation into a postfix logic representation.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface com.google.common.base.Predicate

        equals, test
      • Methods inherited from interface java.util.function.Predicate

        and, negate, or
    • Method Detail

      • compile

        public static <E> LogicExpression<E> compile​(java.util.List<Expression<E>> expressions)
        Compile an infix list of tokens into an expression tree.
        Parameters:
        rpn - a list of tokens in infix form.
        Returns:
        an expression tree.
      • compile

        public static <E> LogicExpression<E> compile​(java.lang.String input,
                                                     com.google.common.base.Function<java.lang.String,​Expression.Arg<E>> factoryDelegate)
        Helper factory method to instantiate a LogicExpression.
        Parameters:
        input - The string to parse.
        factoryDelegate - The factory to build tokens.
        Returns:
        a new LogicExpression
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • isEmpty

        public boolean isEmpty()
        If the expression is empty, it returns true for all inputs.
        Returns:
        true iff the expression is empty.
      • apply

        public boolean apply​(E entity)
        Specified by:
        apply in interface com.google.common.base.Predicate<E>
      • buildAst

        public static <E> Expression.Apply<E> buildAst​(java.util.List<Expression<E>> rpn)
        Compile a rpn list of tokens into an expression tree.
        Parameters:
        rpn - a list of tokens in infix form.
        Returns:
        an expression tree.
      • getArgs

        public java.util.List<java.lang.String> getArgs()
        Return a list of the arguments contained in the expression.
        Returns:
      • getArgs

        private void getArgs​(Expression.Apply<?> apply,
                             java.util.List<java.lang.String> args)
        Private helper method to recursively find arguments.
        Parameters:
        apply - the expression tree to search.
        args - the resulting list of arguments.
      • main

        public static void main​(java.lang.String[] args)
        Iteractively interpret logic statements from stdin such as "true | (true & false)".
        Parameters:
        args -