View Javadoc
1   package com.opencsv.bean.processor;
2   
3   /**
4    * StringProcessor that converts the empty or blank strings to a literal null string.
5    * This is useful when you prefer null in a particular variable.
6    * <p>
7    * A sample of this can be found in the unit test ProcessorTestBean and is annotated as follows.
8    * <p>
9    * <pre>
10   *     &#64;PreAssignmentProcessor(processor = ConvertEmptyOrBlankStringsToNull.class)
11   *     &#64;CsvBindByName(column = "name")
12   *     private String beanName;
13   *  </pre>
14   *
15   * @author Scott Conway
16   * @since 5.4
17   */
18  public class ConvertEmptyOrBlankStringsToNull implements StringProcessor {
19  
20      /**
21       * Default Constructor.
22       */
23      public ConvertEmptyOrBlankStringsToNull() {
24      }
25  
26      @Override
27      public String processString(String value) {
28          if (value == null || value.trim().isEmpty()) {
29              return null;
30          }
31          return value;
32      }
33  
34      /**
35       * This method is unused in this implementation as we are converting to null.
36       * Any calls to this method are ignored.
37       *
38       * @param value Information used by the processor to process the string
39       */
40      @Override
41      public void setParameterString(String value) {
42          // not needed
43      }
44  }