1 package com.opencsv.bean.processor;
2
3 /**
4 * StringProcessor that converts the empty or blank strings to a desired value string.
5 * This is useful when you want a default value.
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 * @PreAssignmentProcessor(processor = ConvertEmptyOrBlankStringsToDefault.class, paramString = "-1")
11 * @CsvBindByName(column = "id")
12 * private int beanId;
13 * </pre>
14 *
15 * @author Scott Conway
16 * @since 5.4
17 */
18 public class ConvertEmptyOrBlankStringsToDefault implements StringProcessor {
19 String defaultValue;
20
21 /**
22 * Default constructor
23 */
24 public ConvertEmptyOrBlankStringsToDefault() {
25 }
26
27 @Override
28 public String processString(String value) {
29 if (value == null || value.trim().isEmpty()) {
30 return defaultValue;
31 }
32 return value;
33 }
34
35 @Override
36 public void setParameterString(String value) {
37 defaultValue = value;
38 }
39 }