View Javadoc
1   package com.github.mygreen.supercsv.expression;
2   
3   import java.util.Arrays;
4   import java.util.Collection;
5   import java.util.Objects;
6   import java.util.stream.Collectors;
7   
8   import com.github.mygreen.supercsv.cellprocessor.format.TextPrinter;
9   
10  /**
11   * EL式中で利用可なユーティリティ関数。
12   * 
13   * @since 2.0
14   * @author T.TSUCHIE
15   *
16   */
17  public class CustomFunctions {
18      
19      /**
20       * 文字列がnullの場合に空文字に変換する。
21       * <pre class="highlight"><code class="java">
22       *     CustomFunctions.defaultString(null) = ""
23       *     CustomFunctions.defaultString("") = ""
24       *     CustomFunctions.defaultString("abc") = "abc"
25       * </code></pre>
26       * 
27       * @param text 判定対象の文字列
28       * @return 非nullの場合は、引数の値をそのまま返す。
29       */
30      public static String defaultString(final String text) {
31          if(text == null) {
32              return "";
33          }
34          
35          return text;
36      }
37      
38      /**
39       * int型の配列の値を結合する。
40       * @param array 結合対象の配列
41       * @param delimiter 区切り文字
42       * @return 結合した文字列を返す。結合の対象の配列がnulの場合、空文字を返す。
43       */
44      public static String join(final int[] array, final String delimiter) {
45          
46          if(array == null || array.length == 0) {
47              return "";
48          }
49          
50          String value = Arrays.stream(array)
51                  .boxed()
52                  .map(String::valueOf)
53                  .collect(Collectors.joining(defaultString(delimiter)));
54          
55          return value;
56      }
57      
58      /**
59       * 配列の値を結合する。
60       * @param array 結合対象の配列
61       * @param delimiter 区切り文字
62       * @return 結合した文字列を返す。結合の対象の配列がnulの場合、空文字を返す。
63       */
64      public static String join(final Object[] array, final String delimiter) {
65          
66          if(array == null || array.length == 0) {
67              return "";
68          }
69          
70          String value = Arrays.stream(array)
71                  .map(v -> v.toString())
72                  .collect(Collectors.joining(defaultString(delimiter)));
73          
74          return value;
75      }
76      
77      /**
78       * 配列の値を結合する。
79       * @param array 結合対象の配列
80       * @param delimiter 区切り文字
81       * @param printer 配列の要素の値のフォーマッタ
82       * @return 結合した文字列を返す。結合の対象の配列がnulの場合、空文字を返す。
83       * @throws NullPointerException {@literal printer is null.}
84       */
85      @SuppressWarnings({"rawtypes", "unchecked"})
86      public static String join(final Object[] array, final String delimiter, final TextPrinter printer) {
87          
88          Objects.requireNonNull(printer);
89          
90          if(array == null || array.length == 0) {
91              return "";
92          }
93          
94          String value = Arrays.stream(array)
95                  .map(v -> printer.print(v))
96                  .collect(Collectors.joining(defaultString(delimiter)));
97          
98          return value;
99      }
100     
101     /**
102      * コレクションの値を結合する。
103      * @param collection 結合対象のコレクション
104      * @param delimiter 区切り文字
105      * @return 結合した文字列を返す。結合の対象のコレクションがnulの場合、空文字を返す。
106      */
107     public static String join(final Collection<?> collection, final String delimiter) {
108         
109         if(collection == null || collection.isEmpty()) {
110             return "";
111         }
112         
113         String value = collection.stream()
114                 .map(v -> v.toString())
115                 .collect(Collectors.joining(defaultString(delimiter)));
116         
117         return value;
118     }
119     
120     /**
121      * コレクションの値を結合する。
122      * @param collection 結合対象のコレクション
123      * @param delimiter 区切り文字
124      * @param printer コレクションの要素の値のフォーマッタ
125      * @return 結合した文字列を返す。結合の対象のコレクションがnulの場合、空文字を返す。
126      * @throws NullPointerException {@literal printer is null.}
127      */
128     @SuppressWarnings({"rawtypes", "unchecked"})
129     public static String join(final Collection<?> collection, final String delimiter, final TextPrinter printer) {
130         
131         Objects.requireNonNull(printer);
132         
133         if(collection == null || collection.isEmpty()) {
134             return "";
135         }
136         
137         String value = collection.stream()
138                 .map(v -> printer.print(v))
139                 .collect(Collectors.joining(defaultString(delimiter)));
140         
141         return value;
142     }
143     
144 }