1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.cellprocessor.constraint;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.supercsv.cellprocessor.CellProcessorAdaptor;
23 import org.supercsv.cellprocessor.ift.CellProcessor;
24 import org.supercsv.cellprocessor.ift.StringCellProcessor;
25 import org.supercsv.exception.SuperCsvCellProcessorException;
26 import org.supercsv.exception.SuperCsvConstraintViolationException;
27 import org.supercsv.util.CsvContext;
28
29
30
31
32
33
34
35
36 public class RequireSubStr extends CellProcessorAdaptor implements StringCellProcessor {
37
38 private final List<String> requiredSubStrings = new ArrayList<String>();
39
40
41
42
43
44
45
46
47
48
49
50 public RequireSubStr(final String... requiredSubStrings) {
51 super();
52 checkPreconditions(requiredSubStrings);
53 checkAndAddRequiredSubStrings(requiredSubStrings);
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public RequireSubStr(final List<String> requiredSubStrings, final CellProcessor next) {
70 super(next);
71 checkPreconditions(requiredSubStrings);
72 checkAndAddRequiredSubStrings(requiredSubStrings);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86 public RequireSubStr(final String requiredSubString, final CellProcessor next) {
87 super(next);
88 checkPreconditions(requiredSubString);
89 checkAndAddRequiredSubStrings(requiredSubString);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public RequireSubStr(final String[] requiredSubStrings, final CellProcessor next) {
106 super(next);
107 checkPreconditions(requiredSubStrings);
108 checkAndAddRequiredSubStrings(requiredSubStrings);
109 }
110
111
112
113
114
115
116
117
118
119
120
121 private static void checkPreconditions(String... requiredSubStrings) {
122 if( requiredSubStrings == null ) {
123 throw new NullPointerException("requiredSubStrings array should not be null");
124 } else if( requiredSubStrings.length == 0 ) {
125 throw new IllegalArgumentException("requiredSubStrings array should not be empty");
126 }
127 }
128
129
130
131
132
133
134
135
136
137
138
139 private static void checkPreconditions(List<String> requiredSubStrings) {
140 if( requiredSubStrings == null ) {
141 throw new NullPointerException("requiredSubStrings List should not be null");
142 } else if( requiredSubStrings.isEmpty() ) {
143 throw new IllegalArgumentException("requiredSubStrings List should not be empty");
144 }
145 }
146
147
148
149
150
151
152
153
154
155 private void checkAndAddRequiredSubStrings(final List<String> requiredSubStrings) {
156 for( String required : requiredSubStrings ) {
157 if( required == null ) {
158 throw new NullPointerException("required substring should not be null");
159 }
160 this.requiredSubStrings.add(required);
161 }
162 }
163
164
165
166
167
168
169
170
171
172 private void checkAndAddRequiredSubStrings(final String... requiredSubStrings) {
173 checkAndAddRequiredSubStrings(Arrays.asList(requiredSubStrings));
174 }
175
176
177
178
179
180
181
182
183
184 public Object execute(final Object value, final CsvContext context) {
185 validateInputNotNull(value, context);
186
187 final String stringValue = value.toString();
188
189 for( final String required : requiredSubStrings ) {
190 if( stringValue.contains(required) ) {
191 return next.execute(value, context);
192 }
193 }
194
195 throw new SuperCsvConstraintViolationException(String.format("'%s' does not contain any of the required substrings", value),
196 context, this);
197 }
198 }