Package com.opencsv.bean
Class ConverterNumber
- java.lang.Object
-
- com.opencsv.bean.AbstractCsvConverter
-
- com.opencsv.bean.ConverterNumber
-
- All Implemented Interfaces:
CsvConverter
public class ConverterNumber extends AbstractCsvConverter
This converter class is used in combination withCsvNumber, that is, when number inputs and outputs should be formatted.- Since:
- 4.2
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.function.UnaryOperator<java.lang.Number>readConversionFunctionprivate java.text.DecimalFormatreadFormatterprivate java.text.DecimalFormatwriteFormatter-
Fields inherited from class com.opencsv.bean.AbstractCsvConverter
errorLocale, locale, type, writeLocale
-
-
Constructor Summary
Constructors Constructor Description ConverterNumber(java.lang.Class<?> type, java.lang.String locale, java.lang.String writeLocale, java.util.Locale errorLocale, java.lang.String readFormat, java.lang.String writeFormat)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.ObjectconvertToRead(java.lang.String value)Method for converting from a string to the proper data type of the destination field.java.lang.StringconvertToWrite(java.lang.Object value)Formats the number in question according to the pattern that has been provided.private java.text.DecimalFormatcreateDecimalFormat(java.lang.String format, java.util.Locale locale)-
Methods inherited from class com.opencsv.bean.AbstractCsvConverter
setErrorLocale, setLocale, setType, setWriteLocale
-
-
-
-
Constructor Detail
-
ConverterNumber
public ConverterNumber(java.lang.Class<?> type, java.lang.String locale, java.lang.String writeLocale, java.util.Locale errorLocale, java.lang.String readFormat, java.lang.String writeFormat) throws CsvBadConverterException- Parameters:
type- The class of the type of the data being processedlocale- If not null or empty, specifies the locale used for converting locale-specific data types for readingwriteLocale- If not null or empty, specifies the locale used for converting locale-specific data types for writingerrorLocale- The locale to use for error messagesreadFormat- The string to use for parsing the number.writeFormat- The string to use for formatting the number.- Throws:
CsvBadConverterException- If the information given to initialize the converter are inconsistent (e.g. the annotationCsvNumberhas been applied to a non-Numbertype.- See Also:
CsvNumber.value()
-
-
Method Detail
-
createDecimalFormat
private java.text.DecimalFormat createDecimalFormat(java.lang.String format, java.util.Locale locale)
-
convertToRead
public java.lang.Object convertToRead(java.lang.String value) throws CsvDataTypeMismatchExceptionDescription copied from interface:CsvConverterMethod for converting from a string to the proper data type of the destination field.- Parameters:
value- The string from the selected field of the CSV file. If the field is marked as required in the annotation, this value is guaranteed not to benull, empty or blank according toStringUtils.isBlank(java.lang.CharSequence)- Returns:
- An
Objectrepresenting the input data converted into the proper type - Throws:
CsvDataTypeMismatchException- If the input string cannot be converted into the proper type
-
convertToWrite
public java.lang.String convertToWrite(java.lang.Object value)
Formats the number in question according to the pattern that has been provided.- Specified by:
convertToWritein interfaceCsvConverter- Overrides:
convertToWritein classAbstractCsvConverter- Parameters:
value- The contents of the field currently being processed from the bean to be written. Can benullif the field is not marked as required.- Returns:
- A string representation of the value of the field in question in
the bean passed in, or an empty string if
valueisnull
-
-