# csv-parser

A small, fluent CSV parser for Java that maps rows to either:

- raw `List<List<String>>` rows,
- a single-column `List<String>`, or
- arbitrary user POJOs whose fields are annotated with `@CSVColumn`.

Built on top of [Apache Commons CSV](https://commons.apache.org/proper/commons-csv/).

## Features

- Fluent configuration: delimiter, header handling, column remapping
- Excel-style alphabetic column references (`"A"`, `"B"`, `"AA"`, ...)
- Pluggable mapper chain (date conversion + a catch-all `String`-constructor mapper)
- Strict date parsing via `@CSVDate(pattern = "...")`
- UTF-8 input with automatic BOM stripping (Excel-exported CSVs work out of the box)
- All errors surfaced as a single unchecked `CsvParseException` with the original cause preserved

## Requirements

- Java 8 or newer
- Maven 3.x

## Installation

The project is published to the Ljunggren Nexus repository. Add the dependency:

```xml
<dependency>
    <groupId>io.ljunggren</groupId>
    <artifactId>csv-parser</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
```

## Quick start

### Parse to a typed POJO

Annotate the POJO fields with `@CSVColumn`. The value is the column reference
in the CSV, using either Excel-style letters (`"A"`, `"B"`, …) or a numeric
zero-based index.

```java
import io.ljunggren.csv.parser.annotation.CSVColumn;

public class Person {
    @CSVColumn("A") private Long id;
    @CSVColumn("B") private String firstName;
    @CSVColumn("C") private String lastName;
    @CSVColumn("D") private int age;
    // getters & setters
}
```

```java
Parser parser = new Parser().firstRowIsHeader();
List<Person> people = parser.parse(file, Person.class);
```

### Parse raw rows

```java
List<List<String>> rows = new Parser().parse(file);
```

### Parse the first column as `List<String>`

```java
List<String> ids = new Parser().parse(file, String.class);
```

### Read just the headers

```java
List<String> headers = new Parser().firstRowIsHeader().parseHeaders(file);
```

## Configuration

```java
Parser parser = new Parser()
        .delimiter('|')          // default is ','
        .firstRowIsHeader()      // skip the first row
        .columnMap(remapping);   // optional column remapping
```

`columnMap` translates the column reference declared on the POJO to the actual
column reference in the file. The map is defensively copied; mutating the
source map after passing it to `columnMap(...)` has no effect on the parser.
The getter, `getColumnMap()`, returns an unmodifiable view.

### Column remapping example

CSV (columns out of order):

| A | B | C | D | E |
|---|---|---|---|---|
| US | Indiana | English | Alex | 1 |

POJO declares `id` at column `"A"`, `name` at column `"B"`, `state` at `"D"`.
To make the POJO line up with the file, supply:

```java
Map<String, String> remap = new HashMap<>();
remap.put("A", "E");   // POJO "A" reads file column E
remap.put("B", "D");   // POJO "B" reads file column D
remap.put("D", "B");   // POJO "D" reads file column B

new Parser().firstRowIsHeader().columnMap(remap).parse(file, Person.class);
```

## Date fields

Annotate `Date` fields with `@CSVDate(pattern = "...")`. The pattern is
interpreted by `java.text.SimpleDateFormat` in **strict** (non-lenient) mode,
so malformed values fail fast.

```java
import io.ljunggren.csv.parser.annotation.CSVDate;

public class Order {
    @CSVColumn("A") private Long id;
    @CSVColumn("B") @CSVDate(pattern = "MM-dd-yyyy") private Date placedOn;
}
```

## Type conversion

The mapper chain runs in this order:

1. **`DateMapper`** — handles fields annotated with `@CSVDate`.
2. **`CatchAllMapper`** — for everything else, invokes the field type's
   `T(String)` constructor (primitive types are converted via their wrapper).

Any field type that exposes a single-argument `String` constructor is
supported out of the box: `String`, `Integer`, `Long`, `Double`, `BigDecimal`,
`Boolean`, etc.

If you need to support a type without a `String` constructor (e.g. an enum,
`LocalDate`, `UUID`), implement your own `MapperChain` subclass — see the
`io.ljunggren.csv.parser.mapper` package.

## Error handling

All parsing failures are wrapped in `io.ljunggren.csv.parser.exception.CsvParseException`,
which is an unchecked exception. The original cause (e.g. `IOException`,
`ParseException`, `NoSuchMethodException`) is always preserved.

```java
try {
    new Parser().firstRowIsHeader().parse(file, Person.class);
} catch (CsvParseException e) {
    log.error("could not parse {}: {}", file, e.getMessage(), e);
}
```

Argument validation: passing `null` for the file or class throws
`NullPointerException` immediately.

## Thread safety

`Parser` instances are **not** thread-safe — each thread should construct its
own instance. A single instance can be reused sequentially across multiple
`parse(...)` calls.
