Compare commits

...

4 Commits

Author SHA1 Message Date
Harald Wolff aca1de8605 CSV Improvements 2018-02-22 19:05:48 +01:00
Niclas Thobaben 8287e1f64a added new CSV-interface 2018-02-12 10:41:56 +01:00
Niclas Thobaben ba08a7bfa3 implemented new basic csv api 2018-02-12 10:41:49 +01:00
Harald Wolff 7bbd7e2cbf Implement close() for serial port streams 2018-02-10 13:00:30 +01:00
4 changed files with 269 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package org.hwo.csv;
import java.util.ArrayList;
public class CSVContainer {
private ArrayList<CSVTable> tables;
private CSVTable emptyTable;
public CSVContainer(){
init();
}
private void init() {
tables = new ArrayList<>();
}
public int getTableCount() {
return this.tables.size();
}
public CSVTable getTabel(int index) {
return this.tables.get(index);
}
public void addTable(CSVTable table) {
this.tables.add(table);
}
public void removeTable(CSVTable table) {
this.tables.remove(table);
}
public void removeTable(int index) {
this.tables.remove(index);
}
public String getCSV() {
String csv = new String();
for(CSVTable t : this.tables) {
csv += t.getCSV();
csv += CSVTableRow.NULL_ROW.getCSV();
}
return csv;
}
}

View File

@ -0,0 +1,123 @@
package org.hwo.csv;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class CSVTable {
ArrayList<CSVTableRow> rows;
public CSVTable() {
this.rows = new ArrayList<>();
}
public void addRow(CSVTableRow row) {
this.rows.add(row);
}
public CSVTableRow getRow(int index) {
return this.rows.get(index);
}
public int getRowCount() {
return this.rows.size();
}
public Object[] getColumn(int column) {
return getColumn(column,0,this.rows.size());
}
public Object[] getColumn(int column,int start) {
return getColumn(column,start,this.rows.size());
}
public Object[] getColumn(int column,int start,int end) {
ArrayList<Object> ret = new ArrayList<>();
for (int n = start; n < end; n++) {
ret.add(this.rows.get(n).getValue(column));
}
return ret.toArray();
}
public <T> T[] getColumn(T[] targetType,int column) {
return getColumn(targetType, column, 0, this.rows.size());
}
public <T> T[] getColumn(T[] targetType,int column,int start) {
return getColumn(targetType, column, start, this.rows.size());
}
@SuppressWarnings("unchecked")
public <T> T[] getColumn(T[] targetType,int column,int start,int end) {
T[] targetArray = (T[])Array.newInstance(targetType.getClass().getComponentType(), end - start);
for (int n = start; n < end; n++) {
Object o = this.rows.get(n).getValue(column);
if (o != null) {
targetArray[n - start] = (T)o;
} else {
targetArray[n - start] = null;
}
}
return targetArray;
}
public int getColumnCount() {
int count = 0;
for(CSVTableRow r : this.rows) {
int rc = r.getColumnCount();
count = rc > count? rc : count;
}
return count;
}
public String getCSV() {
StringBuilder csv = new StringBuilder();
for(CSVTableRow r : this.rows) {
csv.append(r.getCSV());
}
return csv.toString();
}
public static CSVTable ParseCSV(String path) {
CSVTable ret = new CSVTable();
File f = new File(path);
List<String> str = null;
try {
str = Files.readAllLines(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
if(str == null) {
return null;
}
for(String s : str) {
//parse line
CSVTableRow row = new CSVTableRow();
String[] tokens = s.split(",");
for(String t : tokens) {
if (t.equals("")) {
row.addValue(null);
} else if(t.contains("\"")) {
row.addValue(t.substring(1,t.length() - 1));
}
else if(t.contains(".")) {
row.addValue(Float.parseFloat(t));
}
else {
row.addValue(Integer.parseInt(t));
}
}
ret.addRow(row);
}
return ret;
}
}

View File

@ -0,0 +1,93 @@
package org.hwo.csv;
import java.util.ArrayList;
public class CSVTableRow {
public static CSVTableRow NULL_ROW = new CSVTableRow("");
private ArrayList<Object> values; //first value == rowTitle
public CSVTableRow() {
init();
}
public CSVTableRow(String... values) {
init();
for (String value : values) {
addValue(value);
}
}
private void init() {
this.values = new ArrayList<>();
}
//TODO add functionality for column detection (i.e. string column id, Object value)
public void setValue(int column, Object value) {
if(column < values.size()) {
this.values.set(column, value);
}
}
public Object getValue(int column) {
if(column >= this.values.size()) {
return null;
}
return this.values.get(column);
}
public Object[] getValues() {
return this.values.toArray();
}
public int getColumnCount() {
return this.values.size();
}
public void addValue(Object value) {
this.values.add(value);
}
public void addData(Object[] data) {
for(Object o : data) {
this.values.add(o);
}
}
public String getName() {
Object n = this.values.get(0);
if(n != null && n.getClass() == String.class) {
return (String)n;
}
return null;
}
public void clear() {
this.values.clear();
}
public String getCSV() {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < values.size(); i++) {
Object obj = values.get(i);
if(obj != null) {
if(obj.getClass() == String.class) {
sb.append(String.format("\"%s\"",obj.toString()));
}
else {
sb.append(obj.toString());
}
}
else {
}
if(i < this.values.size() -1)
sb.append(',');
}
sb.append("\n");
return sb.toString();
}
}

View File

@ -320,6 +320,11 @@ public class NewSerialPort {
}
return nRead;
}
@Override
public void close() throws IOException {
NewSerialPort.this.close();
}
}
public class NewSerialPortOutputStream extends OutputStream
@ -388,6 +393,10 @@ public class NewSerialPort {
write(b, 0, b.length);
}
@Override
public void close() throws IOException {
NewSerialPort.this.close();
}
}
static public String[] getPortNames(){