Class NullStringConversion

java.lang.Object
com.univocity.parsers.conversions.NullStringConversion
All Implemented Interfaces:
Conversion<Object,Object>

public class NullStringConversion extends Object implements Conversion<Object,Object>
Converts Strings to null and vice versa

This class supports multiple representations of null values. For example, you can define conversions from different Strings such as "N/A, ?, -" to null.

The reverse conversion from a null to String (in revert(Object) will return the first String provided in this class constructor if the object is null.

Using the previous example, a call to revert(Object) will produce "N/A".

  • Field Details

    • nullStrings

      private final Set<String> nullStrings
    • defaultNullString

      private final String defaultNullString
  • Constructor Details

    • NullStringConversion

      public NullStringConversion(String... nullRepresentations)
      Creates conversions from Strings to null.

      The list of Strings that identify nulls are mandatory.

      Parameters:
      nullRepresentations - Strings that identify a true value. The first element will be returned when executing revert(Object)
  • Method Details

    • execute

      public Object execute(Object input)
      Converts an Object to null. The string representation of the object will be used to match the string elements provided in the constructor.
      Specified by:
      execute in interface Conversion<Object,Object>
      Parameters:
      input - an Object to be converted to null.
      Returns:
      null if the string representation of the object matches any one of the Strings provided in the constructor of this class. Otherwise, the original object will be returned.
    • revert

      public Object revert(Object input)
      Converts a null input to a String representation. The String returned will be the first element provided in the constructor of this class.
      Specified by:
      revert in interface Conversion<Object,Object>
      Parameters:
      input - an Object that, if null, will be transformed to a String.
      Returns:
      If the input is null, the string representation for null objects. Otherwise, the original object will be returned.