Class Converters


  • public final class Converters
    extends Object
    General converter utilities and constants.
    Author:
    Jeff Mesnil (c) 2017 Red Hat inc.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Type getConverterType​(Class<?> clazz)
      Get the type of the converter specified by clazz.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> getImplicitConverter​(Class<? extends T> type)
      Get the implicit converter for the given type class, if any.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> maximumValueConverter​(Comparator<? super T> comparator, org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, T maximumValue, boolean inclusive)
      Get a wrapping converter which verifies that the configuration value is less than, or optionally equal to, the given maximum value.
      static <T extends Comparable<T>>
      org.eclipse.microprofile.config.spi.Converter<T>
      maximumValueConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, T maximumValue, boolean inclusive)
      Get a wrapping converter which verifies that the configuration value is less than, or optionally equal to, the given maximum value.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> maximumValueStringConverter​(Comparator<? super T> comparator, org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, String maximumValue, boolean inclusive)
      Get a wrapping converter which verifies that the configuration value is less than, or optionally equal to, the given maximum value (in string form).
      static <T extends Comparable<T>>
      org.eclipse.microprofile.config.spi.Converter<T>
      maximumValueStringConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, String maximumValue, boolean inclusive)
      Get a wrapping converter which verifies that the configuration value is less than, or optionally equal to, the given maximum value (in string form).
      static <T> org.eclipse.microprofile.config.spi.Converter<T> minimumValueConverter​(Comparator<? super T> comparator, org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, T minimumValue, boolean inclusive)
      Get a wrapping converter which verifies that the configuration value is greater than, or optionally equal to, the given minimum value.
      static <T extends Comparable<T>>
      org.eclipse.microprofile.config.spi.Converter<T>
      minimumValueConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, T minimumValue, boolean inclusive)
      Get a wrapping converter which verifies that the configuration value is greater than, or optionally equal to, the given minimum value.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> minimumValueStringConverter​(Comparator<? super T> comparator, org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, String minimumValue, boolean inclusive)
      Get a wrapping converter which verifies that the configuration value is greater than, or optionally equal to, the given minimum value (in string form).
      static <T extends Comparable<T>>
      org.eclipse.microprofile.config.spi.Converter<T>
      minimumValueStringConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, String minimumValue, boolean inclusive)
      Get a wrapping converter which verifies that the configuration value is greater than, or optionally equal to, the given minimum value (in string form).
      static <A,​T>
      org.eclipse.microprofile.config.spi.Converter<A>
      newArrayConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> itemConverter, Class<A> arrayType)
      Get a converter that converts a comma-separated string into an array of converted items.
      static <T,​C extends Collection<T>>
      org.eclipse.microprofile.config.spi.Converter<C>
      newCollectionConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> itemConverter, IntFunction<C> collectionFactory)
      Get a converter that converts a comma-separated string into a list of converted items.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> newEmptyValueConverter​(org.eclipse.microprofile.config.spi.Converter<T> delegateConverter)
      Get a converter which wraps another converter and handles empty values correctly.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> newEmptyValueConverter​(org.eclipse.microprofile.config.spi.Converter<T> delegateConverter, T emptyValue)
      Get a converter which wraps another converter and returns a special value to represent empty.
      static <K,​V>
      org.eclipse.microprofile.config.spi.Converter<Map<K,​V>>
      newMapConverter​(org.eclipse.microprofile.config.spi.Converter<? extends K> keyConverter, org.eclipse.microprofile.config.spi.Converter<? extends V> valueConverter)
      Get a converter that converts content of type <key1>=<value1>;<key2>=<value2>... into a Map<K, V> using the given key and value converters.
      static <T> org.eclipse.microprofile.config.spi.Converter<Optional<T>> newOptionalConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegateConverter)
      Get a converter which wraps another converter's result into an Optional.
      static org.eclipse.microprofile.config.spi.Converter<OptionalDouble> newOptionalDoubleConverter​(org.eclipse.microprofile.config.spi.Converter<Double> delegateConverter)
      Get a converter which wraps another converter's result into an OptionalDouble.
      static org.eclipse.microprofile.config.spi.Converter<OptionalInt> newOptionalIntConverter​(org.eclipse.microprofile.config.spi.Converter<Integer> delegateConverter)
      Get a converter which wraps another converter's result into an OptionalInt.
      static org.eclipse.microprofile.config.spi.Converter<OptionalLong> newOptionalLongConverter​(org.eclipse.microprofile.config.spi.Converter<Long> delegateConverter)
      Get a converter which wraps another converter's result into an OptionalLong.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> newTrimmingConverter​(org.eclipse.microprofile.config.spi.Converter<T> delegateConverter)
      Get a converter which trims the string input before passing it on to the delegate converter.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> patternValidatingConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, String pattern)
      Get a wrapping converter which verifies that the configuration value matches the given pattern.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> patternValidatingConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, Pattern pattern)
      Get a wrapping converter which verifies that the configuration value matches the given pattern.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> rangeValueConverter​(Comparator<? super T> comparator, org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, T minimumValue, boolean minInclusive, T maximumValue, boolean maxInclusive)
      Get a wrapping converter which verifies that the configuration value is within the given range.
      static <T extends Comparable<T>>
      org.eclipse.microprofile.config.spi.Converter<T>
      rangeValueConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, T minimumValue, boolean minInclusive, T maximumValue, boolean maxInclusive)
      Get a wrapping converter which verifies that the configuration value is within the given range.
      static <T> org.eclipse.microprofile.config.spi.Converter<T> rangeValueStringConverter​(Comparator<? super T> comparator, org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, String minimumValue, boolean minInclusive, String maximumValue, boolean maxInclusive)
      Get a wrapping converter which verifies that the configuration value is within the given range (in string form).
      static <T extends Comparable<T>>
      org.eclipse.microprofile.config.spi.Converter<T>
      rangeValueStringConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate, String minimumValue, boolean minInclusive, String maximumValue, boolean maxInclusive)
      Get a wrapping converter which verifies that the configuration value is within the given range (in string form).
    • Method Detail

      • getConverterType

        public static Type getConverterType​(Class<?> clazz)
        Get the type of the converter specified by clazz. If the given class is not a valid converter, then null is returned.
        Parameters:
        clazz - the converter class (must not be null)
        Returns:
        the converter target type, or null if the class does not represent a valid configuration
        Throws:
        IllegalStateException - if the given converter class is not properly parameterized
      • getImplicitConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> getImplicitConverter​(Class<? extends T> type)
        Get the implicit converter for the given type class, if any.
        Type Parameters:
        T - the type
        Parameters:
        type - the type class
        Returns:
        the implicit converter for the given type class, or null if none exists
      • newCollectionConverter

        public static <T,​C extends Collection<T>> org.eclipse.microprofile.config.spi.Converter<C> newCollectionConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> itemConverter,
                                                                                                                                IntFunction<C> collectionFactory)
        Get a converter that converts a comma-separated string into a list of converted items.
        Type Parameters:
        T - the item type
        C - the collection type
        Parameters:
        itemConverter - the item converter (must not be null)
        collectionFactory - the collection factory (must not be null)
        Returns:
        the new converter (not null)
      • newArrayConverter

        public static <A,​T> org.eclipse.microprofile.config.spi.Converter<A> newArrayConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> itemConverter,
                                                                                                     Class<A> arrayType)
        Get a converter that converts a comma-separated string into an array of converted items.
        Type Parameters:
        T - the item type
        A - the array type
        Parameters:
        itemConverter - the item converter (must not be null)
        arrayType - the array type class (must not be null)
        Returns:
        the new converter (not null)
      • newMapConverter

        public static <K,​V> org.eclipse.microprofile.config.spi.Converter<Map<K,​V>> newMapConverter​(org.eclipse.microprofile.config.spi.Converter<? extends K> keyConverter,
                                                                                                                org.eclipse.microprofile.config.spi.Converter<? extends V> valueConverter)
        Get a converter that converts content of type <key1>=<value1>;<key2>=<value2>... into a Map<K, V> using the given key and value converters.
        Type Parameters:
        K - the type of the keys
        V - the type of the values
        Parameters:
        keyConverter - the converter used to convert the keys
        valueConverter - the converter used to convert the values
        Returns:
        the new converter (not null)
      • newOptionalConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<Optional<T>> newOptionalConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegateConverter)
        Get a converter which wraps another converter's result into an Optional. If the delegate converter returns null, this converter returns Optional.empty().
        Type Parameters:
        T - the item type
        Parameters:
        delegateConverter - the delegate converter (must not be null)
        Returns:
        the new converter (not null)
      • newOptionalIntConverter

        public static org.eclipse.microprofile.config.spi.Converter<OptionalInt> newOptionalIntConverter​(org.eclipse.microprofile.config.spi.Converter<Integer> delegateConverter)
        Get a converter which wraps another converter's result into an OptionalInt. If the delegate converter returns null, this converter returns Optional.empty().
        Parameters:
        delegateConverter - the delegate converter (must not be null)
        Returns:
        the new converter (not null)
      • newOptionalLongConverter

        public static org.eclipse.microprofile.config.spi.Converter<OptionalLong> newOptionalLongConverter​(org.eclipse.microprofile.config.spi.Converter<Long> delegateConverter)
        Get a converter which wraps another converter's result into an OptionalLong. If the delegate converter returns null, this converter returns Optional.empty().
        Parameters:
        delegateConverter - the delegate converter (must not be null)
        Returns:
        the new converter (not null)
      • newOptionalDoubleConverter

        public static org.eclipse.microprofile.config.spi.Converter<OptionalDouble> newOptionalDoubleConverter​(org.eclipse.microprofile.config.spi.Converter<Double> delegateConverter)
        Get a converter which wraps another converter's result into an OptionalDouble. If the delegate converter returns null, this converter returns Optional.empty().
        Parameters:
        delegateConverter - the delegate converter (must not be null)
        Returns:
        the new converter (not null)
      • newEmptyValueConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> newEmptyValueConverter​(org.eclipse.microprofile.config.spi.Converter<T> delegateConverter,
                                                                                                  T emptyValue)
        Get a converter which wraps another converter and returns a special value to represent empty.
        Type Parameters:
        T - the value type
        Parameters:
        delegateConverter - the converter to delegate to (must not be null)
        emptyValue - the empty value to return
        Returns:
        the converter
      • newEmptyValueConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> newEmptyValueConverter​(org.eclipse.microprofile.config.spi.Converter<T> delegateConverter)
        Get a converter which wraps another converter and handles empty values correctly. This allows the delegate converter to assume that the value being converted will not be null or empty.
        Type Parameters:
        T - the value type
        Parameters:
        delegateConverter - the converter to delegate to (must not be null)
        Returns:
        the converter
      • newTrimmingConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> newTrimmingConverter​(org.eclipse.microprofile.config.spi.Converter<T> delegateConverter)
        Get a converter which trims the string input before passing it on to the delegate converter.
        Type Parameters:
        T - the value type
        Parameters:
        delegateConverter - the converter to delegate to (must not be null)
        Returns:
        the converter
      • minimumValueConverter

        public static <T extends Comparable<T>> org.eclipse.microprofile.config.spi.Converter<T> minimumValueConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                                       T minimumValue,
                                                                                                                       boolean inclusive)
        Get a wrapping converter which verifies that the configuration value is greater than, or optionally equal to, the given minimum value.
        Type Parameters:
        T - the converter target type
        Parameters:
        delegate - the delegate converter (must not be null)
        minimumValue - the minimum value (must not be null)
        inclusive - true if the minimum value is inclusive, false otherwise
        Returns:
        a range-validating converter
      • minimumValueConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> minimumValueConverter​(Comparator<? super T> comparator,
                                                                                                 org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                 T minimumValue,
                                                                                                 boolean inclusive)
        Get a wrapping converter which verifies that the configuration value is greater than, or optionally equal to, the given minimum value.
        Type Parameters:
        T - the converter target type
        Parameters:
        comparator - the comparator to use (must not be null)
        delegate - the delegate converter (must not be null)
        minimumValue - the minimum value (must not be null)
        inclusive - true if the minimum value is inclusive, false otherwise
        Returns:
        a range-validating converter
      • minimumValueStringConverter

        public static <T extends Comparable<T>> org.eclipse.microprofile.config.spi.Converter<T> minimumValueStringConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                                             String minimumValue,
                                                                                                                             boolean inclusive)
        Get a wrapping converter which verifies that the configuration value is greater than, or optionally equal to, the given minimum value (in string form).
        Type Parameters:
        T - the converter target type
        Parameters:
        delegate - the delegate converter (must not be null)
        minimumValue - the minimum value (must not be null)
        inclusive - true if the minimum value is inclusive, false otherwise
        Returns:
        a range-validating converter
        Throws:
        IllegalArgumentException - if the given minimum value fails conversion
      • minimumValueStringConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> minimumValueStringConverter​(Comparator<? super T> comparator,
                                                                                                       org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                       String minimumValue,
                                                                                                       boolean inclusive)
        Get a wrapping converter which verifies that the configuration value is greater than, or optionally equal to, the given minimum value (in string form).
        Type Parameters:
        T - the converter target type
        Parameters:
        comparator - the comparator to use (must not be null)
        delegate - the delegate converter (must not be null)
        minimumValue - the minimum value (must not be null)
        inclusive - true if the minimum value is inclusive, false otherwise
        Returns:
        a range-validating converter
        Throws:
        IllegalArgumentException - if the given minimum value fails conversion
      • maximumValueConverter

        public static <T extends Comparable<T>> org.eclipse.microprofile.config.spi.Converter<T> maximumValueConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                                       T maximumValue,
                                                                                                                       boolean inclusive)
        Get a wrapping converter which verifies that the configuration value is less than, or optionally equal to, the given maximum value.
        Type Parameters:
        T - the converter target type
        Parameters:
        delegate - the delegate converter (must not be null)
        maximumValue - the maximum value (must not be null)
        inclusive - true if the maximum value is inclusive, false otherwise
        Returns:
        a range-validating converter
      • maximumValueConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> maximumValueConverter​(Comparator<? super T> comparator,
                                                                                                 org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                 T maximumValue,
                                                                                                 boolean inclusive)
        Get a wrapping converter which verifies that the configuration value is less than, or optionally equal to, the given maximum value.
        Type Parameters:
        T - the converter target type
        Parameters:
        comparator - the comparator to use (must not be null)
        delegate - the delegate converter (must not be null)
        maximumValue - the maximum value (must not be null)
        inclusive - true if the maximum value is inclusive, false otherwise
        Returns:
        a range-validating converter
      • maximumValueStringConverter

        public static <T extends Comparable<T>> org.eclipse.microprofile.config.spi.Converter<T> maximumValueStringConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                                             String maximumValue,
                                                                                                                             boolean inclusive)
        Get a wrapping converter which verifies that the configuration value is less than, or optionally equal to, the given maximum value (in string form).
        Type Parameters:
        T - the converter target type
        Parameters:
        delegate - the delegate converter (must not be null)
        maximumValue - the maximum value (must not be null)
        inclusive - true if the maximum value is inclusive, false otherwise
        Returns:
        a range-validating converter
        Throws:
        IllegalArgumentException - if the given maximum value fails conversion
      • maximumValueStringConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> maximumValueStringConverter​(Comparator<? super T> comparator,
                                                                                                       org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                       String maximumValue,
                                                                                                       boolean inclusive)
        Get a wrapping converter which verifies that the configuration value is less than, or optionally equal to, the given maximum value (in string form).
        Type Parameters:
        T - the converter target type
        Parameters:
        comparator - the comparator to use (must not be null)
        delegate - the delegate converter (must not be null)
        maximumValue - the maximum value (must not be null)
        inclusive - true if the maximum value is inclusive, false otherwise
        Returns:
        a range-validating converter
        Throws:
        IllegalArgumentException - if the given maximum value fails conversion
      • rangeValueConverter

        public static <T extends Comparable<T>> org.eclipse.microprofile.config.spi.Converter<T> rangeValueConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                                     T minimumValue,
                                                                                                                     boolean minInclusive,
                                                                                                                     T maximumValue,
                                                                                                                     boolean maxInclusive)
        Get a wrapping converter which verifies that the configuration value is within the given range.
        Type Parameters:
        T - the converter target type
        Parameters:
        delegate - the delegate converter (must not be null)
        maximumValue - the maximum value (must not be null)
        maxInclusive - true if the maximum value is inclusive, false otherwise
        Returns:
        a range-validating converter
      • rangeValueConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> rangeValueConverter​(Comparator<? super T> comparator,
                                                                                               org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                               T minimumValue,
                                                                                               boolean minInclusive,
                                                                                               T maximumValue,
                                                                                               boolean maxInclusive)
        Get a wrapping converter which verifies that the configuration value is within the given range.
        Type Parameters:
        T - the converter target type
        Parameters:
        comparator - the comparator to use (must not be null)
        delegate - the delegate converter (must not be null)
        maximumValue - the maximum value (must not be null)
        maxInclusive - true if the maximum value is inclusive, false otherwise
        Returns:
        a range-validating converter
      • rangeValueStringConverter

        public static <T extends Comparable<T>> org.eclipse.microprofile.config.spi.Converter<T> rangeValueStringConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                                           String minimumValue,
                                                                                                                           boolean minInclusive,
                                                                                                                           String maximumValue,
                                                                                                                           boolean maxInclusive)
        Get a wrapping converter which verifies that the configuration value is within the given range (in string form).
        Type Parameters:
        T - the converter target type
        Parameters:
        delegate - the delegate converter (must not be null)
        maximumValue - the maximum value (must not be null)
        maxInclusive - true if the maximum value is inclusive, false otherwise
        Returns:
        a range-validating converter
        Throws:
        IllegalArgumentException - if the given minimum or maximum value fails conversion
      • rangeValueStringConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> rangeValueStringConverter​(Comparator<? super T> comparator,
                                                                                                     org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                     String minimumValue,
                                                                                                     boolean minInclusive,
                                                                                                     String maximumValue,
                                                                                                     boolean maxInclusive)
        Get a wrapping converter which verifies that the configuration value is within the given range (in string form).
        Type Parameters:
        T - the converter target type
        Parameters:
        comparator - the comparator to use (must not be null)
        delegate - the delegate converter (must not be null)
        maximumValue - the maximum value (must not be null)
        maxInclusive - true if the maximum value is inclusive, false otherwise
        Returns:
        a range-validating converter
        Throws:
        IllegalArgumentException - if the given minimum or maximum value fails conversion
      • patternValidatingConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> patternValidatingConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                      Pattern pattern)
        Get a wrapping converter which verifies that the configuration value matches the given pattern.
        Type Parameters:
        T - the converter target type
        Parameters:
        delegate - the delegate converter (must not be null)
        pattern - the pattern to match (must not be null)
        Returns:
        a pattern-validating converter
      • patternValidatingConverter

        public static <T> org.eclipse.microprofile.config.spi.Converter<T> patternValidatingConverter​(org.eclipse.microprofile.config.spi.Converter<? extends T> delegate,
                                                                                                      String pattern)
        Get a wrapping converter which verifies that the configuration value matches the given pattern.
        Type Parameters:
        T - the converter target type
        Parameters:
        delegate - the delegate converter (must not be null)
        pattern - the pattern string to match (must not be null)
        Returns:
        a pattern-validating converter
        Throws:
        PatternSyntaxException - if the given pattern has invalid syntax