Class ConvertingHashMap

java.lang.Object
de.stefanfrings.utils.ConvertingHashMap
All Implemented Interfaces:
Map<String,String>

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 Details

    • DATE_TIME_PATTERN

      public static final String DATE_TIME_PATTERN
      Pattern used to represent a date with time.
      See Also:
    • DATE_PATTERN

      public static final String DATE_PATTERN
      Pattern used to represent a date without time.
      See Also:
  • Constructor Details

    • ConvertingHashMap

      public ConvertingHashMap()
  • Method Details

    • size

      public int size()
      Specified by:
      size in interface Map<String,String>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,String>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,String>
    • containsValue

      public boolean containsValue(Object o)
      Specified by:
      containsValue in interface Map<String,String>
    • get

      public String get(Object key)
      Specified by:
      get in interface Map<String,String>
    • put

      public String put(String key, String v)
      Specified by:
      put in interface Map<String,String>
    • remove

      public String remove(Object key)
      Specified by:
      remove in interface Map<String,String>
    • putAll

      public void putAll(Map<? extends String,? extends String> map)
      Specified by:
      putAll in interface Map<String,String>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<String,String>
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<String,String>
    • values

      public Collection<String> values()
      Specified by:
      values in interface Map<String,String>
    • entrySet

      public Set<Map.Entry<String,String>> entrySet()
      Specified by:
      entrySet in interface Map<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.