1 /*
2 * Copyright 2012 Olivier Godineau
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 * License for the specific language governing permissions and limitations under
12 * the License.
13 */
14 package olg.csv.bean.getter.impl;
15
16 import java.util.List;
17
18 import olg.csv.base.Row;
19 import olg.csv.bean.getter.AbstractGetter;
20
21 /**
22 * This getter builds a String from a row by assembly Strings which are
23 * producted by a list of getters.
24 *
25 */
26 public final class ConcateGetter extends AbstractGetter {
27
28 /**
29 * Returns The list of Getters.
30 *
31 * @return the getters.
32 */
33 // NOPMD by olivier on 01/02/12 00:22
34 public List<AbstractGetter> getGetters() {
35 return getters;
36 }
37
38 /**
39 * Sets the Getters list.
40 *
41 * @param getters
42 * the getter list.
43 */
44 public void setGetters(List<AbstractGetter> getters) {
45 this.getters = getters;
46 }
47
48 /**
49 * The Getters which product the Strings to assemble.
50 */
51 private List<AbstractGetter> getters;
52
53 /**
54 *
55 * @param getters
56 * Getters List that product Strings. Must not be null or empty.
57 * @param defaultValue
58 * the value to return when assembly products an empty or
59 * {@link NullPointerException} String
60 */
61 public ConcateGetter(List<AbstractGetter> getters, String defaultValue) {
62 super();
63 this.getters = getters;
64 setDefaultValue(defaultValue);
65 }
66
67 @Override
68 protected String doGet(Row line) {
69 StringBuffer str = new StringBuffer();
70 for (AbstractGetter getter : getters) {
71 String value = getter.get(line);
72 if (value != null) {
73 str.append(value);
74 }
75 }
76 return str.toString();
77 }
78
79 }