Ejemplo n.º 1
2
  @Override
  public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    Predicate predicate = cb.conjunction();
    List<Expression<Boolean>> expressions = predicate.getExpressions();

    if (isNotBlank(criteria.getFilter())) {
      expressions.add(
          cb.or(
              cb.like(
                  cb.lower(root.<String>get(Role_.roleName)),
                  wildcardsAndLower(criteria.getFilter())),
              cb.like(
                  cb.lower(root.<String>get(Role_.roleDesc)),
                  wildcardsAndLower(criteria.getFilter()))));
    }

    if (isNotBlank(criteria.getRoleName())) {
      expressions.add(
          cb.like(
              cb.lower(root.<String>get(Role_.roleName)),
              wildcardsAndLower(criteria.getRoleName())));
    }
    if (isNotBlank(criteria.getRoleDesc())) {
      expressions.add(
          cb.like(
              cb.lower(root.<String>get(Role_.roleDesc)),
              wildcardsAndLower(criteria.getRoleDesc())));
    }
    if (null != criteria.getIsSys()) {
      expressions.add(cb.equal(root.<Boolean>get(Role_.isSys), criteria.getIsSys()));
    }
    return predicate;
  }
Ejemplo n.º 2
0
  public void testBasic() {
    final Predicate REFERENCE = new Predicate(SYMBOL, ARITY);

    assertEquals(
        "getPredicateSymbol doesn't work properly", SYMBOL, REFERENCE.getPredicateSymbol());
    assertEquals("getArity doesn't work properly", ARITY, REFERENCE.getArity());
  }
Ejemplo n.º 3
0
 public void assertEqualHashCode(
     Predicate<? super Integer> expected, Predicate<? super Integer> actual) {
   assertEquals(
       actual.toString() + " should hash like " + expected.toString(),
       expected.hashCode(),
       actual.hashCode());
 }
Ejemplo n.º 4
0
  @GwtIncompatible("Predicates.containsPattern")
  public void testContains_apply() {
    Predicate<CharSequence> isFoobar = Predicates.contains(Pattern.compile("^Fo.*o.*bar$"));

    assertTrue(isFoobar.apply("Foxyzoabcbar"));
    assertFalse(isFoobar.apply("Foobarx"));
  }
  private Predicate substFromTheata(AllSubstitution theta, Predicate first) {
    Predicate newGoal = new Predicate();
    newGoal.fnName = first.fnName;
    newGoal.args = new ArrayList<String>();

    List<Substitution> substList = theta.SubstList;
    List<String> args = first.args;

    boolean found;
    for (String str : args) {
      found = false;
      for (Substitution substitution : substList) {

        if (str.equals(substitution.var)) {
          newGoal.args.add(substitution.val);
          found = true;
          break;
        }
      }

      if (!found) {
        newGoal.args.add(str);
      }
    }
    return newGoal;
  }
Ejemplo n.º 6
0
  @Test
  public void testNotNull() throws Exception {
    Predicate<Object> notNull = Predicates.notNull();

    assertTrue(notNull.test("Hallo"));
    assertFalse(notNull.test(null));
  }
Ejemplo n.º 7
0
 public void testOr_listDefensivelyCopied() {
   List<Predicate<Object>> list = newArrayList();
   Predicate<Object> predicate = Predicates.or(list);
   assertFalse(predicate.apply(1));
   list.add(Predicates.alwaysTrue());
   assertFalse(predicate.apply(1));
 }
Ejemplo n.º 8
0
  public void testIsEqualTo_apply() {
    Predicate<Integer> isOne = Predicates.equalTo(1);

    assertTrue(isOne.apply(1));
    assertFalse(isOne.apply(2));
    assertFalse(isOne.apply(null));
  }
Ejemplo n.º 9
0
 @GwtIncompatible("SerializableTester")
 @SuppressWarnings("unchecked") // varargs
 public void testOr_serializationIterable() {
   Predicate<Integer> pre = Predicates.or(Arrays.asList(TRUE, FALSE));
   Predicate<Integer> post = SerializableTester.reserializeAndAssert(pre);
   assertEquals(pre.apply(0), post.apply(0));
 }
