Skip to content

Commit f3c7afd

Browse files
Changes to Bean-handling in BeanSchema and BeanIterator
1 parent 7022eab commit f3c7afd

40 files changed

+577
-520
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>io.frictionlessdata</groupId>
55
<artifactId>tableschema-java</artifactId>
6-
<version>0.2.4-SNAPSHOT</version>
6+
<version>0.3.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<issueManagement>
99
<url>https://github.com/frictionlessdata/tableschema-java/issues</url>

src/main/java/io/frictionlessdata/tableschema/Table.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public Iterator<Object[]> iterator(boolean keyed, boolean extended, boolean cast
192192
return new TableIterator<>(this, keyed, extended, cast, relations);
193193
}
194194

195-
public BeanIterator iterator(Class<?> beanType, boolean relations) throws Exception{
195+
public BeanIterator<?> iterator(Class<?> beanType, boolean relations) throws Exception{
196196
return new BeanIterator(this, beanType, relations);
197197
}
198198

@@ -280,6 +280,7 @@ public List<Object[]> read() throws Exception{
280280
boolean cast = (null != schema);
281281
return read(cast);
282282
}
283+
283284
public String asJson() {
284285
try {
285286
List<Map<String, Object>> arr = new ArrayList<>();
@@ -342,7 +343,7 @@ private void writeCsv(Writer out, CSVFormat format, String[] sortedHeaders) {
342343
? format
343344
: DataSourceFormat.getDefaultCsvFormat();
344345

345-
locFormat = locFormat.withHeader(sortedHeaders);
346+
locFormat = locFormat.builder().setHeader(sortedHeaders).build();
346347
CSVPrinter csvPrinter = new CSVPrinter(out, locFormat);
347348

348349
String[] headers = getHeaders();
@@ -367,7 +368,7 @@ public void write(Writer out, DataSourceFormat.Format dataFormat) {
367368
try {
368369
if (dataFormat.equals(DataSourceFormat.Format.FORMAT_CSV)) {
369370
try {
370-
String[] headers = null;
371+
String[] headers;
371372
if (null != schema) {
372373
List<String> fieldNames = schema.getFieldNames();
373374
headers = fieldNames.toArray(new String[0]);
@@ -438,7 +439,7 @@ public void writeCsv(File outputFile, CSVFormat format) throws Exception{
438439
public void validate() throws TableValidationException, TableSchemaException {
439440
if (null == schema)
440441
return;
441-
String[] headers = null;
442+
String[] headers;
442443
try {
443444
headers = this.dataSourceFormat.getHeaders();
444445
} catch (Exception ex) {
@@ -541,7 +542,7 @@ public boolean equals(Object o) {
541542
if (this == o) return true;
542543
if (o == null || getClass() != o.getClass()) return false;
543544
Table table = (Table) o;
544-
boolean equals = false;
545+
boolean equals;
545546
try {
546547
equals = Arrays.equals(table.getHeaders(), ((Table) o).getHeaders());
547548
if ((this.schema != null) && (table.schema != null)) {

src/main/java/io/frictionlessdata/tableschema/datasourceformat/CsvDataSourceFormat.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,7 @@ public String[] getHeaders() throws Exception{
9595
// Get a copy of the header map that iterates in column order.
9696
// The map keys are column names. The map values are 0-based indices.
9797
Map<String, Integer> headerMap = this.getCSVParser().getHeaderMap();
98-
99-
// Generate list of keys
100-
List<String> headerVals = new ArrayList<>();
101-
102-
headerMap.entrySet().forEach((pair) -> {
103-
headerVals.add(pair.getKey());
104-
});
105-
106-
headers = headerVals.toArray(new String[0]);
98+
headers = headerMap.keySet().toArray(new String[0]);
10799
}
108100
return headers;
109101
}

src/main/java/io/frictionlessdata/tableschema/datasourceformat/DataSourceFormat.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/*
2-
*
3-
*/
41
package io.frictionlessdata.tableschema.datasourceformat;
52

63
import io.frictionlessdata.tableschema.inputstream.ByteOrderMarkStrippingInputStream;
@@ -22,8 +19,8 @@
2219
* Interface for a source of tabular data.
2320
*/
2421
public interface DataSourceFormat {
25-
public static final String UTF16_BOM = "\ufeff";
26-
public static final String UTF8_BOM = "\u00ef\u00bb\u00bf";
22+
String UTF16_BOM = "\ufeff";
23+
String UTF8_BOM = "\u00ef\u00bb\u00bf";
2724
/**
2825
* Returns an Iterator that returns String arrays containing
2926
* one row of data each.
@@ -123,9 +120,11 @@ static String readSkippingBOM(InputStream is) throws IOException {
123120

124121
static CSVFormat getDefaultCsvFormat() {
125122
return CSVFormat.RFC4180
126-
.withHeader()
127-
.withIgnoreSurroundingSpaces(true)
128-
.withRecordSeparator("\n");
123+
.builder()
124+
.setHeader()
125+
.setIgnoreSurroundingSpaces(true)
126+
.setRecordSeparator("\n")
127+
.build();
129128
}
130129

131130
/**
@@ -134,7 +133,7 @@ static CSVFormat getDefaultCsvFormat() {
134133
* @return DataSource created from input String
135134
*/
136135
static DataSourceFormat createDataSourceFormat(InputStream input) throws IOException {
137-
String content = null;
136+
String content;
138137

139138
// Read the file.
140139
try (Reader fr = new InputStreamReader(input)) {
@@ -174,7 +173,7 @@ static Path toSecure(Path testPath, Path referencePath) throws IOException {
174173
}
175174
final Path resolvedPath = referencePath.resolve(testPath).normalize();
176175
if (!Files.exists(resolvedPath))
177-
throw new FileNotFoundException("File "+resolvedPath.toString()+" does not exist");
176+
throw new FileNotFoundException("File "+resolvedPath+" does not exist");
178177
if (!resolvedPath.toFile().isFile()){
179178
throw new IllegalArgumentException("Input must be a file");
180179
}

src/main/java/io/frictionlessdata/tableschema/datasourceformat/JsonArrayDataSourceFormat.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
package io.frictionlessdata.tableschema.datasourceformat;
22

3-
import com.fasterxml.jackson.core.JsonGenerator;
43
import com.fasterxml.jackson.databind.JsonNode;
5-
import com.fasterxml.jackson.databind.JsonSerializer;
6-
import com.fasterxml.jackson.databind.Module;
7-
import com.fasterxml.jackson.databind.SerializerProvider;
8-
import com.fasterxml.jackson.databind.module.SimpleModule;
9-
import com.fasterxml.jackson.databind.module.SimpleSerializers;
104
import com.fasterxml.jackson.databind.node.ArrayNode;
115
import com.fasterxml.jackson.databind.node.ObjectNode;
12-
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
136
import com.google.common.collect.Iterators;
147
import io.frictionlessdata.tableschema.util.JsonUtil;
158

@@ -74,36 +67,10 @@ public Iterator<String[]> iterator() {
7467
public String[] getHeaders() {
7568
Set<String> headers = new LinkedHashSet<>();
7669
((ArrayNode)dataSource).elements().forEachRemaining((firstObject) -> {
77-
Map<String, JsonNode> fields = new HashMap<>();
7870
firstObject.fields().forEachRemaining(f -> {
79-
fields.put(f.getKey(), f.getValue());
8071
headers.add(f.getKey());
8172
});
8273
});
8374
return headers.toArray(new String[]{});
8475
}
85-
86-
private CsvMapper getCsvMapper() {
87-
return CsvMapper.builder()
88-
.addModule(complexObjectSerializationModule())
89-
.build();
90-
}
91-
92-
private Module complexObjectSerializationModule() {
93-
SimpleModule module = new SimpleModule();
94-
module.setSerializers(new SimpleSerializers());
95-
module.addSerializer(ObjectNode.class, mapSerializer());
96-
return module;
97-
}
98-
99-
private JsonSerializer<ObjectNode> mapSerializer() {
100-
return new JsonSerializer<ObjectNode>() {
101-
102-
@Override
103-
public void serialize(ObjectNode value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
104-
gen.writeString(value.toString());
105-
}
106-
107-
};
108-
}
10976
}

src/main/java/io/frictionlessdata/tableschema/datasourceformat/StringArrayDataSourceFormat.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@
1313
*
1414
*/
1515
public class StringArrayDataSourceFormat extends AbstractDataSourceFormat {
16-
private String[] headers;
16+
private final String[] headers;
1717

1818
public StringArrayDataSourceFormat(Collection<String[]> data, String[] headers){
1919
this.dataSource = data;
2020
this.headers = headers;
2121
}
2222

23-
CSVParser getCSVParser() {
24-
throw new TableSchemaException("Not implemented for StringArrayDataSourceFormat");
25-
};
26-
2723
@Override
2824
public Iterator<String[]> iterator() throws Exception{
2925
return ((Collection<String[]>)dataSource).iterator();

src/main/java/io/frictionlessdata/tableschema/field/AnyField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public AnyField(String name) {
1717
}
1818

1919
public AnyField(String name, String format, String title, String description,
20-
URI rdfType, Map constraints, Map options){
20+
URI rdfType, Map<String, Object> constraints, Map<String, Object> options){
2121
super(name, FIELD_TYPE_ANY, format, title, description, rdfType, constraints, options);
2222
}
2323

src/main/java/io/frictionlessdata/tableschema/field/ArrayField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ArrayField(String name) {
1919
}
2020

2121
public ArrayField(String name, String format, String title, String description,
22-
URI rdfType, Map constraints, Map options){
22+
URI rdfType, Map<String, Object> constraints, Map<String, Object> options){
2323
super(name, FIELD_TYPE_ARRAY, format, title, description, rdfType, constraints, options);
2424
}
2525

src/main/java/io/frictionlessdata/tableschema/field/BooleanField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public BooleanField(String name) {
2727
}
2828

2929
public BooleanField(String name, String format, String title, String description,
30-
URI rdfType, Map constraints, Map options){
30+
URI rdfType, Map<String, Object> constraints, Map<String, Object> options){
3131
super(name, FIELD_TYPE_BOOLEAN, format, title, description, rdfType, constraints, options);
3232
}
3333

src/main/java/io/frictionlessdata/tableschema/field/DateField.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
import io.frictionlessdata.tableschema.exception.TypeInferringException;
66

77
import java.net.URI;
8-
import java.text.SimpleDateFormat;
98
import java.time.LocalDate;
109
import java.time.format.DateTimeFormatter;
1110
import java.time.format.DateTimeParseException;
12-
import java.time.temporal.ChronoField;
1311
import java.time.temporal.TemporalAccessor;
14-
import java.time.temporal.TemporalField;
1512
import java.util.Map;
1613
import java.util.regex.Matcher;
1714
import java.util.regex.Pattern;
@@ -29,7 +26,7 @@ public DateField(String name) {
2926
}
3027

3128
public DateField(String name, String format, String title, String description,
32-
URI rdfType, Map constraints, Map options){
29+
URI rdfType, Map<String, Object> constraints, Map<String, Object> options){
3330
super(name, FIELD_TYPE_DATE, format, title, description, rdfType, constraints, options);
3431
}
3532

0 commit comments

Comments
 (0)