Class ObjectConverterManager


  • public class ObjectConverterManager
    extends java.lang.Object
    ObjectConverterManager is a center place to register ObjectConverters with a data type and an optional ConverterContext. The ConverterContext is a switch when you need several different converters for the same data type. If you only use one ObjectConverter for a particular data type, you can use null as the ConverterContext.

    ObjectConverterManager is used in many places in JIDE products, especially in JIDE Grids where ContextSensitiveTableModel adds getCellClassAt (the data type) and getConverterContextAt for each cell in a table model. We use both values as the key to look up for the ObjectConverter from ObjectConverterManager.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addRegistrationListener​(RegistrationListener l)
      Adds a listener to the list that's notified each time a change to the manager occurs.
      static void clear()  
      static java.lang.Object fromString​(java.lang.String string, java.lang.Class<?> clazz)
      Converts from a string to an object with type class.
      static java.lang.Object fromString​(java.lang.String string, java.lang.Class<?> clazz, ConverterContext context)
      Converts from a string to an object with type class using the converter context.
      static ObjectConverter getConverter​(java.lang.Class<?> clazz)
      Gets the converter associated with the type.
      static ObjectConverter getConverter​(java.lang.Class<?> clazz, ConverterContext context)
      Gets the registered converter associated with class and context.
      static ConverterContext[] getConverterContexts​(java.lang.Class<?> clazz)
      Gets the available ConverterContexts registered with the class.
      static RegistrationListener[] getRegistrationListeners()
      Returns an array of all the registration listeners registered on this manager.
      static void initDefaultConverter()
      Initialize default converters.
      static boolean isAutoInit()
      Checks the value of autoInit.
      static void registerConverter​(java.lang.Class<?> clazz, ObjectConverter converter)
      Registers as the default converter with type specified as clazz.
      static void registerConverter​(java.lang.Class<?> clazz, ObjectConverter converter, ConverterContext context)
      Registers a converter with the type specified as class and a converter context specified as context.
      static void removeRegistrationListener​(RegistrationListener l)
      Removes a listener from the list that's notified each time a change to the manager occurs.
      static void resetInit()
      If initDefaultConverter() is called once, calling it again will have no effect because an internal flag is set.
      static void setAutoInit​(boolean autoInit)
      Sets autoInit to true or false.
      static java.lang.String toString​(java.lang.Object object)
      Converts an object to string using default converter context.
      static java.lang.String toString​(java.lang.Object object, java.lang.Class<?> clazz)
      Converts an object to string using default converter context.
      static java.lang.String toString​(java.lang.Object object, java.lang.Class<?> clazz, ConverterContext context)
      Converts an object to string using converter context specified.
      static void unregisterAllConverters()
      Unregisters all the converters which registered before.
      static void unregisterAllConverters​(java.lang.Class<?> clazz)
      Unregisters all converters associated with the class.
      static void unregisterConverter​(java.lang.Class<?> clazz)
      Unregisters converter associated with the class.
      static void unregisterConverter​(java.lang.Class<?> clazz, ConverterContext context)
      Unregisters converter associated with clazz and context.
      • Methods inherited from class java.lang.Object

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

      • ObjectConverterManager

        public ObjectConverterManager()
    • Method Detail

      • registerConverter

        public static void registerConverter​(java.lang.Class<?> clazz,
                                             ObjectConverter converter,
                                             ConverterContext context)
        Registers a converter with the type specified as class and a converter context specified as context.
        Parameters:
        clazz - the type of which the converter will registered. You can use superclass if several subclasses share the same superclass and the same converter can be used. If you register converters for both subclass and superclass, the one registered with the subclass will be in effect.
        converter - the converter to be registered
        context - the converter context.
      • registerConverter

        public static void registerConverter​(java.lang.Class<?> clazz,
                                             ObjectConverter converter)
        Registers as the default converter with type specified as clazz.
        Parameters:
        clazz - the type of which the converter will registered. You can use superclass if several subclasses share the same superclass and the same converter can be used. If you register converters for both subclass and superclass, the one registered with the subclass will be in effect.
        converter - the converter to be registered
      • unregisterConverter

        public static void unregisterConverter​(java.lang.Class<?> clazz,
                                               ConverterContext context)
        Unregisters converter associated with clazz and context.
        Parameters:
        clazz - the type of which the converter will be unregistered.
        context - the converter context.
      • unregisterConverter

        public static void unregisterConverter​(java.lang.Class<?> clazz)
        Unregisters converter associated with the class.
        Parameters:
        clazz - the type of which the converter will be unregistered.
      • unregisterAllConverters

        public static void unregisterAllConverters​(java.lang.Class<?> clazz)
        Unregisters all converters associated with the class.
        Parameters:
        clazz - the type of which the converter will be unregistered.
      • unregisterAllConverters

        public static void unregisterAllConverters()
        Unregisters all the converters which registered before.
      • getConverter

        public static ObjectConverter getConverter​(java.lang.Class<?> clazz,
                                                   ConverterContext context)
        Gets the registered converter associated with class and context.
        Parameters:
        clazz - the type of which the converter will be registered.
        context - the converter context.
        Returns:
        the registered converter.
      • getConverter

        public static ObjectConverter getConverter​(java.lang.Class<?> clazz)
        Gets the converter associated with the type.
        Parameters:
        clazz - type
        Returns:
        the converter
      • toString

        public static java.lang.String toString​(java.lang.Object object)
        Converts an object to string using default converter context.
        Parameters:
        object - object to be converted.
        Returns:
        the string
      • toString

        public static java.lang.String toString​(java.lang.Object object,
                                                java.lang.Class<?> clazz)
        Converts an object to string using default converter context.
        Parameters:
        object - object to be converted.
        clazz - type of the object
        Returns:
        the string
      • toString

        public static java.lang.String toString​(java.lang.Object object,
                                                java.lang.Class<?> clazz,
                                                ConverterContext context)
        Converts an object to string using converter context specified.
        Parameters:
        object - object to be converted.
        clazz - type of the object
        context - converter context
        Returns:
        the string converted from object
      • fromString

        public static java.lang.Object fromString​(java.lang.String string,
                                                  java.lang.Class<?> clazz)
        Converts from a string to an object with type class.
        Parameters:
        string - the string to be converted
        clazz - the type to be converted to
        Returns:
        the object of type class which is converted from string
      • fromString

        public static java.lang.Object fromString​(java.lang.String string,
                                                  java.lang.Class<?> clazz,
                                                  ConverterContext context)
        Converts from a string to an object with type class using the converter context.
        Parameters:
        string - the string to be converted
        clazz - the type to be converted to
        context - converter context to be used
        Returns:
        the object of type class which is converted from string
      • isAutoInit

        public static boolean isAutoInit()
        Checks the value of autoInit.
        Returns:
        true or false.
        See Also:
        setAutoInit(boolean)
      • setAutoInit

        public static void setAutoInit​(boolean autoInit)
        Sets autoInit to true or false. If autoInit is true, whenever someone tries to call methods like as toString or fromString, initDefaultConverter() will be called if it has never be called. By default, autoInit is true.

        This might affect the behavior if users provide their own converters and want to overwrite default converters. In this case, instead of depending on autoInit to initialize default converters, you should call initDefaultConverter() first, then call registerConverter to add your own converters.

        Parameters:
        autoInit - true or false.
      • addRegistrationListener

        public static void addRegistrationListener​(RegistrationListener l)
        Adds a listener to the list that's notified each time a change to the manager occurs.
        Parameters:
        l - the RegistrationListener
      • removeRegistrationListener

        public static void removeRegistrationListener​(RegistrationListener l)
        Removes a listener from the list that's notified each time a change to the manager occurs.
        Parameters:
        l - the RegistrationListener
      • getConverterContexts

        public static ConverterContext[] getConverterContexts​(java.lang.Class<?> clazz)
        Gets the available ConverterContexts registered with the class.
        Parameters:
        clazz - the class.
        Returns:
        the available ConverterContexts.
      • initDefaultConverter

        public static void initDefaultConverter()
        Initialize default converters. Please make sure you call this method before you use any converter related classes. By default we register following converters.
        • registerConverter(String.class, new DefaultObjectConverter());
        • registerConverter(Integer.class, new IntegerConverter());
        • registerConverter(int.class, new IntegerConverter());
        • registerConverter(Integer.class, new NaturalNumberConverter(), NaturalNumberConverter.CONTEXT);
        • registerConverter(int.class, new NaturalNumberConverter(), NaturalNumberConverter.CONTEXT);
        • registerConverter(Long.class, new LongConverter());
        • registerConverter(long.class, new LongConverter());
        • registerConverter(Double.class, new DoubleConverter());
        • registerConverter(double.class, new DoubleConverter());
        • registerConverter(Float.class, new FloatConverter());
        • registerConverter(float.class, new FloatConverter());
        • registerConverter(Short.class, new ShortConverter());
        • registerConverter(short.class, new ShortConverter());
        • registerConverter(Rectangle.class, new RectangleConverter());
        • registerConverter(Point.class, new PointConverter());
        • registerConverter(Insets.class, new InsetsConverter());
        • registerConverter(Dimension.class, new DimensionConverter());
        • registerConverter(Boolean.class, new BooleanConverter());
        • registerConverter(boolean.class, new BooleanConverter());
        • registerConverter(File.class, new FileConverter());
        • registerConverter(String.class, new FontNameConverter(), FontNameConverter.CONTEXT);
        • registerConverter(Date.class, new DateConverter());
        • registerConverter(Calendar.class, new CalendarConverter());
        • registerConverter(Calendar.class, new MonthConverter(), MonthConverter.CONTEXT_MONTH);
        • registerConverter(Color.class, new RgbColorConverter());
        • registerConverter(Color.class, new HexColorConverter(), ColorConverter.CONTEXT_HEX);
        • registerConverter(String[].class, new StringArrayConverter());
      • clear

        public static void clear()