java.lang.Object
io.github.grumpystuff.grumpyrest.request.stringparser.standard.OptionalFieldParser
All Implemented Interfaces:
FromStringParser

public final class OptionalFieldParser extends Object implements FromStringParser
Parses an optional field, returning an OptionalField which wraps the parsed value (or nothing if the field to parse is absent). The type argument fo the OptionalField is used to select the actual parser if the field is present.
  • Constructor Details

    • OptionalFieldParser

      public OptionalFieldParser(FromStringParserRegistry registry)
      Constructor.
      Parameters:
      registry - needed to fetch the parser for the contained type at run-time
  • Method Details

    • supportsType

      public boolean supportsType(Type type)
      Description copied from interface: FromStringParser
      Checks if this parser supports the specified type.
      Specified by:
      supportsType in interface FromStringParser
      Parameters:
      type - the type to check
      Returns:
      true if supported, false if not
    • parseFromString

      public Object parseFromString(String s, Type type) throws FromStringParserException
      Description copied from interface: FromStringParser
      Parses a value from a string.
      Specified by:
      parseFromString in interface FromStringParser
      Parameters:
      s - the string to parse
      type - the type to parse as
      Returns:
      the parsed value
      Throws:
      FromStringParserException - if the string does not conform to the parser's expectation
    • parseFromAbsentString

      public Object parseFromAbsentString(Type type)
      Description copied from interface: FromStringParser
      "Parses" a value from an absent string. This can be used to return a default for optional parameters.

      The standard implementation of this method is that missing values are not tolerated, and throws an exception.

      Specified by:
      parseFromAbsentString in interface FromStringParser
      Parameters:
      type - the type to parse as
      Returns:
      the default value