Package de.stefanfrings.utils
Class ConvertingHashMap
java.lang.Object
de.stefanfrings.utils.ConvertingHashMap
Wrapper around HashMap which adds type conversion with caching.
For each key, this map stores the String value and the last used other type e.g. String and Integer. When you call a specific getter, then the type is converted automatically, if necessary.
- Author:
- Stefan Frings, http://stefanfrings.de/javautils
-
Nested Class Summary
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) boolean
entrySet()
getBigDecimal
(String key, BigDecimal defaultValue) Get an object a BigDecimal.getBigDecimalList
(String key, List<BigDecimal> defaultValue) Get a comma separated list of BigDecimal numbers.getBigInteger
(String key, BigInteger defaultValue) Get an object a BigInteger.getBigIntegerList
(String key, List<BigInteger> defaultValue) Get a comma separated list of BigInteger numbers.getBoolean
(String key, Boolean defaultValue) Get an entry as Boolean.getCalendar
(String key, Calendar defaultValue) Get an entry as GregorianCalendar.Get an entry as Date.getDateList
(String key, List<Date> defaultValue) Get a comma separated list of dates.Get an entry as Double.getDoubleList
(String key, List<Double> defaultValue) Get a comma separated list of double numbers.Get an entry as Float.getFloatList
(String key, List<Float> defaultValue) Get a comma separated list of float numbers.getInteger
(String key, Integer defaultValue) Get an entry as Integer.getIntegerList
(String key, List<Integer> defaultValue) Get a comma separated list of integer numbers.Get an entry as Long.getLongList
(String key, List<Long> defaultValue) Get a comma separated list of long integer numbers.Get an entry as String.getStringList
(String key, List<String> defaultValue) Get a comma separated list of strings.getXMLGregorianCalendar
(String key, XMLGregorianCalendar defaultValue) Get an entry as XMLGregorianCalendar.<T> T
getXmlObject
(String key, Class<T> type, T defaultValue) Get an entry as XmlElement or XmlRootElement object.boolean
isEmpty()
keySet()
void
Add or replace an entry.void
void
putAllObjects
(Map<? extends String, ? extends Object> map) Add or replace many elements.void
putIfNotEmpty
(String key, Object o) Add or replace an entry, but only if the value is not null and no empty string.void
putXmlObject
(String key, Object o) Add or replace an entry.int
size()
values()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
DATE_TIME_PATTERN
Pattern used to represent a date with time.- See Also:
-
DATE_PATTERN
Pattern used to represent a date without time.- See Also:
-
-
Constructor Details
-
ConvertingHashMap
public ConvertingHashMap()
-
-
Method Details
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKey
in interfaceMap<String,
String>
-
containsValue
- Specified by:
containsValue
in interfaceMap<String,
String>
-
get
-
put
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
put
Add or replace an entry.- Parameters:
key
- Key to identify the entry.o
- Value of the entry.
-
putXmlObject
Add or replace an entry. Use this method to convert the object to an XML string.- Parameters:
key
- Key to identify the entry.o
- Value of the entry.- Throws:
javax.xml.bind.JAXBException
- If the XML conversion failed
-
putIfNotEmpty
Add or replace an entry, but only if the value is not null and no empty string. If the value is empty, then the map remains unchanged.- Parameters:
key
- Key to identify the entry.o
- Value of the entry.
-
putAllObjects
Add or replace many elements.- Parameters:
map
- Provides the source keys and objects.
-
getString
Get an entry as String.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
-
getInteger
Get an entry as Integer.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getLong
Get an entry as Long.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getBoolean
Get an entry as Boolean.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
-
getBigDecimal
Get an object a BigDecimal.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getBigInteger
Get an object a BigInteger.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getFloat
Get an entry as Float.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getDouble
Get an entry as Double.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getDate
Get an entry as Date.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
ParseException
- if the string does not contain a parsable date.
-
getCalendar
Get an entry as GregorianCalendar.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
ParseException
- if the string does not contain a parsable date.
-
getXMLGregorianCalendar
public XMLGregorianCalendar getXMLGregorianCalendar(String key, XMLGregorianCalendar defaultValue) throws ParseException, DatatypeConfigurationException Get an entry as XMLGregorianCalendar.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
ParseException
- if the string does not contain a parsable date.DatatypeConfigurationException
- Should never happen
-
getXmlObject
public <T> T getXmlObject(String key, Class<T> type, T defaultValue) throws javax.xml.bind.JAXBException Get an entry as XmlElement or XmlRootElement object.- Type Parameters:
T
- the object type to return (must be an Jaxb XmlElemeent or XmlRootElement)- Parameters:
key
- Key to identify the entry.type
- the object type to return (must be an Jaxb XmlElemeent or XmlRootElement)defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The objects value.
- Throws:
javax.xml.bind.JAXBException
- If the XML conversion failed
-
getStringList
Get a comma separated list of strings.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The list of strings.
-
getIntegerList
public List<Integer> getIntegerList(String key, List<Integer> defaultValue) throws NumberFormatException Get a comma separated list of integer numbers.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The list of integer numbers.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getLongList
Get a comma separated list of long integer numbers.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The list of long integer numbers.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getFloatList
Get a comma separated list of float numbers.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The list of float numbers.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getDoubleList
public List<Double> getDoubleList(String key, List<Double> defaultValue) throws NumberFormatException Get a comma separated list of double numbers.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The list of double numbers.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getBigIntegerList
public List<BigInteger> getBigIntegerList(String key, List<BigInteger> defaultValue) throws NumberFormatException Get a comma separated list of BigInteger numbers.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The list of BigInteger numbers.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getBigDecimalList
public List<BigDecimal> getBigDecimalList(String key, List<BigDecimal> defaultValue) throws NumberFormatException Get a comma separated list of BigDecimal numbers.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The list of BigDecimal numbers.
- Throws:
NumberFormatException
- if the string does not contain a parsable number.
-
getDateList
Get a comma separated list of dates.- Parameters:
key
- Key to identify the entry.defaultValue
- Default value to return if the key is unknown or the value is null or empty.- Returns:
- The list of BigDecimal numbers.
- Throws:
ParseException
- if the string does not contain a parsable date.
-