ConvertEmptyOrBlankStringsToDefault.java

  1. package com.opencsv.bean.processor;

  2. /**
  3.  * StringProcessor that converts the empty or blank strings to a desired value string.
  4.  * This is useful when you want a default value.
  5.  * <p>
  6.  * A sample of this can be found in the unit test ProcessorTestBean and is annotated as follows.
  7.  * <p>
  8.  * <pre>
  9.  *     &#64;PreAssignmentProcessor(processor = ConvertEmptyOrBlankStringsToDefault.class, paramString = "-1")
  10.  *     &#64;CsvBindByName(column = "id")
  11.  *     private int beanId;
  12.  * </pre>
  13.  *
  14.  * @author Scott Conway
  15.  * @since 5.4
  16.  */
  17. public class ConvertEmptyOrBlankStringsToDefault implements StringProcessor {
  18.     String defaultValue;

  19.     /**
  20.      * Default constructor
  21.      */
  22.     public ConvertEmptyOrBlankStringsToDefault() {
  23.     }

  24.     @Override
  25.     public String processString(String value) {
  26.         if (value == null || value.trim().isEmpty()) {
  27.             return defaultValue;
  28.         }
  29.         return value;
  30.     }

  31.     @Override
  32.     public void setParameterString(String value) {
  33.         defaultValue = value;
  34.     }
  35. }