diff options
Diffstat (limited to 'src')
5 files changed, 84 insertions, 4 deletions
diff --git a/src/main/java/it/alessandroiezzi/csv/CSVBuilder.java b/src/main/java/it/alessandroiezzi/csv/CSVBuilder.java index 5111363..982d66e 100644 --- a/src/main/java/it/alessandroiezzi/csv/CSVBuilder.java +++ b/src/main/java/it/alessandroiezzi/csv/CSVBuilder.java @@ -53,7 +53,7 @@ public class CSVBuilder { public CSVBuilder append(String...row) { if (row.length != header.length) - throw new IllegalArgumentException("The number of columns does not match the header"); + throw new CSVHeaderNoMatchRowFieldsException(); rows.add(row); return this; diff --git a/src/main/java/it/alessandroiezzi/csv/CSVEntityNoDefaultConstructorException.java b/src/main/java/it/alessandroiezzi/csv/CSVEntityNoDefaultConstructorException.java new file mode 100644 index 0000000..f6d0a23 --- /dev/null +++ b/src/main/java/it/alessandroiezzi/csv/CSVEntityNoDefaultConstructorException.java @@ -0,0 +1,26 @@ +/* + * Simple and faster CSV parser/writer library. + * + * Copyright (C) 2022-2023 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * csv-utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * csv-utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with csv-utils. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.csv; + +public class CSVEntityNoDefaultConstructorException extends CSVException { + public CSVEntityNoDefaultConstructorException(Class<?> cls) { + super("You need to define a public default constructor on " + cls.getName()); + } +} diff --git a/src/main/java/it/alessandroiezzi/csv/CSVException.java b/src/main/java/it/alessandroiezzi/csv/CSVException.java new file mode 100644 index 0000000..90413a0 --- /dev/null +++ b/src/main/java/it/alessandroiezzi/csv/CSVException.java @@ -0,0 +1,28 @@ +/* + * Simple and faster CSV parser/writer library. + * + * Copyright (C) 2022-2023 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * csv-utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * csv-utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with csv-utils. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.csv; + +public class CSVException extends RuntimeException { + public CSVException() { } + + public CSVException(String message) { + super(message); + } +} diff --git a/src/main/java/it/alessandroiezzi/csv/CSVHeaderNoMatchRowFieldsException.java b/src/main/java/it/alessandroiezzi/csv/CSVHeaderNoMatchRowFieldsException.java new file mode 100644 index 0000000..fe9a7cf --- /dev/null +++ b/src/main/java/it/alessandroiezzi/csv/CSVHeaderNoMatchRowFieldsException.java @@ -0,0 +1,26 @@ +/* + * Simple and faster CSV parser/writer library. + * + * Copyright (C) 2022-2023 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * csv-utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * csv-utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with csv-utils. If not, see <https://www.gnu.org/licenses/>. + */ + +package it.alessandroiezzi.csv; + +public class CSVHeaderNoMatchRowFieldsException extends CSVException { + public CSVHeaderNoMatchRowFieldsException() { + super("The number of fields defined on header doesn't match with the number of row fields"); + } +} diff --git a/src/main/java/it/alessandroiezzi/csv/CSVParser.java b/src/main/java/it/alessandroiezzi/csv/CSVParser.java index f89b38b..45fd050 100644 --- a/src/main/java/it/alessandroiezzi/csv/CSVParser.java +++ b/src/main/java/it/alessandroiezzi/csv/CSVParser.java @@ -41,7 +41,7 @@ public class CSVParser<T> { if (constructor.getParameterCount() == 0) return; } - throw new IllegalArgumentException("You need to define a constructor without parameters on " + cls.getName()); + throw new CSVEntityNoDefaultConstructorException(cls); } private T newInstance() { @@ -90,7 +90,7 @@ public class CSVParser<T> { String[] values = line.replace("\"", "").split(separator, -1); if (values.length != header.length) { - throw new RuntimeException("Il numero di campi definiti nella testata non corrisponde con le colonne dei dati"); + throw new CSVHeaderNoMatchRowFieldsException(); } T data = newInstance(); @@ -131,7 +131,7 @@ public class CSVParser<T> { Map<String, String> data = new HashMap<>(); if (values.length != header.length) { - throw new RuntimeException("Il numero di campi definiti nella testata non corrisponde con le colonne dei dati"); + throw new CSVHeaderNoMatchRowFieldsException(); } for (int j = 0; j < header.length; j++) { data.put(header[j], values[j]); |