Class StringConverter

  • All Implemented Interfaces:
    ConverterMatcher, SingleValueConverter

    public class StringConverter
    extends AbstractSingleValueConverter
    Converts a String to a String ;).

    Well ok, it doesn't actually do any conversion. The converter uses by default a map with weak references to reuse instances of strings that do not exceed a length limit. This limit is by default 38 characters to cache typical strings containing UUIDs. Only shorter strings are typically repeated more often in XML values.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map cache
      A Map to store strings as long as needed to map similar strings onto the same instance and conserve memory.
      private static int LENGTH_LIMIT  
      private int lengthLimit  
    • Constructor Summary

      Constructors 
      Constructor Description
      StringConverter()
      Construct a StringConverter using a cache with weak references for strings not exceeding 38 characters.
      StringConverter​(int lengthLimit)
      Construct a StringConverter using a cache with weak references for strings not exceeding the length limit.
      StringConverter​(java.util.Map map)
      Construct a StringConverter using a map-based cache for strings not exceeding 38 characters.
      StringConverter​(java.util.Map map, int lengthLimit)
      Construct a StringConverter using a map-based cache for strings not exceeding the length limit.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canConvert​(java.lang.Class type)
      Determines whether the converter can marshall a particular type.
      java.lang.Object fromString​(java.lang.String str)
      Unmarshals an Object from its single value representation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • cache

        private final java.util.Map cache
        A Map to store strings as long as needed to map similar strings onto the same instance and conserve memory. The map can be set from the outside during construction, so it can be a LRU map or a weak map, synchronised or not.
      • lengthLimit

        private final int lengthLimit
    • Constructor Detail

      • StringConverter

        public StringConverter​(java.util.Map map,
                               int lengthLimit)
        Construct a StringConverter using a map-based cache for strings not exceeding the length limit.
        Parameters:
        map - the map to use for the instances to reuse (may be null to not cache at all)
        lengthLimit - maximum string length of a cached string, -1 to cache all, 0 to turn off the cache
        Since:
        1.4.2
      • StringConverter

        public StringConverter​(java.util.Map map)
        Construct a StringConverter using a map-based cache for strings not exceeding 38 characters.
        Parameters:
        map - the map to use for the instances to reuse (may be null to not cache at all)
      • StringConverter

        public StringConverter​(int lengthLimit)
        Construct a StringConverter using a cache with weak references for strings not exceeding the length limit.
        Parameters:
        lengthLimit - maximum string length of a cached string, -1 to cache all, 0 to turn off the cache
        Since:
        1.4.2
      • StringConverter

        public StringConverter()
        Construct a StringConverter using a cache with weak references for strings not exceeding 38 characters.