Class PropertyDeserializer

java.lang.Object
nl.colorize.util.PropertyDeserializer

public class PropertyDeserializer extends Object
Generalized mechanism for taking text-based properties and parsing them into various types. There are many cases where property values are defined in a text-based format, but need to be parsed into the appropriate data type. Examples are command line arguments, application preferences, configuration, or file formats that do not support different data types (e.g. CSV files). In these situations, this class can be used to standardize how text-based property values are deserialized, rather than applications having to parse every property individually.

By default, this class provides deserialization for the following types:

  • int/Integer
  • long/Long
  • float/Float
  • double/Double
  • boolean/Boolean
  • String
  • Date (date format detection based on DateParser)
  • LocalDate (date format detection based on DateParser)
  • LocalDateTime (date format detection based on DateParser)
  • File
  • UUID
  • Version

Support for additional types can be added by defining custom deserialization behavior.

This class does not use reflection, which means it can also be used in environments where reflection is not allowed or not supported. However, this class can also be embedded in other mechanisms that do rely on reflection, allowing for a less low-level and more convenient usage in situations where reflection is allowed.

  • Constructor Details

    • PropertyDeserializer

      public PropertyDeserializer()
  • Method Details

    • register

      public <T> void register(Class<T> type, Function<String,T> typeMapper)
      Registers the specified function as a type mapper. Once registered, this class will use the type mapper when it encounters properties that match the specified type.
      Throws:
      IllegalArgumentException - if a type mapper has already been registered for the same type.
    • registerPreprocessor

      public void registerPreprocessor(Function<String,String> preprocessor)
      Registers a function that should be used to pre-process property values before trying to deserialize those values. It is possible to register multiple preprocessors, which will then be used in order.
    • parse

      public <T> T parse(String value, Class<T> type)
      Deserializes a text-based property to the specified type.

      If the type mapper throws an exception, this exception is forwarded to the caller of this method. For example, integers are parsed using Integer.parseInt(String). If calling this method results in a NumberFormatException, that exception will be rethrown by this method.

      Throws:
      UnsupportedOperationException - if no type mapper has been registered for the specified type.
      NullPointerException - when trying to deserialize a null value.
    • parse

      public <T> T parse(String value, Class<T> type, T defaultValue)
      Attempts to deserialize a text-based property to the specified type, but returns a default value when parsing results in an exception or when the value is null.
      Throws:
      UnsupportedOperationException - if no type mapper has been registered for the specified type.
    • attempt

      public <T> Optional<T> attempt(String value, Class<T> type)
      Attempts to deserialize a text-based property to the specified type, but returns an empty optional when parsing results in an exception or when the value is null.
      Throws:
      UnsupportedOperationException - if no type mapper has been registered for the specified type.
    • parseString

      public String parseString(String value, String defaultValue)
    • parseInt

      public int parseInt(String value, int defaultValue)
    • parseLong

      public long parseLong(String value, long defaultValue)
    • parseFloat

      public float parseFloat(String value, float defaultValue)
    • parseDouble

      public double parseDouble(String value, double defaultValue)
    • parseBool

      public boolean parseBool(String value, boolean defaultValue)
    • fromProperties

      public static PropertyDeserializer fromProperties(Properties properties)
      Returns a PropertyDeserializer that acts as a live view for parsing values from the specified Properties object.
    • fromMap

      public static PropertyDeserializer fromMap(Map<String,?> properties)
      Returns a PropertyDeserializer that acts as a live view for parsing values from the specified map.
    • fromCSV

      public static PropertyDeserializer fromCSV(CSVRecord record)
      Returns a PropertyDeserializer that acts as a live view for parsing values from the specified CSV record.
      Throws:
      IllegalStateException - if the CSV does not include any column information.