Class MapReader


  • public class MapReader
    extends Object
    This class can read values from a map with string keys and perform validations and type conversions.
    • Constructor Detail

      • MapReader

        public MapReader​(Map<?,​?> map)
        Constructs a new reader that will read from the specified map.
        Parameters:
        map - the map
    • Method Detail

      • readObject

        public Object readObject​(String key)
                          throws ParseException
        Reads a value as an object. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined or the value is null
      • readObject

        public Object readObject​(String key,
                                 Object defaultVal)
        Reads a value as an object. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
      • readString

        public String readString​(String key)
                          throws ParseException
        Reads a value as a string. It calls toString() on any object. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined or the value is null
      • readString

        public String readString​(String key,
                                 String defaultVal)
        Reads a value as a string. It calls toString() on any object. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - they key
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
      • readStringLength

        public String readStringLength​(String key,
                                       int minLen,
                                       int maxLen)
                                throws ParseException
        Reads a value as a string and validates the string length. It calls toString() on any object. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        minLen - the minimum length (<= 0 if no minimum)
        maxLen - the maximum length (<= 0 if no maximum)
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined or the value is null or the string length is invalid
      • readStringLength

        public String readStringLength​(String key,
                                       int minLen,
                                       int maxLen,
                                       String defaultVal)
                                throws ParseException
        Reads a value as a string and validates the string length (if not null). It calls toString() on any object. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        minLen - the minimum length (<= 0 if no minimum)
        maxLen - the maximum length (<= 0 if no maximum)
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and the string length is invalid
      • readStringRegex

        public String readStringRegex​(String key,
                                      String regex)
                               throws ParseException
        Reads a value as a string and validates it against a regular expression. It calls toString() on any object. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        regex - the regular expression
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined or the value is null or it does not match the regular expression
      • readStringRegex

        public String readStringRegex​(String key,
                                      String regex,
                                      String defaultVal)
                               throws ParseException
        Reads a value as a string and validates it against a regular expression (if not null). It calls toString() on any object. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        regex - the regular expression
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and it does not match the regular expression
      • readInt

        public int readInt​(String key)
                    throws ParseException
        Reads a value as an integer. If the value is a Number, it will get the integer value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined, or the value is null, or it can't be converted to an integer
      • readInt

        public Integer readInt​(String key,
                               Integer defaultVal)
                        throws ParseException
        Reads a value as an integer. If the value is a Number, it will get the integer value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and can't be converted to an integer
      • readIntRange

        public int readIntRange​(String key,
                                Integer min,
                                Integer max)
                         throws ParseException
        Reads a value as an integer and validates that it's in the specified range. If the value is a Number, it will get the integer value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        min - the minimum value (null if no minimum)
        max - the maximum value (null if no maximum)
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined, or the value is null, or it can't be converted to an integer, or it's not in the specified range
      • readIntRange

        public Integer readIntRange​(String key,
                                    Integer min,
                                    Integer max,
                                    Integer defaultVal)
                             throws ParseException
        Reads a value as an integer and validates that it's in the specified range (if not null). If the value is a Number, it will get the integer value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        min - the minimum value (null if no minimum)
        max - the maximum value (null if no maximum)
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and it can't be converted to an integer or it's not in the specified range
      • readLong

        public long readLong​(String key)
                      throws ParseException
        Reads a value as a long. If the value is a Number, it will get the long value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined, or the value is null, or it can't be converted to a long
      • readLong

        public Long readLong​(String key,
                             Long defaultVal)
                      throws ParseException
        Reads a value as a long. If the value is a Number, it will get the long value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and can't be converted to a long
      • readLongRange

        public long readLongRange​(String key,
                                  Long min,
                                  Long max)
                           throws ParseException
        Reads a value as a long and validates that it's in the specified range. If the value is a Number, it will get the long value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        min - the minimum value (null if no minimum)
        max - the maximum value (null if no maximum)
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined, or the value is null, or it can't be converted to a long, or it's not in the specified range
      • readLongRange

        public Long readLongRange​(String key,
                                  Long min,
                                  Long max,
                                  Long defaultVal)
                           throws ParseException
        Reads a value as a long and validates that it's in the specified range (if not null). If the value is a Number, it will get the long value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        min - the minimum value (null if no minimum)
        max - the maximum value (null if no maximum)
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and it can't be converted to a long or it's not in the specified range
      • readDouble

        public double readDouble​(String key)
                          throws ParseException
        Reads a value as a double. If the value is a Number, it will get the double value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined, or the value is null, or it can't be converted to a double
      • readDouble

        public Double readDouble​(String key,
                                 Double defaultVal)
                          throws ParseException
        Reads a value as a double. If the value is a Number, it will get the double value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and can't be converted to a double
      • readBoolean

        public boolean readBoolean​(String key)
                            throws ParseException
        Reads a value as a boolean. If the value is a Boolean, it will get the boolean value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method throws an exception.
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined, or the value is null, or it can't be converted to a boolean
      • readBoolean

        public Boolean readBoolean​(String key,
                                   Boolean defaultVal)
                            throws ParseException
        Reads a value as a boolean. If the value is a Boolean, it will get the boolean value. Otherwise it will call toString() and try to parse the string value. If the key is not defined or the value is null, this method returns the default value.
        Parameters:
        key - the key
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and can't be converted to a boolean
      • readJson

        public <T> T readJson​(String key,
                              Class<T> clazz)
                       throws ParseException
        Reads a value and converts it to the specified class using the Jackson ObjectMapper. For example a Map could be converted to an object. This method does not parse JSON strings. If the key is not defined or the value is null, this method throws an exception.
        Type Parameters:
        T - the return type
        Parameters:
        key - the key
        clazz - the return type
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined, or the value is null, or it can't be converted to the specified type
      • readJson

        public <T> T readJson​(String key,
                              Class<T> clazz,
                              T defaultVal)
                       throws ParseException
        Reads a value and converts it to the specified class using the Jackson ObjectMapper. For example a Map could be converted to an object. This method does not parse JSON strings. If the key is not defined or the value is null, this method returns the default value.
        Type Parameters:
        T - the return type
        Parameters:
        key - the key
        clazz - the return type
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and can't be converted to the specified type
      • readJson

        public <T> T readJson​(String key,
                              com.fasterxml.jackson.core.type.TypeReference<T> typeRef)
                       throws ParseException
        Reads a value and converts it to the specified type using the Jackson ObjectMapper. For example a Map could be converted to an object. This method does not parse JSON strings. If the key is not defined or the value is null, this method throws an exception.

        If you want to convert to MyObject, you can specify:
        new TypeReference<MyObject>() {}

        Type Parameters:
        T - the return type
        Parameters:
        key - the key
        typeRef - the return type
        Returns:
        the value
        Throws:
        ParseException - if the key is not defined, or the value is null, or it can't be converted to the specified type
      • readJson

        public <T> T readJson​(String key,
                              com.fasterxml.jackson.core.type.TypeReference<T> typeRef,
                              T defaultVal)
                       throws ParseException
        Reads a value and converts it to the specified type using the Jackson ObjectMapper. For example a Map could be converted to an object. This method does not parse JSON strings. If the key is not defined or the value is null, this method returns the default value.

        If you want to convert to MyObject, you can specify:
        new TypeReference<MyObject>() {}

        Type Parameters:
        T - the return type
        Parameters:
        key - the key
        typeRef - the return type
        defaultVal - the default value (can be null)
        Returns:
        the value (can only be null if defaultVal is null)
        Throws:
        ParseException - if the value is not null and can't be converted to the specified type