Class GenericParser<U>

  • Type Parameters:
    U - the type of the objects to parse
    Direct Known Subclasses:
    StringParser

    public class GenericParser<U>
    extends java.lang.Object
    Utility class to parse various types from other values.
    Since:
    16184
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<java.lang.Class<?>,​java.util.function.Function<U,​?>> parsers  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        GenericParser()
      Creates an empty GenericParser
      protected GenericParser​(java.util.Map<java.lang.Class<?>,​java.util.function.Function<U,​?>> parsers)
      Creates a new GenericParser with the same parsers as the specified map
        GenericParser​(GenericParser<U> parser)
      Creates a new GenericParser by deeply copying parser
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T parse​(java.lang.Class<T> type, U value)
      Parses the given value as type and returns the result
      <T> GenericParser<U> registerParser​(java.lang.Class<T> type, java.util.function.Function<U,​T> value)  
      boolean supports​(java.lang.Class<?> type)
      Determines whether type can be parsed
      <T> java.util.Optional<T> tryParse​(java.lang.Class<T> type, U value)
      Tries to parse the given value as type and returns the result.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • parsers

        protected final java.util.Map<java.lang.Class<?>,​java.util.function.Function<U,​?>> parsers
    • Constructor Detail

      • GenericParser

        public GenericParser()
        Creates an empty GenericParser
      • GenericParser

        public GenericParser​(GenericParser<U> parser)
        Creates a new GenericParser by deeply copying parser
        Parameters:
        parser - the parser to copy
      • GenericParser

        protected GenericParser​(java.util.Map<java.lang.Class<?>,​java.util.function.Function<U,​?>> parsers)
        Creates a new GenericParser with the same parsers as the specified map
        Parameters:
        parsers - the parsers
    • Method Detail

      • supports

        public boolean supports​(java.lang.Class<?> type)
        Determines whether type can be parsed
        Parameters:
        type - the type
        Returns:
        true if type can be parsed
      • parse

        public <T> T parse​(java.lang.Class<T> type,
                           U value)
        Parses the given value as type and returns the result
        Type Parameters:
        T - the type
        Parameters:
        type - the type class
        value - the value to parse
        Returns:
        the parsed value for string as type type
        Throws:
        java.lang.UnsupportedOperationException - if type is not supported
        UncheckedParseException - when the parsing fails
      • tryParse

        public <T> java.util.Optional<T> tryParse​(java.lang.Class<T> type,
                                                  U value)
        Tries to parse the given value as type and returns the result.
        Type Parameters:
        T - the type
        Parameters:
        type - the type class
        value - the value to parse
        Returns:
        the parsed value for value as type type, or Optional.empty() (if parsing fails, or the type is not supported)