Skip to content

Commit faeadef

Browse files
renamed a method
1 parent 1c37d9f commit faeadef

4 files changed

Lines changed: 57 additions & 22 deletions

File tree

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.5.2-SNAPSHOT</version>
6+
<version>0.6.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: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,14 @@ public BeanIterator<?> iterator(Class<?> beanType, boolean relations){
214214
/**
215215
* This is the simplest case to read data from a Table referencing a file or URL.
216216
*
217-
* Each row of the table will be returned as an Object array. Values in each column
218-
* are parsed and converted ("cast) to Java objects on a best guess approach.
219-
* @return Interator returning table rows as Object Arrays
217+
* If a Schema is set on a table, each row will be returned as an Object array. Values in each column
218+
* are parsed and converted ("cast") to Java objects based on the Field definitions of the Schema. If no Schema is
219+
* present, rows will always return string arrays
220+
*
221+
* @return Iterator returning table rows as Object/String Arrays
220222
*/
221223
public Iterator<Object[]> iterator() {
222-
return new TableIterator<>(this, false, false, true, false);
224+
return new TableIterator<>(this, false, false, true, false);
223225
}
224226

225227
/**
@@ -237,8 +239,14 @@ public Iterator<Object[]> iterator() {
237239
* < li> Resolving references to other data sources (parameter `relations` = true)</li>
238240
* </ul>
239241
*
240-
* Without a Schema, values in each column are parsed and converted ("cast) to Java objects on a
241-
* best guess approach. If a Schema is set on a table, the Field definitions will be used for parsing
242+
* The following rules apply:
243+
* <ul>
244+
* <li>if no Schema is present, rows will always return string arrays, not objects, as if `cast` was always off</li>
245+
* <li>if `extended` is true, then `cast` is also true, but `keyed` is false</li>
246+
* <li>if `keyed` is true, then `cast` is also true, but `extended` is false</li>
247+
* </ul>
248+
*
249+
* If a Schema is set on a table, the Field definitions will be used for parsing
242250
* data values to objects.
243251
*
244252
* @return Interator returning table rows as Objects, either Arrays or Maps
@@ -249,7 +257,7 @@ public Iterator<Object> iterator(boolean keyed, boolean extended, boolean cast,
249257

250258
/**
251259
* This method creates an Iterator that will return table rows as String arrays.
252-
* It therefore disregards the Schema set on the table.
260+
* It therefore disregards the Schema set on the table. It does not follow relations.
253261
*
254262
* @return Iterator that returns rows as string arrays.
255263
*/
@@ -259,7 +267,7 @@ public Iterator<String[]> stringArrayIterator() {
259267

260268
/**
261269
* This method creates an Iterator that will return table rows as String arrays.
262-
* It therefore disregards the Schema set on the table.
270+
* It therefore disregards the Schema set on the table. It can be configured to follow relations.
263271
*
264272
* @param relations Whether references to other data sources get resolved
265273
* @return Iterator that returns rows as string arrays.
@@ -270,7 +278,7 @@ public Iterator<String[]> stringArrayIterator(boolean relations) {
270278

271279
/**
272280
* This method creates an Iterator that will return table rows as a Map&lt;String,Object&gt;
273-
* where key is the header name, and val is the data converted to Java objects
281+
* where key is the header name, and val is the data converted to Java objects. It does not follow relations.
274282
*
275283
* @return Iterator that returns rows as Maps.
276284
*/
@@ -280,12 +288,13 @@ public Iterator<Map<String, Object>> mappingIterator() {
280288

281289
/**
282290
* This method creates an Iterator that will return table rows as a Map&lt;String,Object&gt;
283-
* where key is the header name, and val is the data converted to Java objects
291+
* where key is the header name, and val is the data converted to Java objects.
292+
* It can be configured to follow relations
284293
*
285294
* @param relations Whether references to other data sources get resolved
286295
* @return Iterator that returns rows as Maps.
287296
*/
288-
public Iterator<Map<String, Object>> keyedIterator(boolean extended, boolean cast, boolean relations){
297+
public Iterator<Map<String, Object>> mappingIterator(boolean extended, boolean cast, boolean relations){
289298
return new TableIterator<>(this, true, extended, cast, relations);
290299
}
291300

src/test/java/io/frictionlessdata/tableschema/iterator/TableIteratorTest.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
import java.time.Duration;
1919
import java.time.LocalDate;
2020
import java.time.format.DateTimeFormatter;
21-
import java.util.ArrayList;
22-
import java.util.Iterator;
23-
import java.util.List;
24-
import java.util.Map;
21+
import java.util.*;
2522

2623
import static io.frictionlessdata.tableschema.TestHelper.getResourceFile;
2724
import static io.frictionlessdata.tableschema.TestHelper.getTestDataDirectory;
@@ -79,6 +76,35 @@ void hasNext() throws Exception {
7976
Assertions.assertFalse(Table.fromSource("").iterator().hasNext());
8077
}
8178

79+
@Test
80+
@DisplayName("Test non-casting Iterator")
81+
void nonCasting() {
82+
validPopulationTable.iterator(false, false, false, false).forEachRemaining((r) -> {
83+
Assertions.assertTrue(r instanceof Object[]);
84+
Arrays.stream(((Object[])r)).forEach((c)->{
85+
Assertions.assertTrue(c instanceof String);
86+
});
87+
});
88+
89+
}
90+
91+
92+
@Test
93+
@DisplayName("Test no Schema, but casting Iterator")
94+
void noSchema() throws Exception {
95+
File testDataDir = getTestDataDirectory();
96+
File file = new File("data/population.csv");
97+
Table populationTable
98+
= Table.fromSource(file, testDataDir, null, TableDataSource.getDefaultCsvFormat());
99+
populationTable.iterator(false, false, true, false).forEachRemaining((r) -> {
100+
Assertions.assertTrue(r instanceof Object[]);
101+
Arrays.stream(((Object[])r)).forEach((c)->{
102+
Assertions.assertTrue(c instanceof String);
103+
});
104+
});
105+
106+
}
107+
82108
@Test
83109
@DisplayName("Test Iterator throws on remove")
84110
void remove() {
@@ -89,12 +115,12 @@ void remove() {
89115

90116
@Test
91117
@DisplayName("Test casting Iterator")
92-
void testNextCast() throws Exception {
118+
void testNextCast() {
93119
Iterator<Map<String, Object>> iter
94-
= nullValuesPopulationTable.keyedIterator( false, true, false);
95-
Map<String, Object> obj = (Map<String, Object>)iter.next();
120+
= nullValuesPopulationTable.mappingIterator( false, true, false);
121+
Map<String, Object> obj = iter.next();
96122
Assertions.assertNull(obj.get("year"));
97-
obj = (Map<String, Object>)iter.next();
123+
obj = iter.next();
98124
Assertions.assertNull(obj.get("year"));
99125
}
100126

@@ -248,7 +274,7 @@ void testStringObjectMapIterateDataFromJSONFormatAlternateSchema() throws Except
248274
List<String[]> expectedData = this.getExpectedAlternatePopulationData();
249275

250276
// Get Iterator.
251-
Iterator<Map<String, Object>> iter = table.keyedIterator(false, true, false);
277+
Iterator<Map<String, Object>> iter = table.mappingIterator(false, true, false);
252278
int expectedDataIndex = 0;
253279

254280
// Assert data.

src/test/java/io/frictionlessdata/tableschema/table_tests/TableOtherTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public void testIterateCastKeyedData() throws Exception{
233233
File file = new File("data/employee_data.csv");
234234
Table employeeTable = Table.fromSource(file, testDataDir, employeeTableSchema, TableDataSource.getDefaultCsvFormat());
235235

236-
Iterator<Map<String, Object>> iter = employeeTable.keyedIterator(false, false, false);
236+
Iterator<Map<String, Object>> iter = employeeTable.mappingIterator(false, false, false);
237237

238238
while(iter.hasNext()){
239239
Map row = iter.next();

0 commit comments

Comments
 (0)