xdean.csv.CsvValueFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-csv Show documentation
Show all versions of fluent-csv Show documentation
Fluent and Flexible CSV Framework
The newest version!
package xdean.csv;
import java.util.function.Function;
import xdean.jex.util.lang.PrimitiveTypeUtil;
/**
* CSV value formatter.
*
* @apiNote The formatter must be stateless.
* @author Dean Xu ([email protected])
* @param the value type.
*/
public interface CsvValueFormatter {
/**
* Format the value to text.
*/
String format(T value);
/**
* The value type.
*/
Class type();
/**
* Get default {@link CsvValueFormatter} by using {@link Object#toString()}
*/
static CsvValueFormatter toString(Class clz) {
return create(clz, t -> t.toString());
}
/**
* Create {@code CsvValueFormatter} from the {@code Function}
*/
@SuppressWarnings("unchecked")
static CsvValueFormatter create(Class clz, Function function) {
Class c = (Class) PrimitiveTypeUtil.toWrapper(clz);
return new CsvValueFormatter() {
@Override
public String format(T value) {
return function.apply(value);
}
@Override
public Class type() {
return c;
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy