Class GenericConverters

java.lang.Object
com.ultikits.ultitools.config.convert.builtin.GenericConverters
All Implemented Interfaces:
ConfigConverter<Object>

@Internal public final class GenericConverters extends Object implements ConfigConverter<Object>
Generic factory categories, their construction policy, and per-element diagnostic boundaries.
  • Field Details

  • Constructor Details

    • GenericConverters

      private GenericConverters()
  • Method Details

    • registeredTypes

      public static Set<Class<?>> registeredTypes()
      Returns:
      the implementation-owned supported builtin factory categories
    • resolve

      public static ConfigConverter<?> resolve(Type type)
      Parameters:
      type - the declared type
      Returns:
      a generic converter only when its result can be constructed
    • resolveWrite

      public static ConfigConverter<?> resolveWrite(Type type)
      Parameters:
      type - runtime/declaration type
      Returns:
      constructor-free write converter
    • toPlain

      public Object toPlain(Object value, ConversionContext context) throws ConversionException
      Description copied from interface: ConfigConverter
      Converts a Java value to plain data.
      Specified by:
      toPlain in interface ConfigConverter<Object>
      Parameters:
      value - the Java value
      context - the immutable conversion location and recursive dispatcher
      Returns:
      plain data
      Throws:
      ConversionException - when conversion cannot preserve the value
    • writeMap

      private static Map<String,Object> writeMap(Map<?,?> value, ConverterRegistry.Context ctx) throws ConversionException
      Writes each entry under the file's own key text for its key when the file value at this location holds one (the first in file order), else under the key's verified text, and descends into the value under that text so nested maps bind too.
      Throws:
      ConversionException
    • fromPlain

      public Object fromPlain(Object plain, ConversionContext context) throws ConversionException
      Description copied from interface: ConfigConverter
      Converts plain data to a Java value.
      Specified by:
      fromPlain in interface ConfigConverter<Object>
      Parameters:
      plain - the value read from the document
      context - the immutable conversion location and recursive dispatcher
      Returns:
      the Java value
      Throws:
      ConversionException - when the input cannot be converted
    • bindsKeyText

      @Internal public static boolean bindsKeyText(Type keyType)
      Whether a map with this declared key type binds its keys to the file's key text: every key type other than String and Object, whose keys are already the text the file holds.
      Parameters:
      keyType - declared map key type
      Returns:
      whether the key is typed
    • fileKeyIndex

      @Internal public static Map<Object,List<String>> fileKeyIndex(ConverterRegistry.Context ctx, Type keyType)
      Indexes the keys of the map the file holds at ctx's location by the typed key each reads back to, through keyType and the same registry read path the loader uses. Each typed key maps to its file key texts in file order; a file key that does not read as the key type is skipped. Reading a key is a probe, so nothing is recorded in the conversion's failures. The one index behind both the map writer's binding (first spelling wins) and the file-key ambiguity scan (two or more spellings).
      Parameters:
      ctx - context whose file value is the map to index
      keyType - declared map key type
      Returns:
      typed key to its file spellings in file order; empty when the key type is not typed or the file value at this location is not a map
    • keyText

      private static String keyText(Object plain, Object javaKey, Type keyType, ConverterRegistry.Context keyContext) throws ConversionException
      The text a converted map key is written under. A key that converts to text keeps that text. A key that converts to another plain scalar (Integer, Long, Boolean, ...) is written as that scalar's text only when the text reads back, through the declared key type and the same registry path the loader uses, to a key equal to the entry's own; anything else is refused, so a key is never written under a text that loads as a different key. The read-back is a probe and records nothing in the conversion's failures.
      Throws:
      ConversionException
    • readElements

      private static void readElements(List<?> plain, Type element, ConverterRegistry.Context ctx, Collection<Object> values)
    • readEnum

      private static Object readEnum(Object plain, Class<?> raw, ConverterRegistry.Context ctx) throws ConversionException
      Throws:
      ConversionException
    • newCollection

      private static Collection<Object> newCollection(Class<?> raw, Type element, ConverterRegistry.Context ctx) throws ConversionException
      Throws:
      ConversionException
    • newMap

      private static Map<Object,Object> newMap(Class<?> raw, Type key, ConverterRegistry.Context ctx) throws ConversionException
      Throws:
      ConversionException
    • construct

      private static Object construct(Class<?> raw, ConverterRegistry.Context ctx) throws ConversionException
      Throws:
      ConversionException