Ejemplo n.º 10
0
  @Test
  public void testIsNull() throws Exception {
    Predicate<Object> isNull = Predicates.isNull();

    assertTrue(isNull.test(null));
    assertFalse(isNull.test("test"));
  }
Ejemplo n.º 11
0
 public void visitAndExpr(OpAnd and) {
   if (predicates > 0) {
     Expression parent = and.getParent();
     if (!(parent instanceof PathExpr)) {
       LOG.warn("Parent expression of boolean operator is not a PathExpr: " + parent);
       return;
     }
     PathExpr path;
     Predicate predicate;
     if (parent instanceof Predicate) {
       predicate = (Predicate) parent;
       path = predicate;
     } else {
       path = (PathExpr) parent;
       parent = path.getParent();
       if (!(parent instanceof Predicate) || path.getLength() > 1) {
         LOG.warn(
             "Boolean operator is not a top-level expression in the predicate: "
                 + parent.getClass().getName());
         return;
       }
       predicate = (Predicate) parent;
     }
     if (LOG.isTraceEnabled())
       LOG.trace("Rewriting boolean expression: " + ExpressionDumper.dump(and));
     hasOptimized = true;
     LocationStep step = (LocationStep) predicate.getParent();
     Predicate newPred = new Predicate(context);
     newPred.add(and.getRight());
     step.insertPredicate(predicate, newPred);
     path.replaceExpression(and, and.getLeft());
   }
 }
Ejemplo n.º 12
0
 @SuppressWarnings("unchecked") // varargs
 public void testOr_arrayDefensivelyCopied() {
   Predicate[] array = {Predicates.alwaysFalse()};
   Predicate<Object> predicate = Predicates.or(array);
   assertFalse(predicate.apply(1));
   array[0] = Predicates.alwaysTrue();
   assertFalse(predicate.apply(1));
 }
Ejemplo n.º 13
0
 public StringBuilder makeStr(StringBuilder sb, String jname, String rel) {
   sb.append("(");
   _l.toJavaStr(sb, jname);
   sb.append(" ").append(rel).append(" ");
   _r.toJavaStr(sb, jname);
   sb.append(")");
   return sb;
 }
Ejemplo n.º 14
0
  @Test
  public void testAlwaysTrue() {
    Predicate<Object> alwaysTrue = Predicates.alwaysTrue();

    assertTrue(alwaysTrue.test(null));
    assertTrue(alwaysTrue.test("Hallo"));
    assertTrue(alwaysTrue.test(55l));
  }
Ejemplo n.º 15
0
 @Override
 boolean eval(Object left, final Object right, final PredicateContext ctx) {
   PredicateContextImpl pci = (PredicateContextImpl) ctx;
   Predicate exp = (Predicate) left;
   return exp.apply(
       new PredicateContextImpl(
           right, ctx.root(), ctx.configuration(), pci.documentPathCache()));
 }
Ejemplo n.º 16
0
 private void validate(Object key, Object value) {
   if (!keyPredicate.evaluate(key)) {
     throw new IllegalArgumentException("Cannot add key - Predicate rejected it");
   }
   if (!valuePredicate.evaluate(value)) {
     throw new IllegalArgumentException("Cannot add value - Predicate rejected it");
   }
 }
Ejemplo n.º 17
0
  @Test
  public void testAlwaysFalse() {
    Predicate<Object> alwaysFalse = Predicates.alwaysFalse();

    assertFalse(alwaysFalse.test(null));
    assertFalse(alwaysFalse.test("FooBar"));
    assertFalse(alwaysFalse.test(66d));
  }
Ejemplo n.º 18
0
 /* (non-Javadoc)
  * @see org.exist.xquery.Expression#resetState()
  */
 public void resetState(boolean postOptimization) {
   super.resetState(postOptimization);
   expression.resetState(postOptimization);
   for (Iterator i = predicates.iterator(); i.hasNext(); ) {
     Predicate pred = (Predicate) i.next();
     pred.resetState(postOptimization);
   }
 }
