Example #1
0
  // Reads: key operator value
  private Predicate readComparison(Queue<String> tokens, ParameterList parameters) {
    String key = tokens.poll();
    if (key == null) {
      return null;
    }

    String realKey = Query.Static.getCanonicalKey(key);
    if (realKey != null) {
      key = realKey;
    }

    String operator = tokens.poll();
    boolean isIgnoreCase = false;
    if (operator == null) {
      throw new IllegalArgumentException(String.format("No operator after [%s] key!", key));
    } else {
      operator = operator.toLowerCase(Locale.ENGLISH);
      if (operator.endsWith("[c]")) {
        operator = operator.substring(0, operator.length() - 3);
        isIgnoreCase = true;
      }
    }

    Object value = readValue(tokens);
    if (value == null) {
      throw new IllegalArgumentException(
          String.format("No value after [%s] key and [%s] operator!", key, operator));

    } else if (value instanceof String) {
      String valueString = (String) value;

      if (valueString.startsWith("?")) {
        if (valueString.length() == 1) {
          value = parameters.poll();

        } else {
          String path = valueString.substring(1);
          int slashAt = path.indexOf('/');
          String splitIndex;
          String splitPath;

          if (slashAt > -1) {
            splitIndex = path.substring(0, slashAt);
            splitPath = path.substring(slashAt + 1);
          } else {
            splitIndex = path;
            splitPath = "";
          }

          Integer index = ObjectUtils.to(Integer.class, splitIndex);

          if (index == null) {
            index = 0;
          } else {
            path = splitPath;
          }

          value = index < parameters.size() ? parameters.get(index) : null;

          if (value != null && path.length() > 0) {
            if (value instanceof Recordable) {
              value = ((Recordable) value).getState().getByPath(path);
            } else {
              value = CollectionUtils.getByPath(value, path);
            }
          }
        }

      } else if ("true".equalsIgnoreCase(valueString)) {
        value = Boolean.TRUE;

      } else if ("false".equalsIgnoreCase(valueString)) {
        value = Boolean.FALSE;

      } else if ("null".equalsIgnoreCase(valueString)) {
        value = null;

      } else if ("missing".equalsIgnoreCase(valueString)) {
        value = Query.MISSING_VALUE;
      }
    }

    String comparisonOperator = getComparisonOperators().get(operator);
    if (comparisonOperator != null) {
      return new ComparisonPredicate(
          comparisonOperator, isIgnoreCase, key, ObjectUtils.to(Iterable.class, value));

    } else {
      throw new IllegalArgumentException(
          String.format("[%s] isn't a valid comparison operator!", operator));
    }
  }