View Javadoc
1   /*
2    * Copyright 2016 Andrew Rucker Jones.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.opencsv.bean.customconverter;
17  
18  import com.opencsv.bean.AbstractBeanField;
19  import com.opencsv.exceptions.CsvDataTypeMismatchException;
20  import org.apache.commons.lang3.StringUtils;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  /**
27   * This class takes a string and splits it on whitespace into a list of strings.
28   *
29   * @param <T> Type of the bean to be manipulated
30   * @param <I> Type of the index into a multivalued field
31   *
32   * @author Andrew Rucker Jones
33   */
34  public class ConvertSplitOnWhitespace<T, I> extends AbstractBeanField<T, I> {
35  
36      /**
37       * Silence code style checker by adding a useless constructor.
38       */
39      public ConvertSplitOnWhitespace() {
40      }
41  
42      /**
43       * Takes a string that is a list of substrings separated by whitespace and
44       * returns a list of the substrings.
45       * For example, the string "Jones Smith Cartwright Cooper" would be
46       * converted by this method to ("Jones", "Smith", "Cartwright", "Cooper").
47       * When might this be useful? A CSV has a set number of columns, but
48       * sometimes some fields might need to contain a variable number of entries.
49       * In this case, a list within one field of a CSV might make sense.
50       *
51       * @param value The string to be converted
52       * @return List&lt;String&gt; consisting of the substrings from the input
53       * that were separated in the input by whitespace
54       * 
55       */
56      @Override
57      protected Object convert(String value) {
58          List<String> l = null;
59          if (!StringUtils.isEmpty(value)) {
60              l = new ArrayList<>(Arrays.asList(value.split("\\s+")));
61          }
62          return l;
63      }
64      
65      /**
66       * This method takes the current value of the field in question in the bean
67       * passed in and converts it to a string.
68       * 
69       * @return The concatenation of a list of strings, with every entry
70       *   separated by a space
71       * @throws CsvDataTypeMismatchException If the field is not a list of strings
72       */
73      @Override
74      protected String convertToWrite(Object value) throws CsvDataTypeMismatchException {
75          String result = "";
76          try {
77              if(value != null) {
78                  @SuppressWarnings("unchecked") List<String> values = (List<String>) value;
79                  result = String.join(" ", values);
80              }
81          }
82          catch(ClassCastException e) {
83              CsvDataTypeMismatchException csve =
84                      new CsvDataTypeMismatchException("The field must be of type List<String>.");
85              csve.initCause(e);
86              throw csve;
87          }
88          return result;
89      }
90  
91  }