Class SystemRegistry

java.lang.Object
org.apache.sis.internal.converter.ConverterRegistry
org.apache.sis.internal.converter.SystemRegistry

public final class SystemRegistry extends ConverterRegistry
The Apache SIS system-wide ConverterRegistry. This class serves two purposes:
  • Fetch the list of converters from the content of all META-INF/services/org.apache.sis.util.ObjectConverter files found on the classpath. The intent is to allow other modules to register their own converters.
  • Apply heuristic rules in addition to the explicitly registered converters. Those heuristic rules are provided in a separated class in order to keep the ConverterRegistry class a little bit more "pure", and concentrate most arbitrary decisions in this single class.
When using SystemRegistry, new converters may "automagically" appear as a consequence of the above-cited heuristic rules. This differs from the ConverterRegistry behavior, where only registered converters are used.

Thread safety

The same INSTANCE can be safely used by many threads without synchronization on the part of the caller.
Since:
0.3
Version:
0.8
  • Field Details

    • INSTANCE

      public static final ConverterRegistry INSTANCE
      The default system-wide instance. This register is initialized with conversions between some basic Java and SIS objects, like conversions between Date and Long. Those conversions are defined for the lifetime of the JVM.

      If a temporary set of converters is desired, a new instance of ConverterRegistry should be created explicitly instead.

      See the package javadoc for information about how applications can add custom system-wide converters.

  • Constructor Details

    • SystemRegistry

      private SystemRegistry()
      Creates an initially empty set of object converters. The heuristic rules apply right away, even if no converter have been registered yet.
  • Method Details

    • initialize

      protected void initialize()
      Invoked when this ConverterRegistry needs to be initialized. This method is automatically invoked the first time that ConverterRegistry.register(ObjectConverter) or ConverterRegistry.find(Class, Class) is invoked.

      The default implementation is equivalent to the following code (see the package javadoc for more information):

      Overrides:
      initialize in class ConverterRegistry
    • tryInverse

      private static boolean tryInverse(Class<?> targetClass)
      Returns true if we should look for the inverse converter of the given target class. For example if no converter is explicitly registered from Float to String, we can look for the converter from String to Float, then fetch its inverse.

      We allow this operation only for a few types which are needed for the way SIS converters are defined in this internal package.

    • createConverter

      protected <S, T> ObjectConverter<S,T> createConverter(Class<S> sourceClass, Class<T> targetClass)
      Creates dynamically the converters for a few special cases. This method is invoked only the first time that a new pair of source and target classes is requested. Then, the value returned by this method will be cached for future invocations.

      Some (not all) special cases are:

      • If the source class is CharSequence, tries to delegate to another converter accepting String sources.
      • If the source and target types are numbers, generates a NumberConverter on the fly.
      • If the target type is a code list, generate the converter on-the-fly. We do not register every code lists in advance because there is too many of them, and a generic code is available for all of them.
      Overrides:
      createConverter in class ConverterRegistry
      Type Parameters:
      S - the source class.
      T - the target class.
      Parameters:
      sourceClass - the source class.
      targetClass - the target class, or Object.class for any.
      Returns:
      a newly generated converter from the specified source class to the target class, or null if none.
    • isSupportedNumber

      private static boolean isSupportedNumber(Class<?> type)
      Returns true if the given type is one of the types supported by NumberConverter.