Ejemplo n.º 19
0
 /* (non-Javadoc)
  * @see org.exist.xquery.Expression#analyze(org.exist.xquery.AnalyzeContextInfo)
  */
 public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
   contextInfo.setParent(this);
   expression.analyze(contextInfo);
   for (Iterator i = predicates.iterator(); i.hasNext(); ) {
     Predicate pred = (Predicate) i.next();
     pred.analyze(contextInfo);
   }
 }
 @Override
 public boolean isSatisfied(DataType data) {
   for (final Predicate<DataType> condition : conditions) {
     if (condition.isSatisfied(data)) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 21
0
  @GwtIncompatible("Predicates.instanceOf")
  public void testIsInstanceOf_subclass() {
    Predicate<Object> isNumber = Predicates.instanceOf(Number.class);

    assertTrue(isNumber.apply(1));
    assertTrue(isNumber.apply(2.0f));
    assertFalse(isNumber.apply(""));
    assertFalse(isNumber.apply(null));
  }
Ejemplo n.º 22
0
  @GwtIncompatible("Predicates.instanceOf")
  public void testIsInstanceOf_interface() {
    Predicate<Object> isComparable = Predicates.instanceOf(Comparable.class);

    assertTrue(isComparable.apply(1));
    assertTrue(isComparable.apply(2.0f));
    assertTrue(isComparable.apply(""));
    assertFalse(isComparable.apply(null));
  }
Ejemplo n.º 23
0
 /**
  * Checks a binary predicate for a type matching the type parameters.
  *
  * @param pred a predicate to check.
  * @param type1 the proposed type of parameter 1.
  * @param type2 the proposed type of parameter 2.
  * @return true if <code>pred</code> is a binary predicate that matches the types.
  */
 protected static String checkTypes(
     Predicate pred, Predicate.ParamType type1, Predicate.ParamType type2) {
   if (pred.getArity() != 2) return formatError(pred, "expected 2 arguments");
   else if (pred.getParamType(0) != type1)
     return formatError(pred, "argument 1 should be type " + type1);
   else if (pred.getParamType(1) != type2)
     return formatError(pred, "argument 2 should be type " + type2);
   return null;
 }
Ejemplo n.º 24
0
  public void testIn_apply() {
    Collection<Integer> nums = Arrays.asList(1, 5);
    Predicate<Integer> isOneOrFive = Predicates.in(nums);

    assertTrue(isOneOrFive.apply(1));
    assertTrue(isOneOrFive.apply(5));
    assertFalse(isOneOrFive.apply(3));
    assertFalse(isOneOrFive.apply(null));
  }
Ejemplo n.º 25
0
  /**
   * Attempts to evalutate or assert the predicate by searching for and executing a method named
   * "a_[PredicateName]" (where [PredicateName] is the name of the Predicate object <code>pred
   * </code>) and that has a parameter type list that matches the predicate's parameter type list.
   *
   * @param isAssert true if this is to be interpreted as an assertion, false if it's to be treated
   *     as an evaluation.
   * @param pred a predicate object to assert or evaluate.
   * @return the value returned by the method described above; false if something when wrong.
   */
  private boolean assert_or_eval(boolean isAssert, Predicate pred) {
    currentPred = pred;
    String predName = pred.getName();
    if (predName == null || predName.length() == 0) return false;
    String methodName = (isAssert ? "a_" : "e_") + predName.replaceAll("-", "_");

    int arity = pred.getArity();
    Class<?>[] paramTypes = new Class[arity];
    Object[] paramValues = new Object[arity];
    for (int i = 0; i < arity; i++) {
      switch ((Predicate.ParamType) pred.getParamType(i)) {
        case STRING:
          paramTypes[i] = String.class;
          paramValues[i] = pred.getStringParam(i);
          break;
        case SET:
          paramTypes[i] = TreeSet.class;
          paramValues[i] = pred.getSetParam(i);
          break;
        case LONG:
          paramTypes[i] = Long.class;
          paramValues[i] = pred.getLongParam(i);
          break;
        default:
          error(
              (isAssert ? "!" : "")
                  + pred.toString()
                  + ": Unrecognized argument type argument "
                  + i
                  + " in Predicate");
          return false;
      }
    }

    try {
      Method m = this.getClass().getMethod(methodName, paramTypes);
      Object ret = m.invoke(this, paramValues);
      if (ret instanceof Boolean) return ((Boolean) ret).booleanValue();
    } catch (InvocationTargetException e) {
      error(
          formatLineNumber()
              + (isAssert ? "!" : "")
              + pred.toString()
              + ": InvocationTargetException ("
              + e.getCause()
              + ")");
      e.getTargetException().printStackTrace();
    } catch (Exception e) {
      error(
          formatLineNumber()
              + (isAssert ? "!" : "")
              + pred.toString()
              + ": predicate not found or failed ("
              + e.toString()
              + ")");
    }

    return false;
  }
Ejemplo n.º 26
0
 @Subroutine("substring-no-properties")
 public static LispString substringNoProperties(
     LispString string, @Optional LispObject from, LispObject to) {
   int length = string.size();
   int start = Predicate.isNil(from) ? 0 : processBound(getInt(from), length);
   int end = Predicate.isNil(to) ? length : processBound(getInt(to), length);
   try {
     return string.substring(start, end, false);
   } catch (IndexOutOfBoundsException e) {
     throw new ArgumentOutOfRange(string, start, end);
   }
 }
Ejemplo n.º 27
0
 /**
  * Attempts to assert a predicate, created from <code>line</code> using {@link
  * #makePredicate(String)}. If the line is empty, just contains an comment, or the predicate is is
  * malformed, not attempt is made to call {@link #assert_(Predicate)}.
  *
  * @param line the line to make a predicate from.
  */
 public void assert_(String line) {
   if (line != null) {
     line = line.trim();
     if (line.length() > 0) {
       Predicate p = makePredicate(line);
       if (p != null && p.getName() != null) {
         // System.out.println(p.toString());
         assert_(p);
       }
     }
   }
 }
Ejemplo n.º 28
0
  /* We will simply create a scan operator for every relation in the query. */
  void construct_scan_operators() {
    scan_operators = new Vector<ScanOperator>();

    for (BaseRelationSchema rs : query_relations) {
      /* There may be predicates involving just that relation. */
      Vector<Predicate> v = new Vector<Predicate>();

      for (Predicate p : query_predicates) if (p.isScanPredicate(rs)) v.add(p);

      scan_operators.add(new ScanOperator(rs, v));
    }
  }
Ejemplo n.º 29
0
 /**
  * Attempts to evaluate a predicate, created from <code>line</code> using {@link
  * #makePredicate(String)}. If the line is empty, just contains an comment, or the predicate is is
  * malformed, not attempt is made to call {@link #eval(Predicate)}, and false is returned.
  *
  * @param line the line to make a predicate from.
  * @return false if there's problems (above) or there is no such predicate; the return value of
  *     the predicate interpreter method otherwise.
  */
 public boolean eval(String line) {
   if (line != null) {
     line = line.trim();
     if (line.length() > 0) {
       Predicate p = makePredicate(line);
       if (p != null && p.getName() != null) {
         // System.out.println(p.toString());
         return eval(p);
       }
     }
   }
   return false;
 }
Ejemplo n.º 30
0
 public void computeParamTypes(Map<String, Table> tableMap) {
   Predicate currentP = null;
   try {
     for (Predicate p : getBodyP()) {
       currentP = p;
       Table t = tableMap.get(p.name());
       p.computeVarTypes(t);
     }
     // currentP=getHead();
     // getHead().computeVarTypes(tableMap);
   } catch (InternalException e) {
     throw new AnalysisException(e, this, currentP);
   }
 }