1515import java .time .LocalDateTime ;
1616import java .time .format .DateTimeParseException ;
1717import java .util .*;
18+ import java .util .function .Predicate ;
1819import java .util .stream .Collectors ;
1920import java .util .stream .Stream ;
2021
@@ -133,16 +134,20 @@ private BigDecimal bigDecimalValue(String value, String sheetName, int row, int
133134 }
134135 }
135136
137+ /*
138+ *
139+ * ISSUE #57
140+ * if a date regex has been specified then it wont be null
141+ * so then make sure the string matches the pattern
142+ * if it doesn't, fall back to default
143+ * else continue to turn string into java date
144+ *
145+ * the reason for this is sometime Java will manage to parse a string to a
146+ * date object without any exceptions but since the string was not an exact
147+ * match you get a very strange date
148+ */
136149 private Date dateValue (String value , String sheetName , int row , int col , PoijiOptions options ) {
137150
138- //ISSUE #57
139- //if a date regex has been specified then it wont be null
140- //so then make sure the string matches the pattern
141- //if it doesn't, fall back to default
142- //else continue to turn string into java date
143-
144- //the reason for this is sometime Java will manage to parse a string to a date object
145- //without any exceptions but since the string was not an exact match you get a very strange date
146151 if (options .getDateRegex () != null && !value .matches (options .getDateRegex ())) {
147152 return options .preferNullOverDefault () ? null : Calendar .getInstance ().getTime ();
148153 } else {
@@ -151,21 +156,25 @@ private Date dateValue(String value, String sheetName, int row, int col, PoijiOp
151156 sdf .setLenient (options .getDateLenient ());
152157 return sdf .parse (value );
153158 } catch (ParseException e ) {
154- return onError (value , sheetName , row , col , e , options .preferNullOverDefault () ? null : Calendar .getInstance ().getTime ());
159+ return onError (value , sheetName , row , col , e ,
160+ options .preferNullOverDefault () ? null : Calendar .getInstance ().getTime ());
155161 }
156162 }
157163 }
158164
165+ /*
166+ * ISSUE #57
167+ * if a date regex has been specified then it wont be null
168+ * so then make sure the string matches the pattern
169+ * if it doesn't, fall back to default
170+ * else continue to turn string into java date
171+ *
172+ * the reason for this is sometime java will manage to parse a string to a
173+ * date object without any exceptions but since the string was not an exact
174+ * match you get a very strange date
175+ *
176+ */
159177 private LocalDate localDateValue (String value , String sheetName , int row , int col , PoijiOptions options ) {
160-
161- //ISSUE #57
162- //if a date regex has been specified then it wont be null
163- //so then make sure the string matches the pattern
164- //if it doesn't, fall back to default
165- //else continue to turn string into java date
166-
167- //the reason for this is sometime java will manage to parse a string to a date object
168- //without any exceptions but since the string was not an exact match you get a very strange date
169178 if (options .getDateRegex () != null && !value .matches (options .getDateRegex ())) {
170179 return options .preferNullOverDefault () ? null : LocalDate .now ();
171180 } else {
@@ -184,18 +193,19 @@ private LocalDateTime localDateTimeValue(String value, String sheetName, int row
184193 try {
185194 return LocalDateTime .parse (value , options .dateTimeFormatter ());
186195 } catch (DateTimeParseException e ) {
187- return onError (value , sheetName , row , col , e , options .preferNullOverDefault () ? null : LocalDateTime .now ());
196+ return onError (value , sheetName , row , col , e ,
197+ options .preferNullOverDefault () ? null : LocalDateTime .now ());
188198 }
189199 }
190200 }
191201
192-
193202 private Object enumValue (String value , String sheetName , int row , int col , Class <?> type ) {
194203 return Arrays .stream (type .getEnumConstants ())
195204 .filter (o -> ((Enum <?>) o ).name ().equals (value ))
196205 .findFirst ()
197206 .orElseGet (() -> {
198- IllegalArgumentException e = new IllegalArgumentException ("No enumeration " + type .getSimpleName () + "." + value );
207+ IllegalArgumentException e = new IllegalArgumentException (
208+ "No enumeration " + type .getSimpleName () + "." + value );
199209 return onError (value , sheetName , row , col , e , null );
200210 });
201211 }
@@ -204,33 +214,34 @@ private Object castListValue(String value, String sheetName, int row, int col, F
204214 final ParameterizedType genericType = (ParameterizedType ) field .getGenericType ();
205215 final Type fieldType = genericType .getActualTypeArguments ()[0 ];
206216 String [] valueList = value .split (options .getListDelimiter ());
217+ Stream <String > valueStream = Stream .of (valueList ).filter (Predicate .not (String ::isEmpty ));
207218
208219 if (fieldType == Integer .class ) {
209- return Stream . of ( valueList )
220+ return valueStream
210221 .map (rv -> primitiveIntegerValue (rv , sheetName , row , col ))
211222 .collect (Collectors .toList ());
212223 } else if (fieldType == BigDecimal .class ) {
213- return Stream . of ( valueList )
224+ return valueStream
214225 .map (rv -> bigDecimalValue (rv , sheetName , row , col , options ))
215226 .collect (Collectors .toList ());
216227 } else if (fieldType == Long .class ) {
217- return Stream . of ( valueList )
228+ return valueStream
218229 .map (rv -> longValue (rv , sheetName , row , col , options ))
219230 .collect (Collectors .toList ());
220231 } else if (fieldType == Double .class ) {
221- return Stream . of ( valueList )
232+ return valueStream
222233 .map (rv -> doubleValue (rv , sheetName , row , col , options ))
223234 .collect (Collectors .toList ());
224235 } else if (fieldType == Boolean .class ) {
225- return Stream . of ( valueList )
236+ return valueStream
226237 .map (rv -> booleanValue (rv , sheetName , row , col , options ))
227238 .collect (Collectors .toList ());
228239 } else if (fieldType == Float .class ) {
229- return Stream . of ( valueList )
240+ return valueStream
230241 .map (rv -> floatValue (rv , sheetName , row , col , options ))
231242 .collect (Collectors .toList ());
232243 } else {
233- return Arrays . asList ( valueList );
244+ return valueStream . collect ( Collectors . toList () );
234245 }
235246 }
236247
@@ -240,7 +251,8 @@ public Object castValue(Field field, String rawValue, int row, int col, PoijiOpt
240251 return getValueObject (field , row , col , options , rawValue , fieldType );
241252 }
242253
243- protected Object getValueObject (Field field , int row , int col , PoijiOptions options , String rawValue , Class <?> fieldType ) {
254+ protected Object getValueObject (Field field , int row , int col , PoijiOptions options , String rawValue ,
255+ Class <?> fieldType ) {
244256 String sheetName = options .getSheetName ();
245257 String value = options .trimCellValue () ? rawValue .trim () : rawValue ;
246258
@@ -289,11 +301,10 @@ protected Object getValueObject(Field field, int row, int col, PoijiOptions opti
289301
290302 } else if (fieldType .isEnum ()) {
291303 o = enumValue (value , sheetName , row , col , fieldType );
292-
293- } else if (value .isEmpty ()) {
294- o = options .preferNullOverDefault () ? null : value ;
295304 } else if (fieldType == List .class ) {
296305 o = castListValue (value , sheetName , row , col , field , options );
306+ } else if (value .isEmpty ()) {
307+ o = options .preferNullOverDefault () ? null : value ;
297308 } else {
298309 o = value ;
299310
0 commit comments