Interface Converter<S,​T>

    • Method Detail

      • accept

        default boolean accept​(java.lang.Class<?> sourceType,
                               java.lang.Class<?> targetType)
        Accept the source type and target type or not
        Parameters:
        sourceType - the source type
        targetType - the target type
        Returns:
        if accepted, return true, or false
      • convert

        @Nullable
        T convert​(@Nullable
                  S source)
        Convert the source-typed value to the target-typed value
        Parameters:
        source - the source-typed value
        Returns:
        the target-typed value
      • getSourceType

        default java.lang.Class<S> getSourceType()
        Get the source type
        Returns:
        non-null
      • getTargetType

        default java.lang.Class<T> getTargetType()
        Get the target type
        Returns:
        non-null
      • getConverter

        static <S,​T> Converter<S,​T> getConverter​(java.lang.Class<S> sourceType,
                                                             java.lang.Class<T> targetType)
        Retrieves a converter instance that can convert from the specified source type to the target type.

        This method uses the service loader mechanism to find all available converters and sorts them based on their priority. The first converter that accepts the given source and target types will be returned.

        Example Usage

        
         Class<String> sourceType = String.class;
         Class<Integer> targetType = Integer.class;
         Converter<String, Integer> converter = getConverter(sourceType, targetType);
         if (converter != null) {
             Integer result = converter.convert("123");
         }
         
        Type Parameters:
        S - the source type
        T - the target type
        Parameters:
        sourceType - the class of the source type
        targetType - the class of the target type
        Returns:
        a converter instance that can handle the specified types, or null if no suitable converter is found
      • convertIfPossible

        static <T> T convertIfPossible​(java.lang.Object source,
                                       java.lang.Class<T> targetType)
        Converts the given source object to the specified targetType if a suitable converter is available.

        This method attempts to find a converter that can convert from the type of the source object to the target type using the service loader mechanism. If such a converter exists, it will be used to perform the conversion. If no suitable converter is found, this method returns null.

        Example Usage

        
         String source = "123";
         Integer result = convertIfPossible(source, Integer.class);
         
        Type Parameters:
        T - the type of the target
        Parameters:
        source - the source object to be converted
        targetType - the target type to convert to
        Returns:
        the converted object of type T, or null if no suitable converter is found