Skip to content

Commit 7be6716

Browse files
authored
Merge pull request #335 from ozlerhakan/4.8.0
v4.8.0
2 parents bd5487d + 30fb921 commit 7be6716

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

README.adoc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:branch: 4.7.0
77

88
image:https://github.com/ozlerhakan/poiji/actions/workflows/maven.yml/badge.svg["Build Status"] image:https://app.codacy.com/project/badge/Grade/64f7e2cb9e604807b62334a4cfc3952d["Codacy code quality",link="https://www.codacy.com/gh/ozlerhakan/poiji/dashboard?utm_source=github.com&utm_medium=referral&utm_content=ozlerhakan/poiji&utm_campaign=Badge_Grade"]
9-
image:https://codecov.io/gh/ozlerhakan/poiji/branch/master/graph/badge.svg?token=MN6V6xOWBq["Codecov",link="https://codecov.io/gh/ozlerhakan/poiji"] image:https://img.shields.io/badge/apache.poi-5.4.0-brightgreen.svg[] image:https://app.fossa.com/api/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji.svg?type=shield["FOSSA Status",link="https://app.fossa.com/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji?ref=badge_shield"]
9+
image:https://codecov.io/gh/ozlerhakan/poiji/branch/master/graph/badge.svg?token=MN6V6xOWBq["Codecov",link="https://codecov.io/gh/ozlerhakan/poiji"] image:https://img.shields.io/badge/apache.poi-5.4.1-brightgreen.svg[] image:https://app.fossa.com/api/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji.svg?type=shield["FOSSA Status",link="https://app.fossa.com/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji?ref=badge_shield"]
1010

1111
Poiji is a tiny thread-safe Java library that provides one way mapping from Excel sheets to Java classes.
1212
In a way it lets us convert each row of the specified excel data into Java objects.
@@ -608,17 +608,23 @@ You can create your own casting implementation without relying on the default Po
608608

