[Minor] moved CsvData and CsvRow to their own classes

This commit is contained in:
Robert von Burg 2015-07-10 12:37:24 +02:00
parent 35bbb04d89
commit a3c2a2ed8f
4 changed files with 105 additions and 71 deletions

View File

@ -0,0 +1,52 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.eitchnet.utils.csv;
import java.util.ArrayList;
import java.util.List;
public class CsvData {
private List<String> headers;
private List<CsvRow> rows;
public CsvData() {
this.headers = new ArrayList<>();
this.rows = new ArrayList<>();
}
public String getHeaderAtIndex(int column) {
if (this.headers.size() < column + 1) {
throw new IllegalArgumentException("No header exists at column index " + column);
}
return this.headers.get(column);
}
public void addHeader(String header) {
this.headers.add(header);
}
public void addRow(CsvRow row) {
this.rows.add(row);
}
public List<String> getHeaders() {
return this.headers;
}
public List<CsvRow> getRows() {
return this.rows;
}
}

View File

@ -18,13 +18,9 @@ package ch.eitchnet.utils.csv;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.xml.XmlKeyValue;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -85,68 +81,4 @@ public class CsvParser {
return data;
}
public class CsvData {
private List<String> headers;
private List<CsvRow> rows;
public CsvData() {
this.headers = new ArrayList<>();
this.rows = new ArrayList<>();
}
public String getHeaderAtIndex(int column) {
if (this.headers.size() < column + 1) {
throw new IllegalArgumentException("No header exists at column index " + column);
}
return this.headers.get(column);
}
public void addHeader(String header) {
this.headers.add(header);
}
public void addRow(CsvRow row) {
this.rows.add(row);
}
public List<String> getHeaders() {
return this.headers;
}
public List<CsvRow> getRows() {
return this.rows;
}
}
public class CsvRow {
private int index;
private List<XmlKeyValue> values;
public CsvRow(int index) {
this.index = index;
this.values = new ArrayList<>();
}
public int getIndex() {
return this.index;
}
public void addColumnValue(String header, String value) {
this.values.add(new XmlKeyValue(header, value));
}
public String getColumnValue(String header) {
for (Iterator<XmlKeyValue> iter = this.values.iterator(); iter.hasNext();) {
XmlKeyValue next = iter.next();
if (next.getKey().equals(header))
return next.getValue();
}
throw new IllegalArgumentException("No value exists for header" + header);
}
public List<XmlKeyValue> getValues() {
return this.values;
}
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.eitchnet.utils.csv;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import ch.eitchnet.utils.xml.XmlKeyValue;
public class CsvRow {
private long index;
private List<XmlKeyValue> values;
public CsvRow(long index) {
this.index = index;
this.values = new ArrayList<>();
}
public long getIndex() {
return this.index;
}
public void addColumnValue(String header, String value) {
this.values.add(new XmlKeyValue(header, value));
}
public String getColumnValue(String header) {
for (Iterator<XmlKeyValue> iter = this.values.iterator(); iter.hasNext();) {
XmlKeyValue next = iter.next();
if (next.getKey().equals(header))
return next.getValue();
}
throw new IllegalArgumentException("No value exists for header" + header);
}
public List<XmlKeyValue> getValues() {
return this.values;
}
}

View File

@ -22,9 +22,6 @@ import java.util.Arrays;
import org.junit.Test;
import ch.eitchnet.utils.csv.CsvParser.CsvData;
import ch.eitchnet.utils.csv.CsvParser.CsvRow;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/