Class IntegerUtil


  • public class IntegerUtil
    extends Object
    Utility methods that will make it easier to implement data validation for integer attributes.
    Version:
    2.0
    Author:
    Nicklas, Enell
    • Constructor Detail

      • IntegerUtil

        public IntegerUtil()
    • Method Detail

      • getInt

        public static int getInt​(String value)
        Get the int value of an String. If the String couldn't be parsed 0 is returned.
        Parameters:
        value - The Integer
        Returns:
        The value of the Integer or 0 if it is not parseable
      • getInt

        public static int getInt​(String value,
                                 int defaultValue)
        Get the int value of an String. If the String couldn't be parsed default value is returned.
        Parameters:
        value - The Integer
        defaultValue - Default value to return if the parsing fails.
        Returns:
        The value of the Integer or defaultValue if it is not parseable
      • getInt

        public static int getInt​(Integer value)
        Get the value of an Integer.
        Parameters:
        value - The Integer
        Returns:
        The value of the Integer or 0 if it is null
      • getInt

        public static int getInt​(Integer value,
                                 int defaultValue)
        Get the value of an Integer.
        Parameters:
        value - The Integer
        defaultValue - The default value to return if the value is null
        Returns:
        The value of the Integer or defaultValue if it is null
      • checkMin

        public static int checkMin​(int value,
                                   String name,
                                   int minValue)
                            throws NumberOutOfRangeException
        Check that an integer is equal or greater than a minimum value.
        Parameters:
        value - The value to check
        name - The name of the attribute to use if an exception is thrown
        minValue - The minimum allowed value
        Returns:
        The same value
        Throws:
        NumberOutOfRangeException - If the value lower than the minimum
      • checkMax

        public static int checkMax​(int value,
                                   String name,
                                   int maxValue)
                            throws NumberOutOfRangeException
        Check that an integer is equal or lower than a maximum value.
        Parameters:
        value - The value to check
        name - The name of the attribute to use if an exception is thrown
        maxValue - The maximum allowed value
        Returns:
        The same value
        Throws:
        NumberOutOfRangeException - If the value greater than the maximum
      • checkMinMax

        public static int checkMinMax​(int value,
                                      String name,
                                      int minValue,
                                      int maxValue)
                               throws NumberOutOfRangeException
        Check that an integer is equal or between a minimum and a maximum value.
        Parameters:
        value - The value to check
        name - The name of the attribute to use if an exception is thrown
        minValue - The minimum allowed value
        maxValue - The maximum allowed value
        Returns:
        The same value
        Throws:
        NumberOutOfRangeException - If the value is outside the range