Exemplo n.º 1
0
  public void testEmpty() {
    String testStr = "";
    assertTrue(StringUtil.isEmpty(testStr));

    testStr = null;
    assertTrue(StringUtil.isEmpty(testStr));
  }
  private static boolean allRangeComponentsAreEmpty(
      String opLeft, String valLeft, String opRight, String valRight) {
    boolean fromOpBlank = StringUtil.isEmpty(opLeft);
    boolean toOpBlank = StringUtil.isEmpty(opRight);
    boolean fromValueBlank = StringUtil.isEmpty(valLeft);
    boolean toValueBlank = StringUtil.isEmpty(valRight);

    return (fromOpBlank && toOpBlank && fromValueBlank && toValueBlank);
  }
Exemplo n.º 3
0
  public void testNotEmpty() {
    String testStr = "x";
    assertTrue(!StringUtil.isEmpty(testStr));

    testStr = "notherstring";
    assertTrue(!StringUtil.isEmpty(testStr));

    testStr = " ";
    assertTrue(!StringUtil.isEmpty(testStr));
  }
  /**
   * This method validates a range of numeric values.
   *
   * <p>Each side of the range includes a relational operator (>,>=,<,<=,=) and the value. The value
   * can include a decimal number with an optional space and then anything to the right of the space
   * is ignored (like units: mm).
   *
   * <p>Though vacuous, if neither side is specified, the range is considered to be valid. Not
   * counting when nothing is specified, the left "side" is required, and the right side is
   * optional. When specifying a side, both the operator and value must be specified.
   *
   * <p>When the range is ok, null is returned. Otherwise a cryptic string is returned.... this
   * string is a key to a resource bundle or something? no clue.
   *
   * <p>(Apparently) bad things about this method that am afraid to change:
   *
   * <ul>
   *   <li>This is a ** validation ** method whose main point is to return a _message_ if something
   *       is wrong, but if the value isn't a decimal number it throws an exception instead of
   *       returning a message.
   *   <li>The left value being 0 is considered an error. What about negative numbers!!!!?!?!!?!??!?
   *   <li>when an operator is = and there is both a left and right side this method considers that
   *       valid....
   * </ul>
   */
  public static String validateRange(
      String opLeft, String valLeft, String opRight, String valRight) {

    logger.debug(
        "validation for range: " + opLeft + "|" + valLeft + "|" + opRight + "|" + valRight);

    if (allRangeComponentsAreEmpty(opLeft, valLeft, opRight, valRight)) {
      return null;
    }

    // Make sure that the left operator and value are filled out
    if (leftSideHasEmptyComponent(opLeft, valLeft)) {
      return ResourceBundleUtil.getString("searchNoRange");
    }

    // Makes sure both right values are either selected or deselected
    if (rightSideHasOnlyOneComponent(opRight, valRight)) {
      return ResourceBundleUtil.getString("searchInvalidOperator");
    }

    // Look for a space in the value (e.g. '0 mm') and ignore anything after the space
    valLeft = stripUnits(valLeft);

    double leftVal = Double.parseDouble(valLeft);

    // Makes sure no values are selected on the right if you are using less
    // than
    if (opLeft.equals(RangeData.LESS_THAN) || opLeft.equals(RangeData.LESS_THAN_EQUAL)) {
      if (!StringUtil.isEmpty(opRight)) {
        return ResourceBundleUtil.getString("searchInvalidRightOperator");
      }

      if (leftVal == 0) {
        return ResourceBundleUtil.getString("searchValueZero");
      }
    } else {
      if (!StringUtil.isEmpty(valRight)) {
        valRight = stripUnits(valRight);

        double rightVal = Double.parseDouble(valRight);

        // Makes sure that the values of the range selected are valid
        if (rightVal <= leftVal) {
          return ResourceBundleUtil.getString("searchInvalidRange");
        }
      }
    }

    // Return null if no errors found
    return null;
  }
  /**
   * This range is "empty" if:
   *
   * <ul>
   *   <li>neither operator is specified (regardless of value??)
   *   <li>assuming both operators are not blank, then if either side has an operator it must have a
   *       value (or be considered empty).
   * </ul>
   *
   * If there's no from:/left side specified, but there's a to:/right side specified, it's NOT empty
   * which doesn't really jive with {@link #validateRange}
   */
  public boolean isEmpty() {
    boolean fromOpBlank = StringUtil.isEmpty(fromOperator);
    boolean toOpBlank = StringUtil.isEmpty(toOperator);
    boolean fromValueBlank = (fromValue == null);
    boolean toValueBlank = (toValue == null);

    if (fromOpBlank && toOpBlank) {
      return true;
    }

    if (!fromOpBlank && fromValueBlank) {
      return true;
    }

    if (!toOpBlank && toValueBlank) {
      return true;
    }

    return false;
  }
 private static boolean rightSideHasOnlyOneComponent(String opRight, String valRight) {
   return StringUtil.isEmpty(opRight) ^ StringUtil.isEmpty(valRight);
 }
 private static boolean leftSideHasEmptyComponent(String opLeft, String valLeft) {
   return StringUtil.isEmpty(opLeft) || StringUtil.isEmpty(valLeft);
 }