Class ConfigMappers


  • public final class ConfigMappers
    extends Object
    Utility methods for converting configuration to Java types.

    Note that this class defines many methods of the form <type> to<type>(String) which are automatically registered with each Config.Builder.

    See Also:
    ConfigMapperProvider
    • Method Detail

      • toByte

        public static Byte toByte​(String stringValue)
        Maps stringValue to byte.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to byte
      • toShort

        public static Short toShort​(String stringValue)
        Maps stringValue to short.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to short
      • toInt

        public static Integer toInt​(String stringValue)
        Maps stringValue to int.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to int
      • toLong

        public static Long toLong​(String stringValue)
        Maps stringValue to long.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to long
      • toFloat

        public static Float toFloat​(String stringValue)
        Maps stringValue to float.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to float
      • toDouble

        public static Double toDouble​(String stringValue)
        Maps stringValue to double.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to double
      • toBoolean

        public static Boolean toBoolean​(String stringValue)
        Maps stringValue to boolean.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to boolean
      • toChar

        public static Character toChar​(String stringValue)
        Maps stringValue to char.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to char
      • toClass

        public static Class<?> toClass​(String stringValue)
        Maps stringValue to Class<?>.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to Class<?>
      • toUUID

        public static UUID toUUID​(String stringValue)
        Maps stringValue to UUID.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to UUID
      • toBigDecimal

        public static BigDecimal toBigDecimal​(String stringValue)
        Maps stringValue to BigDecimal.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to BigDecimal
      • toBigInteger

        public static BigInteger toBigInteger​(String stringValue)
        Maps stringValue to BigInteger.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to BigInteger
      • toFile

        public static File toFile​(String stringValue)
        Maps stringValue to File.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to File
      • toPath

        public static Path toPath​(String stringValue)
        Maps stringValue to Path.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to Path
      • toCharset

        public static Charset toCharset​(String stringValue)
        Maps stringValue to Charset.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to Charset
      • toPattern

        public static Pattern toPattern​(String stringValue)
        Maps stringValue to Pattern.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to Pattern
      • toUri

        public static URI toUri​(String stringValue)
        Maps stringValue to URI.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to URI
      • toUrl

        public static URL toUrl​(String stringValue)
        Maps stringValue to URL.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to URL
      • toLocalDate

        public static LocalDate toLocalDate​(String stringValue)
        Maps stringValue to LocalDate.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to LocalDate
        See Also:
        LocalDate.parse(CharSequence)
      • toLocalTime

        public static LocalTime toLocalTime​(String stringValue)
        Maps stringValue to LocalTime.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to LocalTime
        See Also:
        LocalTime.parse(CharSequence)
      • toLocalDateTime

        public static LocalDateTime toLocalDateTime​(String stringValue)
        Maps stringValue to LocalDateTime.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to LocalDateTime
        See Also:
        LocalDateTime.parse(CharSequence)
      • toZonedDateTime

        public static ZonedDateTime toZonedDateTime​(String stringValue)
        Maps stringValue to ZonedDateTime.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to ZonedDateTime
        See Also:
        ZonedDateTime.parse(CharSequence)
      • toZoneId

        public static ZoneId toZoneId​(String stringValue)
        Maps stringValue to ZoneId.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to ZoneId
        See Also:
        ZoneId.of(String)
      • toZoneOffset

        public static ZoneOffset toZoneOffset​(String stringValue)
        Maps stringValue to ZoneOffset.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to ZoneOffset
        See Also:
        ZoneOffset.of(String)
      • toTimeZone

        @Deprecated
        public static TimeZone toTimeZone​(String stringValue)
        Deprecated.
        use new time API, such as ZoneId
        Maps stringValue to TimeZone.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to TimeZone
        See Also:
        ZoneId.of(String)
      • toSimpleTimeZone

        @Deprecated
        public static SimpleTimeZone toSimpleTimeZone​(String stringValue)
        Deprecated.
        use new time API, such as ZoneId
        Maps stringValue to SimpleTimeZone.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to SimpleTimeZone
        See Also:
        ZoneId.of(String)
      • toInstant

        public static Instant toInstant​(String stringValue)
        Maps stringValue to Instant.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to Instant
        See Also:
        Instant.parse(CharSequence)
      • toOffsetTime

        public static OffsetTime toOffsetTime​(String stringValue)
        Maps stringValue to OffsetTime.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to OffsetTime
        See Also:
        OffsetTime.parse(CharSequence)
      • toDuration

        public static Duration toDuration​(String stringValue)
        Maps stringValue to Duration.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to Duration
        See Also:
        Duration.parse(CharSequence)
      • toPeriod

        public static Period toPeriod​(String stringValue)
        Maps stringValue to Period.
        Parameters:
        stringValue - source value as a String
        Returns:
        mapped stringValue to Period
        See Also:
        Period.parse(CharSequence)
      • toMap

        public static Map<String,​String> toMap​(Config config)
        Transform all leaf nodes (values) into Map instance.

        Fully qualified key of config node is used as a key in returned Map. Detach config node before transforming to Map in case you want to cut current Config node key prefix.

        Let's say we work with following configuration:

         app:
              name: Example 1
              page-size: 20
         logging:
              app.level = INFO
              level = WARNING
         
        Map app1 contains two keys: app.name, app.page-size.
        
         Map<String, String> app1 = ConfigMappers.toMap(config.get("app"));
         
        Detaching app config node returns new Config instance with "reset" local root.
        
         Map<String, String> app2 = ConfigMappers.toMap(config.get("app").detach());
         
        Map app2 contains two keys without app prefix: name, page-size.
        Parameters:
        config - config node used to transform into Properties
        Returns:
        new Map instance that contains all config leaf node values
        See Also:
        Config.detach()
      • toProperties

        public static Properties toProperties​(Config config)
        Transform all leaf nodes (values) into Properties instance.

        Fully qualified key of config node is used as a key in returned Properties. Detach config node before transforming to Properties in case you want to cut current Config node key prefix.

        Let's say we work with following configuration:

         app:
              name: Example 1
              page-size: 20
         logging:
              app.level = INFO
              level = WARNING
         
        Properties app1 contains two keys: app.name, app.page-size.
         Properties app1 = ConfigMappers.toProperties(config.get("app"));
         
        Detaching app config node returns new Config instance with "reset" local root.
         Properties app2 = ConfigMappers.toProperties(config.get("app").detach());
         
        Properties app2 contains two keys without app prefix: name, page-size.
        Parameters:
        config - config node used to transform into Properties
        Returns:
        Properties instance that contains all config leaf node values.
        See Also:
        Config.detach()