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;
15
16 import java.util.List;
17
18 import olg.csv.base.Row;
19 import olg.csv.bean.filter.AbstractStringFilter;
20 import olg.csv.bean.getter.impl.ConcateGetter;
21 import olg.csv.bean.getter.impl.ConstantGetter;
22 import olg.csv.bean.getter.impl.DefaultGetter;
23
24 /**
25 * This class extracts a string from a row. In the bean reading process from a
26 * Strings List, this class is used to extract a string identified as property
27 * of the bean to produce.
28 *
29 * @author Olivier Godineau
30 *
31 */
32 public abstract class AbstractGetter {
33
34 /**
35 * This value is apply when this getter cannot extract String from a row.
36 * <code>null</code> as default value.
37 */
38 private String defaultValue = null;
39
40 /**
41 * An optional filter.<code>null</code> as default value.
42 */
43 private AbstractStringFilter filter = null;
44
45 /**
46 * In case of this getter cannot extract a string from list, it's possible
47 * to apply a default value in replacement of.
48 *
49 * @return the default value
50 * @see #doGet(Row)
51 */
52 public final String getDefaultValue() {
53 return defaultValue;
54 }
55
56 /**
57 * Sets the given value as default value.
58 *
59 * @see AbstractGetter#doGet(Row)
60 *
61 * @param defaultValue
62 * the default value to use when the filtered "getted" string is
63 * <code>null</code> or empty.
64 */
65 public final void setDefaultValue(String defaultValue) {
66 this.defaultValue = defaultValue;
67 }
68
69 /**
70 * Returns the filter used to filter the String returned by
71 * {@link #doGet(Row)}.
72 *
73 * @return the filter.
74 */
75 public final AbstractStringFilter getFilter() {
76 return filter;
77 }
78
79 /**
80 * Sets the given filter.
81 *
82 * @param filter
83 * the fitler.
84 */
85 public final void setFilter(AbstractStringFilter filter) {
86 this.filter = filter;
87 }
88
89 /**
90 * Returns a String built from the given row.
91 *
92 * @param line
93 * the row from which extract the string.
94 * @return a String built from the given row
95 */
96 protected abstract String doGet(Row line);
97
98 /**
99 * Returns a String from a row. if a filter is defined, the string returned
100 * by doGet method is filtered.If this String is empty or null, the default
101 * value is returned.
102 * <p>
103 * throws an IllegalArgumentException When Error occurs if the given line is
104 * <code>null</code>.
105 * </p>
106 *
107 * @param line
108 * the row.
109 * @return the string extracted from the row.
110 */
111 public final String get(Row line) {
112 if (line == null) {
113 throw new IllegalArgumentException("AbstractGetter#get Row argument must be not null");
114 }
115
116 String got = (filter == null ? doGet(line) : filter.filtre(doGet(line)));
117 return (got == null || "".equals(got) ? defaultValue : got);
118
119 }
120
121 /**
122 * Returns a new instance of the default getter. This getter extracts the
123 * string at the given rank in the row.
124 *
125 * @param rang
126 * the cell number in the row. Must be greater or equals than 0
127 * or conformed to the sheet cell number format.
128 * @param defaultValue
129 * default value to apply if the filtered String at the given
130 * rank is <code>null</code> or empty.
131 * @return the default getter.
132 */
133 public static final AbstractGetter getDefault(String rang, String defaultValue) {
134
135 AbstractGetter getter = new DefaultGetter(rang);
136 getter.setDefaultValue(defaultValue);
137 return getter;
138 }
139
140 /**
141 * Returns a new instance of a concate Getter. This getter products a string
142 * based on concate strings issued of the givent Getters List.
143 *
144 * @param getters
145 * List of AbstractGetters which product a String from a Row.
146 * Must not be <code>null</code> or empty.
147 * @param defaultValue
148 * default value to apply if the filtered string from concate is
149 * <code>null</code> or empty.
150 * @return an instance of a concate getter.
151 */
152 public static final AbstractGetter getConcate(List<AbstractGetter> getters, String defaultValue) {
153 if (getters == null || getters.isEmpty()) {
154 throw new IllegalArgumentException("getConcate getters argument must be not null and not empty");
155 }
156
157 return new ConcateGetter(getters, defaultValue);
158 }
159
160 /**
161 * Returns a new instance of a constant Getter. This getter always return
162 * the given value regardless of the row.
163 *
164 * @param defaultValue
165 * the value the getter returns
166 * @return an instance of a constant getter.
167 */
168 public static final AbstractGetter getConstant(String defaultValue) {
169 return new ConstantGetter(defaultValue);
170 }
171 }