From a3c2a2ed8f65c5463609f26c2835ce8f7baa5e69 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 10 Jul 2015 12:37:24 +0200 Subject: [PATCH] [Minor] moved CsvData and CsvRow to their own classes --- .../java/ch/eitchnet/utils/csv/CsvData.java | 52 ++++++++++++++ .../java/ch/eitchnet/utils/csv/CsvParser.java | 68 ------------------- .../java/ch/eitchnet/utils/csv/CsvRow.java | 53 +++++++++++++++ .../ch/eitchnet/utils/csv/CsvParserTest.java | 3 - 4 files changed, 105 insertions(+), 71 deletions(-) create mode 100644 src/main/java/ch/eitchnet/utils/csv/CsvData.java create mode 100644 src/main/java/ch/eitchnet/utils/csv/CsvRow.java diff --git a/src/main/java/ch/eitchnet/utils/csv/CsvData.java b/src/main/java/ch/eitchnet/utils/csv/CsvData.java new file mode 100644 index 000000000..86b7053f0 --- /dev/null +++ b/src/main/java/ch/eitchnet/utils/csv/CsvData.java @@ -0,0 +1,52 @@ +/* + * Copyright 2015 Robert von Burg + * + * 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 headers; + private List 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 getHeaders() { + return this.headers; + } + + public List getRows() { + return this.rows; + } +} \ No newline at end of file diff --git a/src/main/java/ch/eitchnet/utils/csv/CsvParser.java b/src/main/java/ch/eitchnet/utils/csv/CsvParser.java index 7398b91b2..df088f6be 100644 --- a/src/main/java/ch/eitchnet/utils/csv/CsvParser.java +++ b/src/main/java/ch/eitchnet/utils/csv/CsvParser.java @@ -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 @@ -85,68 +81,4 @@ public class CsvParser { return data; } - - public class CsvData { - private List headers; - private List 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 getHeaders() { - return this.headers; - } - - public List getRows() { - return this.rows; - } - } - - public class CsvRow { - private int index; - private List 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 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 getValues() { - return this.values; - } - } } diff --git a/src/main/java/ch/eitchnet/utils/csv/CsvRow.java b/src/main/java/ch/eitchnet/utils/csv/CsvRow.java new file mode 100644 index 000000000..e0939ac4f --- /dev/null +++ b/src/main/java/ch/eitchnet/utils/csv/CsvRow.java @@ -0,0 +1,53 @@ +/* + * Copyright 2015 Robert von Burg + * + * 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 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 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 getValues() { + return this.values; + } +} \ No newline at end of file diff --git a/src/test/java/ch/eitchnet/utils/csv/CsvParserTest.java b/src/test/java/ch/eitchnet/utils/csv/CsvParserTest.java index 2a208c512..60d60efb6 100644 --- a/src/test/java/ch/eitchnet/utils/csv/CsvParserTest.java +++ b/src/test/java/ch/eitchnet/utils/csv/CsvParserTest.java @@ -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 */