package io.ljunggren.csv.parser.mapper;
import java.lang.reflect.Field;
/**
* Base class for the chain-of-responsibility used to convert raw CSV string
* values into typed objects assignable to a target {@link Field}.
*
* <p>Concrete subclasses should attempt the conversion they are responsible
* for and otherwise delegate to {@link #next(Field, String)} which safely
* forwards to the {@link #nextChain} when present.</p>
*/
public abstract class MapperChain {
protected MapperChain nextChain;
/**
* Append the given mapper as the next link in the chain and return it,
* allowing fluent composition such as
* {@code new DateMapper().nextChain(new CatchAllMapper())}.
*
* @param nextChain the next mapper to delegate to
* @return the supplied {@code nextChain} so callers can continue chaining
*/
public MapperChain nextChain(MapperChain nextChain) {
this.nextChain = nextChain;
return this;
}
/**
* Convert {@code value} into an object assignable to {@code field}.
*
* @param field the target field that will receive the converted value
* @param value the raw CSV cell value
* @return the converted value, or {@code null} if no mapper handled it
* @throws Exception if the conversion fails
*/
public abstract Object map(Field field, String value) throws Exception;
/**
* Delegate to the next mapper in the chain if one is configured.
*
* @param field the target field
* @param value the raw value
* @return the value produced by the next mapper, or {@code null} if there
* is no next mapper
* @throws Exception if the next mapper fails
*/
protected Object next(Field field, String value) throws Exception {
if (nextChain == null) {
return null;
}
return nextChain.map(field, value);
}
}