609609
[source,java]
610610
----
611-
public class MyCasting implements Casting {
611+
import com.poiji.config.DefaultCasting;
612+
613+
public class ModifiedBooleanCasting extends DefaultCasting {
612614
@Override
613-
public Object castValue(Class<?> fieldType, String value, PoijiOptions options) {
614-
return value.trim();
615+
Boolean booleanValue(String value, String sheetName, int row, int col, PoijiOptions options) {
616+
if (!value.equals("y") && !value.equals("n")) {
617+
return onError(value, sheetName, row, col, bpe, options.preferNullOverDefault() ? null : false);
618+
} else {
619+
return value.equals("y");
620+
}
615621
}
616622
}
617623
618624
public class Person {
619625
620626
@ExcelCell(0)
621-
protected String employeeId;
627+
protected Boolean employed;
622628
623629
@ExcelCell(1)
624630
protected String name;
@@ -634,7 +640,7 @@ Then you can add your custom implementation with the `withCasting` method:
634640
[source,java]
635641
----
636642
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings()
637-
.withCasting(new MyCasting())
643+
.withCasting(new ModifiedBooleanCasting())
638644
.build();
639645
640646
List<Person> people = Poiji.fromExcel(excel, Person.class, options);

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
</issueManagement>
4646

4747
<properties>
48-
<apache.poi.version>5.4.0</apache.poi.version>
48+
<apache.poi.version>5.4.1</apache.poi.version>
4949
<junit.version>4.13.2</junit.version>
5050
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
5151
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/com/poiji/config/DefaultCasting.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public DefaultCasting(boolean errorLoggingEnabled) {
3535
this.errorLoggingEnabled = errorLoggingEnabled;
3636
}
3737

38-
private <T> T onError(String value, String sheetName, int row, int col, Exception exception, T defaultValue) {
38+
protected <T> T onError(String value, String sheetName, int row, int col, Exception exception, T defaultValue) {
3939
logError(value, defaultValue, sheetName, row, col, exception);
4040
return defaultValue;
4141
}
@@ -46,87 +46,87 @@ private void logError(String value, Object defaultValue, String sheetName, int r
4646
}
4747
}
4848

49-
private Boolean primitiveBooleanValue(String value, String sheetName, int row, int col) {
49+
protected Boolean primitiveBooleanValue(String value, String sheetName, int row, int col) {
5050
try {
5151
return Parsers.booleans().parse(value);
5252
} catch (BooleanParser.BooleanParseException bpe) {
5353
return onError(value, sheetName, row, col, bpe, false);
5454
}
5555
}
5656

57-
private Boolean booleanValue(String value, String sheetName, int row, int col, PoijiOptions options) {
57+
protected Boolean booleanValue(String value, String sheetName, int row, int col, PoijiOptions options) {
5858
try {
5959
return Parsers.booleans().parse(value);
6060
} catch (BooleanParser.BooleanParseException bpe) {
6161
return onError(value, sheetName, row, col, bpe, options.preferNullOverDefault() ? null : false);
6262
}
6363
}
6464

65-
private int primitiveIntegerValue(String value, String sheetName, int row, int col) {
65+
protected int primitiveIntegerValue(String value, String sheetName, int row, int col) {
6666
try {
6767
return Parsers.integers().parse(value).intValue();
6868
} catch (NumberFormatException nfe) {
6969
return onError(value, sheetName, row, col, nfe, 0);
7070
}
7171
}
7272

73-
private Integer integerValue(String value, String sheetName, int row, int col, PoijiOptions options) {
73+
protected Integer integerValue(String value, String sheetName, int row, int col, PoijiOptions options) {
7474
try {
7575
return Parsers.integers().parse(value).intValue();
7676
} catch (NumberFormatException nfe) {
7777
return onError(value, sheetName, row, col, nfe, options.preferNullOverDefault() ? null : 0);
7878
}
7979
}
8080

81-
private long primitiveLongValue(String value, String sheetName, int row, int col) {
81+
protected long primitiveLongValue(String value, String sheetName, int row, int col) {
8282
try {
8383
return Parsers.longs().parse(value).longValue();
8484
} catch (NumberFormatException nfe) {
8585
return onError(value, sheetName, row, col, nfe, 0L);
8686
}
8787
}
8888

89-
private Long longValue(String value, String sheetName, int row, int col, PoijiOptions options) {
89+
protected Long longValue(String value, String sheetName, int row, int col, PoijiOptions options) {
9090
try {
9191
return Parsers.longs().parse(value).longValue();
9292
} catch (NumberFormatException nfe) {
9393
return onError(value, sheetName, row, col, nfe, options.preferNullOverDefault() ? null : 0L);
9494
}
9595
}
9696

97-
private double primitiveDoubleValue(String value, String sheetName, int row, int col, PoijiOptions options) {
97+
protected double primitiveDoubleValue(String value, String sheetName, int row, int col, PoijiOptions options) {
9898
try {
9999
return Parsers.numbers(options.getLocale()).parse(value).doubleValue();
100100
} catch (NumberFormatException nfe) {
101101
return onError(value, sheetName, row, col, nfe, 0d);
102102
}
103103
}
104104

105-
private Double doubleValue(String value, String sheetName, int row, int col, PoijiOptions options) {
105+
protected Double doubleValue(String value, String sheetName, int row, int col, PoijiOptions options) {
106106
try {
107107
return Parsers.numbers(options.getLocale()).parse(value).doubleValue();
108108
} catch (NumberFormatException nfe) {
109109
return onError(value, sheetName, row, col, nfe, options.preferNullOverDefault() ? null : 0d);
110110
}
111111
}
112112

113-
private float primitiveFloatValue(String value, String sheetName, int row, int col, PoijiOptions options) {
113+
protected float primitiveFloatValue(String value, String sheetName, int row, int col, PoijiOptions options) {
114114
try {
115115
return Parsers.numbers(options.getLocale()).parse(value).floatValue();
116116
} catch (NumberFormatException nfe) {
117117
return onError(value, sheetName, row, col, nfe, 0f);
118118
}
119119
}
120120

121-
private Float floatValue(String value, String sheetName, int row, int col, PoijiOptions options) {
121+
protected Float floatValue(String value, String sheetName, int row, int col, PoijiOptions options) {
122122
try {
123123
return Parsers.numbers(options.getLocale()).parse(value).floatValue();
124124
} catch (NumberFormatException nfe) {
125125
return onError(value, sheetName, row, col, nfe, options.preferNullOverDefault() ? null : 0f);
126126
}
127127
}
128128

129-
private BigDecimal bigDecimalValue(String value, String sheetName, int row, int col, PoijiOptions options) {
129+
protected BigDecimal bigDecimalValue(String value, String sheetName, int row, int col, PoijiOptions options) {
130130
try {
131131
return Parsers.bigDecimals(options.getLocale()).parse(value);
132132
} catch (NumberFormatException | IllegalStateException e) {
@@ -146,7 +146,7 @@ private BigDecimal bigDecimalValue(String value, String sheetName, int row, int
146146
* date object without any exceptions but since the string was not an exact
147147
* match you get a very strange date
148148
*/
149-
private Date dateValue(String value, String sheetName, int row, int col, PoijiOptions options) {
149+
protected Date dateValue(String value, String sheetName, int row, int col, PoijiOptions options) {
150150

151151
if (options.getDateRegex() != null && !value.matches(options.getDateRegex())) {
152152
return options.preferNullOverDefault() ? null : Calendar.getInstance().getTime();
@@ -174,7 +174,7 @@ private Date dateValue(String value, String sheetName, int row, int col, PoijiOp
174174
* match you get a very strange date
175175
*
176176
*/
177-
private LocalDate localDateValue(String value, String sheetName, int row, int col, PoijiOptions options) {
177+
protected LocalDate localDateValue(String value, String sheetName, int row, int col, PoijiOptions options) {
178178
if (options.getDateRegex() != null && !value.matches(options.getDateRegex())) {
179179
return options.preferNullOverDefault() ? null : LocalDate.now();
180180
} else {
@@ -186,7 +186,7 @@ private LocalDate localDateValue(String value, String sheetName, int row, int co
186186
}
187187
}
188188

189-
private LocalDateTime localDateTimeValue(String value, String sheetName, int row, int col, PoijiOptions options) {
189+
protected LocalDateTime localDateTimeValue(String value, String sheetName, int row, int col, PoijiOptions options) {
190190
if (options.getDateTimeRegex() != null && !value.matches(options.getDateTimeRegex())) {
191191
return options.preferNullOverDefault() ? null : LocalDateTime.now();
192192
} else {
@@ -199,7 +199,7 @@ private LocalDateTime localDateTimeValue(String value, String sheetName, int row
199199
}
200200
}
201201

202-
private Object enumValue(String value, String sheetName, int row, int col, Class<?> type) {
202+
protected Object enumValue(String value, String sheetName, int row, int col, Class<?> type) {
203203
return Arrays.stream(type.getEnumConstants())
204204
.filter(o -> ((Enum<?>) o).name().equals(value))
205205
.findFirst()
@@ -210,7 +210,7 @@ private Object enumValue(String value, String sheetName, int row, int col, Class
210210
});
211211
}
212212

213-
private Object castListValue(String value, String sheetName, int row, int col, Field field, PoijiOptions options) {
213+
protected Object castListValue(String value, String sheetName, int row, int col, Field field, PoijiOptions options) {
214214
final ParameterizedType genericType = (ParameterizedType) field.getGenericType();
215215
final Type fieldType = genericType.getActualTypeArguments()[0];
216216
String[] valueList = value.split(options.getListDelimiter());

src/main/java/com/poiji/config/DefaultCastingError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class DefaultCastingError {
1313

1414
private Exception exception;
1515

16-
DefaultCastingError(String value, Object defaultValue, String sheetName, int row, int column, Exception exception) {
16+
public DefaultCastingError(String value, Object defaultValue, String sheetName, int row, int column, Exception exception) {
1717
this.value = value;
1818
this.defaultValue = defaultValue;
1919
this.sheetName = sheetName;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.poiji.config;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Method;
6+
import java.lang.reflect.Modifier;
7+
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
import static org.hamcrest.CoreMatchers.*;
10+
11+
public class DefaultCastingTest {
12+
13+
@Test
14+
public void valueMethodsShouldBeProtectedOrPublic() {
15+
Method[] allMethods = DefaultCasting.class.getMethods();
16+
for (Method method : allMethods) {
17+
if (method.getName().endsWith("Value")) {
18+
assertThat(
19+
"Method " + method.getName() + " should be protected or public",
20+
Modifier.isProtected(method.getModifiers()) || Modifier.isPublic(method.getModifiers()),
21+
is(true)
22+
);
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)