Package eu.woolplatform.utils.validation
Class MapReader
- java.lang.Object
-
- eu.woolplatform.utils.validation.MapReader
-
public class MapReader extends Object
This class can read values from a map with string keys and perform validations and type conversions.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanreadBoolean(String key)Reads a value as a boolean.BooleanreadBoolean(String key, Boolean defaultVal)Reads a value as a boolean.doublereadDouble(String key)Reads a value as a double.DoublereadDouble(String key, Double defaultVal)Reads a value as a double.<T> TreadEnum(String key, Class<T> enumClass)intreadInt(String key)Reads a value as an integer.IntegerreadInt(String key, Integer defaultVal)Reads a value as an integer.intreadIntRange(String key, Integer min, Integer max)Reads a value as an integer and validates that it's in the specified range.IntegerreadIntRange(String key, Integer min, Integer max, Integer defaultVal)Reads a value as an integer and validates that it's in the specified range (if not null).<T> TreadJson(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeRef)Reads a value and converts it to the specified type using the Jackson ObjectMapper.<T> TreadJson(String key, com.fasterxml.jackson.core.type.TypeReference<T> typeRef, T defaultVal)Reads a value and converts it to the specified type using the Jackson ObjectMapper.<T> TreadJson(String key, Class<T> clazz)Reads a value and converts it to the specified class using the Jackson ObjectMapper.<T> TreadJson(String key, Class<T> clazz, T defaultVal)Reads a value and converts it to the specified class using the Jackson ObjectMapper.longreadLong(String key)Reads a value as a long.LongreadLong(String key, Long defaultVal)Reads a value as a long.longreadLongRange(String key, Long min, Long max)Reads a value as a long and validates that it's in the specified range.LongreadLongRange(String key, Long min, Long max, Long defaultVal)Reads a value as a long and validates that it's in the specified range (if not null).ObjectreadObject(String key)Reads a value as an object.ObjectreadObject(String key, Object defaultVal)Reads a value as an object.StringreadString(String key)Reads a value as a string.StringreadString(String key, String defaultVal)Reads a value as a string.StringreadStringLength(String key, int minLen, int maxLen)Reads a value as a string and validates the string length.StringreadStringLength(String key, int minLen, int maxLen, String defaultVal)Reads a value as a string and validates the string length (if not null).StringreadStringRegex(String key, String regex)Reads a value as a string and validates it against a regular expression.StringreadStringRegex(String key, String regex, String defaultVal)Reads a value as a string and validates it against a regular expression (if not null).
-
-
-
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 keydefaultVal- 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 keydefaultVal- 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 keyminLen- 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 keyminLen- 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 keyregex- 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 keyregex- the regular expressiondefaultVal- 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
-
readEnum
public <T> T readEnum(String key, Class<T> enumClass) throws ParseException
- Throws:
ParseException
-
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 keydefaultVal- 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 keymin- 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 keymin- 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 keydefaultVal- 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 keymin- 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 keymin- 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 keydefaultVal- 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 keydefaultVal- 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 keyclazz- 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 keyclazz- the return typedefaultVal- 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 keytypeRef- 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 keytypeRef- the return typedefaultVal- 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
-
-