Commit a1ce3fa4 authored by R.W.Majeed's avatar R.W.Majeed
Browse files

abstract table with generics

parent df24a214
......@@ -3,14 +3,14 @@ package de.sekmi.histream.etl;
import java.io.IOException;
import java.util.function.Supplier;
import de.sekmi.histream.etl.config.PatientTable;
import de.sekmi.histream.etl.config.Table;
public class PatientStream implements Supplier<PatientRow>, AutoCloseable{
public class RecordSupplier<R> implements Supplier<R>, AutoCloseable{
RowSupplier rows;
PatientTable table;
Table<R> table;
ColumnMap map;
public PatientStream(RowSupplier rows, PatientTable table) throws IOException{
public RecordSupplier(RowSupplier rows, Table<R> table) throws IOException{
this.rows = rows;
this.table = table;
this.map = table.getColumnMap(rows.getHeaders());
......@@ -22,24 +22,20 @@ public class PatientStream implements Supplier<PatientRow>, AutoCloseable{
}
@Override
public PatientRow get() {
public R get() {
Object[] row = rows.get();
if( row == null ){
// no more rows
return null;
}
PatientRow p;
R p;
try {
p = table.fillPatient(map, row);
p = table.fillRecord(map, row);
} catch (ParseException e) {
throw new UncheckedParseException(e);
}
return p;
}
}
package de.sekmi.histream.etl;
public class WideRow {
}
package de.sekmi.histream.etl.config;
import java.io.IOException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
......@@ -11,7 +10,6 @@ import de.sekmi.histream.DateTimeAccuracy;
import de.sekmi.histream.etl.ColumnMap;
import de.sekmi.histream.etl.ParseException;
import de.sekmi.histream.etl.PatientRow;
import de.sekmi.histream.etl.PatientStream;
/**
* Patient table. Contains patient id and other identifying information.
......@@ -19,7 +17,7 @@ import de.sekmi.histream.etl.PatientStream;
* @author marap1
*
*/
public class PatientTable extends Table implements WideInterface{
public class PatientTable extends Table<PatientRow> implements WideInterface{
@XmlElement
IDAT idat;
......@@ -61,7 +59,9 @@ public class PatientTable extends Table implements WideInterface{
return map;
}
public PatientRow fillPatient(ColumnMap map, Object[] row) throws ParseException{
@Override
public PatientRow fillRecord(ColumnMap map, Object[] row) throws ParseException {
PatientRow patient = new PatientRow();
patient.setId(idat.patientId.valueOf(map, row).toString());
patient.setNames((String)idat.firstname.valueOf(map, row), (String)idat.surname.valueOf(map, row));
......@@ -71,8 +71,4 @@ public class PatientTable extends Table implements WideInterface{
return patient;
}
public PatientStream open() throws IOException{
return new PatientStream(source.rows(), this);
}
}
package de.sekmi.histream.etl.config;
import java.io.IOException;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import de.sekmi.histream.etl.ColumnMap;
import de.sekmi.histream.etl.ParseException;
import de.sekmi.histream.etl.RecordSupplier;
@XmlTransient
public abstract class Table {
public abstract class Table<T> {
@XmlElement(required=true)
TableSource source;
......@@ -33,4 +37,11 @@ public abstract class Table {
map.registerColumn(m.unit);
}
}
public abstract T fillRecord(ColumnMap map, Object[] row) throws ParseException;
public RecordSupplier<T> open() throws IOException{
return new RecordSupplier<>(source.rows(), this);
}
}
......@@ -7,8 +7,10 @@ import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlType;
import de.sekmi.histream.etl.ColumnMap;
import de.sekmi.histream.etl.ParseException;
import de.sekmi.histream.etl.VisitRow;
public class VisitTable extends Table implements WideInterface{
public class VisitTable extends Table<VisitRow> implements WideInterface{
@XmlElement
IDAT idat;
......@@ -38,4 +40,9 @@ public class VisitTable extends Table implements WideInterface{
}
return map;
}
@Override
public VisitRow fillRecord(ColumnMap map, Object[] row) throws ParseException {
// TODO Auto-generated method stub
return null;
}
}
......@@ -4,8 +4,10 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import de.sekmi.histream.etl.ColumnMap;
import de.sekmi.histream.etl.ParseException;
import de.sekmi.histream.etl.WideRow;
public class WideTable extends Table {
public class WideTable extends Table<WideRow> {
@XmlElement
DataTableIdat idat;
......@@ -24,4 +26,10 @@ public class WideTable extends Table {
}
return map;
}
@Override
public WideRow fillRecord(ColumnMap map, Object[] row) throws ParseException {
// TODO Auto-generated method stub
return null;
}
}
......@@ -8,7 +8,7 @@ import javax.xml.bind.JAXB;
import org.junit.Test;
import de.sekmi.histream.etl.PatientRow;
import de.sekmi.histream.etl.PatientStream;
import de.sekmi.histream.etl.RecordSupplier;
import org.junit.Assert;
public class TestReadTables {
......@@ -19,7 +19,7 @@ public class TestReadTables {
try( InputStream in = getClass().getResourceAsStream("/test-1-datasource.xml") ){
ds = JAXB.unmarshal(in, DataSource.class);
}
try( PatientStream s = ds.patientTable.open() ){
try( RecordSupplier<PatientRow> s = ds.patientTable.open() ){
PatientRow r = s.get();
Assert.assertEquals("1", r.getId());
System.out.println(r.getBirthDate());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment