Class TypeFactory


  • public class TypeFactory
    extends Object
    A class that contains built-in parsers from string into common object types, typically for Reflection purposes.

    It supports all of the primitive types (including their wrappers) as well as enumerations and following object types:

    From the java.time package:

    • java.time.Duration such as "PT15M" (15 minutes)
    • java.time.Instant such as "2007-12-03T13:15:30Z"
    • java.time.LocalDate such as "2007-12-03"
    • java.time.LocalTime such as "10:15" or "10:15:30"
    • java.time.LocalDateTime such as "2007-12-03T10:15:30"
    • java.time.OffsetDateTime such as "2007-12-03T10:15:30-03:00"
    • java.time.OffsetTime such as "10:15:30+01:00"
    • java.time.ZonedDateTime such as "2007-12-03T10:15:30-03:00[America/Sao_Paulo]"
    • java.time.ZoneId such as "Europe/Paris" or "+01:00"
    • java.time.ZoneOffset such as "+01", "+0100" or "+01:00"

    From the java.sql package:

    • java.sql.Date such as "2007-12-03"
    • java.sql.Time such as "23:13:33"
    • java.sql.Timestamp such as "2007-12-03 10:15:30.998"

    From the java.util package:

    • java.util.Currency from ISO 4217 codes, such as: "BRL" (Brazilian Real)
    • java.util.Date such as "2007-12-03T10:15:30+01:00" (accepting valid date-time representations in RFC 3339 formats)
    • java.util.TimeZone such as: "America/Sao_Paulo" or "GMT-03:00"
    • java.util.UUID from universally-unique identifier strings in RFC 4122 format

    From the java.io package:

    • java.io.File

    From the java.nio package:

    • java.nio.charset.Charset
    • java.nio.file.Path

    From the java.lang package:

    • java.lang.Class

    From the java.math package:

    • java.math.BigDecimal
    • java.math.BigInteger

    From the java.net package:

    • java.net.InetAddress from strings representing an IP address
    • java.net.URI such as: "example.com/docs"
    • java.net.URL such as: "http://www.example.com/resource1.html"

    Enum elements can also be retrieved based on their constant names (and performs case-insensitive matching). For example:

     ParseFactory.parse(Month.class, "january"); // returns Month.JANUARY
     ParseFactory.parse(DayOfWeek.class, "friday"); // returns DayOfWeek.FRIDAY
     
    Since:
    2.5.0
    Author:
    oswaldo.bapvic.jr (Oswaldo Junior)
    • Method Detail

      • parse

        public static <T> T parse​(Class<T> type,
                                  String string)
                           throws ParseException
        Parses the contents of a string into the specified type.
        Type Parameters:
        T - the target type
        Parameters:
        type - the target type
        string - the string to be parsed
        Returns:
        an object containing the result of the parsing of the specified string into the specified type
        Throws:
        UnsupportedOperationException - if the specified type is not supported
        ParseException - if an error is encountered while parsing