Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1134

Add methods to check numbers against NaN and inifinite to Validate

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.4
    • 3.5
    • lang.*
    • None

    Description

      These are suggestions for new methods for the Validate class.

      Floating point values

      notNaN(value)

      Throws an exception if value != value .

      double value;
      
      value = Double.NaN;
      Validate.notNaN(value);    // Throws exception
      
      value = 1.0;
      Validate.notNaN(value);    // Validates
      
      value = Double.POSITIVE_INFINITY;
      Validate.notNaN(value);    // Validates

      finite(value)

      Validates that the argument contains a numeric value (not NaN or infinite).

      double value;
      
      value = Double.NaN;
      Validate.finite(value);    // Throws exception
      
      value = Double.POSITIVE_INFINITY;
      Validate.finite(value);    // Throws exception
      
      value = 1.0;
      Validate.finite(value);    // Validates

      Integers and floats

      The following methods are overloaded to accept both integers and floating point values.

      greater(reference, value), greaterOrEqual(reference, value)

      Ensures the argument is greater than (or equal to) a given value.

      double value;
      
      value = 0.0;
      Validate.greater(0.0, value);    // Throws exception
      Validate.greaterOrEqual(0.0, value); // Validates
      
      value = Double.POSITIVE_INFINITY;
      Validate.greater(0.0, value);    // Validates
      
      value = Double.NaN;
      Validate.greater(0.0, value);    // Throws exception

      smaller(reference, value), smallerOrEqual(reference, value)

      Ensures the argument is smaller than (or equal to) a given value. Does the opposite of greater(), see example above.

      different(reference, value)

      Ensures the argument is not equal to a given value. A typical use case would be to accept only non-zero values.

      double value;
      
      value = 0.0;
      Validate.different(0.0, value);    // Throws exception
      Validate.different(1.0, value);    // Validates
      
      value = Double.NaN;
      Validate.different(0.0, value);    // Validates

      Attachments

        Issue Links

          Activity

            People

              chonton Charles Honton
              Stardust Stardust
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: