Package org.refcodes.textual
Class CsvBuilderImpl
- java.lang.Object
-
- org.refcodes.textual.CsvBuilderImpl
-
- All Implemented Interfaces:
org.refcodes.mixin.DelimiterAccessor
,org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<CsvMixin>
,org.refcodes.mixin.DelimiterAccessor.DelimiterMutator
,org.refcodes.mixin.DelimiterAccessor.DelimiterProperty
,org.refcodes.mixin.TrimAccessor
,org.refcodes.mixin.TrimAccessor.TrimBuilder<CsvMixin>
,org.refcodes.mixin.TrimAccessor.TrimMutator
,org.refcodes.mixin.TrimAccessor.TrimProperty
,CsvBuilder
,CsvEscapeModeAccessor
,CsvEscapeModeAccessor.CsvEscapeModeBuilder<CsvMixin>
,CsvEscapeModeAccessor.CsvEscapeModeMutator
,CsvEscapeModeAccessor.CsvEscapeModeProperty
,CsvMixin
public class CsvBuilderImpl extends Object implements CsvBuilder
This class implements the interfaceCsvBuilderImpl
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.refcodes.textual.CsvEscapeModeAccessor
CsvEscapeModeAccessor.CsvEscapeModeBuilder<B extends CsvEscapeModeAccessor.CsvEscapeModeBuilder<?>>, CsvEscapeModeAccessor.CsvEscapeModeMutator, CsvEscapeModeAccessor.CsvEscapeModeProperty
-
Nested classes/interfaces inherited from interface org.refcodes.mixin.DelimiterAccessor
org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<B extends org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<B>>, org.refcodes.mixin.DelimiterAccessor.DelimiterMutator, org.refcodes.mixin.DelimiterAccessor.DelimiterProperty
-
-
Constructor Summary
Constructors Constructor Description CsvBuilderImpl()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearCommentPrefixes()
Clears the prefixes used to identify CSV lines to be ignored.protected static List<String>
fromSeparatedValues(String aSeparatedValues)
protected static List<String>
fromSeparatedValues(String aSeparatedValues, char aDelimiter)
protected static List<String>
fromSeparatedValues(String aRecord, char aDelimiter, CsvEscapeMode aCsvEscapeMode, boolean isTrimRecords)
From separated values.protected static List<String>
fromSplitValues(String aSplitValues)
protected static List<String>
fromSplitValues(String aSplitValues, char aDelimiter)
protected static List<String>
fromSplitValues(String aValues, String aDelimiters)
String[]
getCommentPrefixes()
Gets the prefixes used to identify CSV lines to be ignored.CsvEscapeMode
getCsvEscapeMode()
Gets the currently setCsvEscapeMode
being used.char
getDelimiter()
Collection<Object>
getFields()
Retrieves the CSV elements from the CSV elements property.String
getRecord()
Retrieves the CSV line from the CSV line property.boolean
isTrim()
void
setCommentPrefixes(String... aCommentPrefixes)
Sets the prefixes used to identify CSV lines to be ignored.void
setCsvEscapeMode(CsvEscapeMode aCsvEscapeMode)
Sets theCsvEscapeMode
to be used.void
setDelimiter(char aDelimiterChar)
void
setFields(Object... aFields)
Sets the CSV elements for the CSV elements property.void
setFields(Collection<Object> aFields)
Sets the CSV elements for the CSV elements property.void
setRecord(String aRecord)
Sets the CSV line for the CSV line property.void
setTrim(boolean isTrimRecords)
protected static String
toEscapedRegexp(char aDelimiter)
To escaped regexp.List<String>
toFields()
Creates a list ofString
elements from the CSV line being set viaCsvBuilder.setRecord(String)
orCsvBuilder.withRecord(String)
.String
toRecord()
Creates a CSV line from the elements being set viaCsvBuilder.setFields(String[])
orCsvBuilder.withFields(String[])
.String
toRecord(Object... aFields)
Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method).String
toRecord(String... aFields)
Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method).String
toRecord(Collection<Object> aFields)
Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method).protected static String
toSeparatedValue(Object aValue)
Escapes a single value according to the rules of CSV like separated values with the accordingDelimiter.CSV
delimiter.protected static String
toSeparatedValue(Object aValue, char aDelimiter)
Escapes a single value according to the rules of CSV like separated values.protected static String
toSeparatedValues(Object[] aArray)
protected static String
toSeparatedValues(Object[] aArray, char aDelimiter)
Takes the array's String representation and creates a singleString
with the provided delimiter.protected static String
toSeparatedValues(Collection<?> aCollection)
Takes the collection and creates a singleString
with the accordingDelimiter.CSV
delimiter.protected static String
toSeparatedValues(Collection<?> aCollection, char aDelimiter)
Takes the collection and creates a singleString
with the provided delimiter.protected static String
toSeparatedValues(Collection<Object> aFields, char aDelimiter, CsvEscapeMode aCsvEscapeMode)
To separated values.protected static String
toSplitValue(Object aValue, char aDelimiter)
Creates theString
representation of a single split value not applying any escaping.protected static String
toSplitValues(Object[] aArray)
protected static String
toSplitValues(Object[] aArray, char aDelimiter)
Takes the array's String representation and creates a singleString
with the provided delimiter.protected static String
toSplitValues(Collection<?> aCollection)
Takes the collection and creates a singleString
with the accordingDelimiter.CSV
delimiter.protected static String
toSplitValues(Collection<?> aCollection, char aDelimiter)
Takes the collection and creates a singleString
with the provided delimiter.String
toString()
TheString
being build by the builder upon the settings of the attributes.String[]
toStrings()
Creates an array ofString
s from the CSV line being set viaCsvBuilder.setRecord(String)
orCsvBuilder.withRecord(String)
.String[]
toStrings(String aRecord)
Convenience method for theCsvBuilder.toFields(String)
method.-
Methods inherited from interface org.refcodes.textual.CsvBuilder
build, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, setFields, toFields, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toRecord, toString, toString, toString, withCommentPrefixes, withCsvEscapeMode, withDelimiter, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withFields, withRecord, withTrim
-
-
-
-
Method Detail
-
setTrim
public void setTrim(boolean isTrimRecords)
- Specified by:
setTrim
in interfaceorg.refcodes.mixin.TrimAccessor.TrimMutator
-
isTrim
public boolean isTrim()
- Specified by:
isTrim
in interfaceorg.refcodes.mixin.TrimAccessor
-
getCsvEscapeMode
public CsvEscapeMode getCsvEscapeMode()
Gets the currently setCsvEscapeMode
being used.- Specified by:
getCsvEscapeMode
in interfaceCsvEscapeModeAccessor
- Returns:
- The currently configured
CsvEscapeMode
s.
-
setCsvEscapeMode
public void setCsvEscapeMode(CsvEscapeMode aCsvEscapeMode)
Sets theCsvEscapeMode
to be used.- Specified by:
setCsvEscapeMode
in interfaceCsvEscapeModeAccessor.CsvEscapeModeMutator
- Parameters:
aCsvEscapeMode
- TheCsvEscapeMode
to be stored by theCsvEscapeMode
property.
-
setRecord
public void setRecord(String aRecord)
Sets the CSV line for the CSV line property.- Specified by:
setRecord
in interfaceCsvBuilder
- Parameters:
aRecord
- The CSV line to be stored by the CSV line property.
-
getRecord
public String getRecord()
Retrieves the CSV line from the CSV line property.- Specified by:
getRecord
in interfaceCsvBuilder
- Returns:
- The CSV line stored by the CSV line property.
-
setFields
public void setFields(Object... aFields)
Sets the CSV elements for the CSV elements property.- Specified by:
setFields
in interfaceCsvBuilder
- Parameters:
aFields
- The CSV elements to be stored by the CSV elements property.
-
getFields
public Collection<Object> getFields()
Retrieves the CSV elements from the CSV elements property.- Specified by:
getFields
in interfaceCsvBuilder
- Returns:
- The CSV elements stored by the CSV elements property.
-
setFields
public void setFields(Collection<Object> aFields)
Sets the CSV elements for the CSV elements property.- Specified by:
setFields
in interfaceCsvBuilder
- Parameters:
aFields
- The CSV elements to be stored by the CSV elements property.
-
getDelimiter
public char getDelimiter()
- Specified by:
getDelimiter
in interfaceorg.refcodes.mixin.DelimiterAccessor
-
setDelimiter
public void setDelimiter(char aDelimiterChar)
- Specified by:
setDelimiter
in interfaceorg.refcodes.mixin.DelimiterAccessor.DelimiterMutator
-
toFields
public List<String> toFields() throws IllegalStateException
Creates a list ofString
elements from the CSV line being set viaCsvBuilder.setRecord(String)
orCsvBuilder.withRecord(String)
.- Specified by:
toFields
in interfaceCsvBuilder
- Returns:
- The elements created from the CSV line.
- Throws:
IllegalStateException
- Thrown in case no elements have been set via theCsvBuilder.withFields(String[])
orCsvBuilder.setFields(String[])
methods.
-
toRecord
public String toRecord() throws IllegalStateException
Creates a CSV line from the elements being set viaCsvBuilder.setFields(String[])
orCsvBuilder.withFields(String[])
.- Specified by:
toRecord
in interfaceCsvBuilder
- Returns:
- The CSV line created from the CSV line.
- Throws:
IllegalStateException
- Thrown in case no CSV line has been set via theCsvBuilder.withRecord(String)
orCsvBuilder.setRecord(String)
methods.
-
toRecord
public String toRecord(Object... aFields)
Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method). Still changing for example the delimiter viaCsvBuilder.withDelimiter(char)
can cause side effects! For avoiding thread race conditions / side effects regarding the fields (and the resulting record), use this method instead of the combination ofCsvBuilder.withFields(String...)
withCsvBuilder.toRecord()
.- Specified by:
toRecord
in interfaceCsvBuilder
- Parameters:
aFields
- The CSV elements to be converted into a record.- Returns:
- The CSV line created from the fields.
-
toRecord
public String toRecord(Collection<Object> aFields)
Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method). Still changing for example the delimiter viaCsvBuilder.withDelimiter(char)
can cause side effects! For avoiding thread race conditions / side effects regarding the fields (and the resulting record), use this method instead of the combination ofCsvBuilder.withFields(String...)
withCsvBuilder.toRecord()
.- Specified by:
toRecord
in interfaceCsvBuilder
- Parameters:
aFields
- The CSV elements to be converted into a record.- Returns:
- The CSV line created from the fields.
-
toStrings
public String[] toStrings() throws IllegalStateException
Creates an array ofString
s from the CSV line being set viaCsvBuilder.setRecord(String)
orCsvBuilder.withRecord(String)
. It should return the same result as an array as a call toCsvBuilder.toFields()
. Returns null if the record is a comment as ofCsvMixin.isComment(String)
.- Specified by:
toStrings
in interfaceCsvBuilder
- Returns:
- The elements created from the CSV line or null if the record is a comment.
- Throws:
IllegalStateException
-
toRecord
public String toRecord(String... aFields) throws IllegalStateException
Creates a CSV line from the fields being provided This method is to be side effect free in terms of the fields (and the resulting record) is not part of the state for this instance (from the point of view of this method). Still changing for example the delimiter viaCsvBuilder.withDelimiter(char)
can cause side effects! For avoiding thread race conditions / side effects regarding the fields (and the resulting record), use this method instead of the combination ofCsvBuilder.withFields(String...)
withCsvBuilder.toRecord()
.- Specified by:
toRecord
in interfaceCsvBuilder
- Parameters:
aFields
- The CSV elements to be converted into a record.- Returns:
- The CSV line created from the fields.
- Throws:
IllegalStateException
-
toStrings
public String[] toStrings(String aRecord)
Convenience method for theCsvBuilder.toFields(String)
method.- Specified by:
toStrings
in interfaceCsvBuilder
- Parameters:
aRecord
- The CSV line to be stored by the CSV line property.- Returns:
- The elements created from the CSV line or null if the record is a comment.
-
toString
public String toString() throws IllegalStateException
TheString
being build by the builder upon the settings of the attributes. It should return the same result as a call toCsvBuilder.toRecord()
.- Specified by:
toString
in interfaceCsvBuilder
- Overrides:
toString
in classObject
- Returns:
- The according resulting
String
- Throws:
IllegalStateException
-
setCommentPrefixes
public void setCommentPrefixes(String... aCommentPrefixes)
Sets the prefixes used to identify CSV lines to be ignored.- Specified by:
setCommentPrefixes
in interfaceCsvMixin
- Parameters:
aCommentPrefixes
- The prefixes used to identify CSV lines to be ignored.
-
getCommentPrefixes
public String[] getCommentPrefixes()
Gets the prefixes used to identify CSV lines to be ignored.- Specified by:
getCommentPrefixes
in interfaceCsvMixin
- Returns:
- return The prefixes used to identify CSV lines to be ignored.
-
clearCommentPrefixes
public void clearCommentPrefixes()
Clears the prefixes used to identify CSV lines to be ignored. No CSV lines will be ignored any more.- Specified by:
clearCommentPrefixes
in interfaceCsvMixin
-
fromSeparatedValues
protected static List<String> fromSeparatedValues(String aRecord, char aDelimiter, CsvEscapeMode aCsvEscapeMode, boolean isTrimRecords)
From separated values.- Parameters:
aRecord
- the recordaDelimiter
- the delimiteraCsvEscapeMode
- the csv escape modeisTrimRecords
- When true, then leading and trailing whitespaces are trimmed.- Returns:
- the list
-
toSeparatedValues
protected static String toSeparatedValues(Collection<Object> aFields, char aDelimiter, CsvEscapeMode aCsvEscapeMode)
To separated values.- Parameters:
aFields
- the fieldsaDelimiter
- the delimiteraCsvEscapeMode
- the csv escape mode- Returns:
- the string
-
toSeparatedValues
protected static String toSeparatedValues(Collection<?> aCollection)
Takes the collection and creates a singleString
with the accordingDelimiter.CSV
delimiter. Unescaping any escaped chars is taken care of by this method, for example an unescaped delimiter char is escaped in the result.
-
toSeparatedValues
protected static String toSeparatedValues(Collection<?> aCollection, char aDelimiter)
Takes the collection and creates a singleString
with the provided delimiter. Unescaping any escaped chars is taken care of by this method, for example an unescaped delimiter char is escaped in the result.
-
toSeparatedValue
protected static String toSeparatedValue(Object aValue)
Escapes a single value according to the rules of CSV like separated values with the accordingDelimiter.CSV
delimiter.- Parameters:
aValue
- The value which'sString
representation is to be escaped accordingly.- Returns:
- A single value escaped according to the rules of CSV like separated values.
-
toSeparatedValue
protected static String toSeparatedValue(Object aValue, char aDelimiter)
Escapes a single value according to the rules of CSV like separated values.
-
fromSeparatedValues
protected static List<String> fromSeparatedValues(String aSeparatedValues)
Takes theString
and creates aList
withString
instances containing the elements as identified from theString
by splitting theString
at the delimiter positions and adding those chunks to theList
with the accordingDelimiter.CSV
delimiter.. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.
-
fromSeparatedValues
protected static List<String> fromSeparatedValues(String aSeparatedValues, char aDelimiter)
Takes theString
and creates aList
withString
instances containing the elements as identified from theString
by splitting theString
at the delimiter positions and adding those chunks to theList
. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.- Parameters:
aSeparatedValues
- TheString
representing the CSVString
where contained elements are separated from each other by the given delimiter.aDelimiter
- The delimiter used for separating the elements if the single CSVString
.- Returns:
- The
List
containing the elements as identified from the given CSVString
.
-
toSplitValues
protected static String toSplitValues(Object[] aArray)
Takes the array'sString
representation and creates a singleString
with the accordingDelimiter.CSV
delimiter. No escaping is taken care of, for escaping either use the accordingtoSeparatedValues(Object[])
method or do escaping manually.
-
toSplitValues
protected static String toSplitValues(Object[] aArray, char aDelimiter)
Takes the array's String representation and creates a singleString
with the provided delimiter. No escaping is taken of, for escaping either use the accordingtoSeparatedValues(Object[], char)
method or do escaping manually.
-
toSplitValues
protected static String toSplitValues(Collection<?> aCollection)
Takes the collection and creates a singleString
with the accordingDelimiter.CSV
delimiter. No escaping is taken of, for escaping either use the accordingtoSeparatedValues(java.util.Collection<java.lang.Object>,char,org.refcodes.textual.CsvEscapeMode)
method or do escaping manually viatoSeparatedValue(java.lang.Object)
.
-
toSplitValues
protected static String toSplitValues(Collection<?> aCollection, char aDelimiter)
Takes the collection and creates a singleString
with the provided delimiter. No escaping is taken of, for escaping either use the accordingtoSeparatedValues(java.util.Collection<java.lang.Object>,char,org.refcodes.textual.CsvEscapeMode)
method or do escaping manually viatoSeparatedValue(java.lang.Object)
.
-
toSplitValue
protected static String toSplitValue(Object aValue, char aDelimiter)
Creates theString
representation of a single split value not applying any escaping.
-
fromSplitValues
protected static List<String> fromSplitValues(String aSplitValues)
Takes theString
and creates aList
withString
instances containing the elements as identified from theString
by splitting theString
at the delimiter positions and adding those chunks to theList
with the accordingDelimiter.CSV
delimiter.. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.
-
fromSplitValues
protected static List<String> fromSplitValues(String aSplitValues, char aDelimiter)
Takes theString
and creates aList
withString
instances containing the elements as identified from theString
by splitting theString
at the delimiter positions and adding those chunks to theList
. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.- Parameters:
aSplitValues
- TheString
representing the CSVString
where contained elements are split from each other by the given delimiter.aDelimiter
- The delimiter used for separating the elements if the single CSVString
.- Returns:
- The
List
containing the elements as identified from the given CSVString
.
-
fromSplitValues
protected static List<String> fromSplitValues(String aValues, String aDelimiters)
Takes theString
and creates aList
withString
instances containing the elements as identified from theString
by splitting theString
at the delimiter positions and adding those chunks to theList
. Escaping any unescaped chars is taken care of by this method, for example an escaped delimiter char is unescaped in the result.
-
toSeparatedValues
protected static String toSeparatedValues(Object[] aArray, char aDelimiter)
Takes the array's String representation and creates a singleString
with the provided delimiter. Unescaping any escaped chars is taken care of by this method, for example an unescaped delimiter char is escaped in the result.
-
toEscapedRegexp
protected static String toEscapedRegexp(char aDelimiter)
To escaped regexp.- Parameters:
aDelimiter
- the delimiter- Returns:
- the string
-
-