Package de.stefanfrings.utils
Class ConvertingHashMap
- java.lang.Object
-
- de.stefanfrings.utils.ConvertingHashMap
-
public class ConvertingHashMap extends Object implements Map<String,String>
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
-
-
Field Summary
Fields Modifier and Type Field Description static String
DATE_PATTERN
Pattern used to represent a date without time.static String
DATE_TIME_PATTERN
Pattern used to represent a date with time.
-
Constructor Summary
Constructors Constructor Description ConvertingHashMap()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
boolean
containsKey(Object key)
boolean
containsValue(Object o)
Set<Map.Entry<String,String>>
entrySet()
String
get(Object key)
BigDecimal
getBigDecimal(String key, BigDecimal defaultValue)
Get an object a BigDecimal.List<BigDecimal>
getBigDecimalList(String key, List<BigDecimal> defaultValue)
Get a comma separated list of BigDecimal numbers.BigInteger
getBigInteger(String key, BigInteger defaultValue)
Get an object a BigInteger.List<BigInteger>
getBigIntegerList(String key, List<BigInteger> defaultValue)
Get a comma separated list of BigInteger numbers.Boolean
getBoolean(String key, Boolean defaultValue)
Get an entry as Boolean.Calendar
getCalendar(String key, Calendar defaultValue)
Get an entry as GregorianCalendar.Date
getDate(String key, Date defaultValue)
Get an entry as Date.List<Date>
getDateList(String key, List<Date> defaultValue)
Get a comma separated list of dates.Double
getDouble(String key, Double defaultValue)
Get an entry as Double.List<Double>
getDoubleList(String key, List<Double> defaultValue)
Get a comma separated list of double numbers.Float
getFloat(String key, Float defaultValue)
Get an entry as Float.List<Float>
getFloatList(String key, List<Float> defaultValue)
Get a comma separated list of float numbers.Integer
getInteger(String key, Integer defaultValue)
Get an entry as Integer.List<Integer>
getIntegerList(String key, List<Integer> defaultValue)
Get a comma separated list of integer numbers.Long
getLong(String key, Long defaultValue)
Get an entry as Long.List<Long>
getLongList(String key, List<Long> defaultValue)
Get a comma separated list of long integer numbers.String
getString(String key, String defaultValue)
Get an entry as String.List<String>
getStringList(String key, List<String> defaultValue)
Get a comma separated list of strings.XMLGregorianCalendar
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()
Set<String>
keySet()
void
put(String key, Object o)
Add or replace an entry.String
put(String key, String v)
void
putAll(Map<? extends String,? extends String> map)
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.String
remove(Object key)
int
size()
Collection<String>
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 Detail
-
DATE_TIME_PATTERN
public static final String DATE_TIME_PATTERN
Pattern used to represent a date with time.- See Also:
- Constant Field Values
-
DATE_PATTERN
public static final String DATE_PATTERN
Pattern used to represent a date without time.- See Also:
- Constant Field Values
-
-
Method Detail
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<String,String>
-
containsValue
public boolean containsValue(Object o)
- Specified by:
containsValue
in interfaceMap<String,String>
-
put
public void put(String key, Object o)
Add or replace an entry.- Parameters:
key
- Key to identify the entry.o
- Value of the entry.
-
putXmlObject
public void putXmlObject(String key, Object o) throws javax.xml.bind.JAXBException
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
public void putIfNotEmpty(String key, Object o)
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
public void putAllObjects(Map<? extends String,? extends Object> map)
Add or replace many elements.- Parameters:
map
- Provides the source keys and objects.
-
getString
public String getString(String key, String defaultValue)
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
public Integer getInteger(String key, Integer defaultValue) throws NumberFormatException
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
public Long getLong(String key, Long defaultValue) throws NumberFormatException
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
public Boolean getBoolean(String key, Boolean defaultValue)
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
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) throws NumberFormatException
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
public BigInteger getBigInteger(String key, BigInteger defaultValue) throws NumberFormatException
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
public Float getFloat(String key, Float defaultValue) throws NumberFormatException
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
public Double getDouble(String key, Double defaultValue) throws NumberFormatException
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
public Date getDate(String key, Date defaultValue) throws ParseException
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
public Calendar getCalendar(String key, Calendar defaultValue) throws ParseException
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
public List<String> getStringList(String key, List<String> defaultValue)
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
public List<Long> getLongList(String key, List<Long> defaultValue) throws NumberFormatException
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
public List<Float> getFloatList(String key, List<Float> defaultValue) throws NumberFormatException
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
public List<Date> getDateList(String key, List<Date> defaultValue) throws ParseException
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.
-
-