Class OutputValueSwitch
- java.lang.Object
-
- com.univocity.parsers.common.processor.RowWriterProcessorSwitch
-
- com.univocity.parsers.common.processor.OutputValueSwitch
-
- All Implemented Interfaces:
RowWriterProcessor<java.lang.Object>
public class OutputValueSwitch extends RowWriterProcessorSwitch
A concrete implementation ofRowWriterProcessorSwitch
that allows switching among different implementations ofRowWriterProcessor
based on values found on rows to be written to an output
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
OutputValueSwitch.Switch
-
Field Summary
Fields Modifier and Type Field Description private int
columnIndex
private java.util.Comparator
comparator
private OutputValueSwitch.Switch
defaultSwitch
private java.lang.String
headerName
private OutputValueSwitch.Switch
selectedSwitch
private OutputValueSwitch.Switch[]
switches
private java.lang.Class[]
types
-
Constructor Summary
Constructors Constructor Description OutputValueSwitch()
Creates a switch that will analyze the first column of output rows to determine whichRowWriterProcessor
to use for each output rowOutputValueSwitch(int columnIndex)
Creates a switch that will analyze a column of output rows to determine whichRowWriterProcessor
to use.OutputValueSwitch(java.lang.String headerName)
Creates a switch that will analyze a column of output rows to determine whichRowWriterProcessor
to use.OutputValueSwitch(java.lang.String headerName, int columnIndex)
Creates a switch that will analyze a column of output rows to determine whichRowWriterProcessor
to use.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addSwitch(OutputValueSwitch.Switch newSwitch)
<T> void
addSwitchForType(java.lang.Class<T> beanType)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.<T> void
addSwitchForType(java.lang.Class<T> beanType, int... indexesToUse)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.<T> void
addSwitchForType(java.lang.Class<T> beanType, java.lang.String... headersToUse)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.void
addSwitchForValue(java.lang.Object value, RowWriterProcessor<java.lang.Object[]> rowProcessor)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.void
addSwitchForValue(java.lang.Object value, RowWriterProcessor<java.lang.Object[]> rowProcessor, int... indexesToUse)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.void
addSwitchForValue(java.lang.Object value, RowWriterProcessor<java.lang.Object[]> rowProcessor, java.lang.String... headersToUse)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.protected java.lang.String
describeSwitch()
int
getColumnIndex()
Returns the column index whose values will be used to switching from a row processor to another.protected NormalizedString[]
getHeaders()
Returns the headers in use by the current row writer processor implementation, which can vary among row writer processors.NormalizedString[]
getHeaders(java.lang.Object input)
Returns the sequence of headers to use for processing an input record.NormalizedString[]
getHeaders(java.util.Map headerMapping, java.util.Map mapInput)
Returns the sequence of headers to use for processing an input record represented by a map A map of headers can be optionally provided to assign a name to the keys of the input map.private NormalizedString[]
getHeadersFromSwitch(java.lang.Object value)
protected int[]
getIndexes()
Returns the indexes in use by the current row writer processor implementation, which can vary among row writer processors.private OutputValueSwitch.Switch
getSwitch(java.lang.Object value)
private java.util.List<java.lang.Object>
getSwitchValues()
private java.lang.String
getValidatedHeaderName(java.lang.String headerName)
private int
getValidatedIndex(int columnIndex)
private <V> V
getValue(java.util.Map<?,V> map, int index)
void
setComparator(java.util.Comparator<?> comparator)
Configures the switch to use a customComparator
to compare values in the column to analyze which is given in the constructor of this class.void
setDefaultSwitch(RowWriterProcessor<java.lang.Object[]> rowProcessor, int... indexesToUse)
Defines a defaultRowWriterProcessor
implementation to use when no matching value is found in the output row.void
setDefaultSwitch(RowWriterProcessor<java.lang.Object[]> rowProcessor, java.lang.String... headersToUse)
Defines a defaultRowWriterProcessor
implementation to use when no matching value is found in the output row.protected RowWriterProcessor<?>
switchRowProcessor(java.lang.Object row)
Analyzes an output row to determine whether or not the row writer processor implementation must be changed-
Methods inherited from class com.univocity.parsers.common.processor.RowWriterProcessorSwitch
getMinimumRowLength, rowProcessorSwitched, write, write
-
-
-
-
Field Detail
-
defaultSwitch
private OutputValueSwitch.Switch defaultSwitch
-
switches
private OutputValueSwitch.Switch[] switches
-
selectedSwitch
private OutputValueSwitch.Switch selectedSwitch
-
types
private java.lang.Class[] types
-
columnIndex
private final int columnIndex
-
headerName
private final java.lang.String headerName
-
comparator
private java.util.Comparator comparator
-
-
Constructor Detail
-
OutputValueSwitch
public OutputValueSwitch()
Creates a switch that will analyze the first column of output rows to determine whichRowWriterProcessor
to use for each output row
-
OutputValueSwitch
public OutputValueSwitch(int columnIndex)
Creates a switch that will analyze a column of output rows to determine whichRowWriterProcessor
to use.- Parameters:
columnIndex
- the column index whose value will be used to determine whichRowWriterProcessor
to use for each output row.
-
OutputValueSwitch
public OutputValueSwitch(java.lang.String headerName)
Creates a switch that will analyze a column of output rows to determine whichRowWriterProcessor
to use. When no column index is defined, the switch will use the first column of any input rows sent for writing.- Parameters:
headerName
- the column name whose value will be used to determine whichRowWriterProcessor
to use for each output row.
-
OutputValueSwitch
public OutputValueSwitch(java.lang.String headerName, int columnIndex)
Creates a switch that will analyze a column of output rows to determine whichRowWriterProcessor
to use.- Parameters:
headerName
- the column name whose value will be used to determine whichRowWriterProcessor
to use for each output row.columnIndex
- the position of an input column to use to perform the switch when no headers are available.
-
-
Method Detail
-
getValidatedIndex
private int getValidatedIndex(int columnIndex)
-
getValidatedHeaderName
private java.lang.String getValidatedHeaderName(java.lang.String headerName)
-
setComparator
public void setComparator(java.util.Comparator<?> comparator)
Configures the switch to use a customComparator
to compare values in the column to analyze which is given in the constructor of this class.- Parameters:
comparator
- the comparator to use for matching values in the output column with the values provided inaddSwitchForValue(Object, RowWriterProcessor)
-
setDefaultSwitch
public void setDefaultSwitch(RowWriterProcessor<java.lang.Object[]> rowProcessor, java.lang.String... headersToUse)
Defines a defaultRowWriterProcessor
implementation to use when no matching value is found in the output row.- Parameters:
rowProcessor
- the default row writer processor implementationheadersToUse
- the (optional) sequence of headers to assign to the given row writer processor
-
setDefaultSwitch
public void setDefaultSwitch(RowWriterProcessor<java.lang.Object[]> rowProcessor, int... indexesToUse)
Defines a defaultRowWriterProcessor
implementation to use when no matching value is found in the output row.- Parameters:
rowProcessor
- the default row writer processor implementationindexesToUse
- the (optional) sequence of column indexes to assign to the given row writer processor
-
getHeaders
protected NormalizedString[] getHeaders()
Description copied from class:RowWriterProcessorSwitch
Returns the headers in use by the current row writer processor implementation, which can vary among row writer processors. Ifnull
, the headers defined inCommonSettings.getHeaders()
will be returned.- Overrides:
getHeaders
in classRowWriterProcessorSwitch
- Returns:
- the current sequence of headers to use.
-
getIndexes
protected int[] getIndexes()
Description copied from class:RowWriterProcessorSwitch
Returns the indexes in use by the current row writer processor implementation, which can vary among row writer processors. Ifnull
, the indexes of fields that have been selected usingCommonSettings.selectFields(String...)
orCommonSettings.selectIndexes(Integer...)
will be returned.- Overrides:
getIndexes
in classRowWriterProcessorSwitch
- Returns:
- the current sequence of indexes to use.
-
getSwitch
private OutputValueSwitch.Switch getSwitch(java.lang.Object value)
-
switchRowProcessor
protected RowWriterProcessor<?> switchRowProcessor(java.lang.Object row)
Description copied from class:RowWriterProcessorSwitch
Analyzes an output row to determine whether or not the row writer processor implementation must be changed- Specified by:
switchRowProcessor
in classRowWriterProcessorSwitch
- Parameters:
row
- a record with data to be written to the output- Returns:
- the row writer processor implementation to use. If it is not the same as the one used by the previous row,
the returned row writer processor will be used, and the
RowWriterProcessorSwitch.rowProcessorSwitched(RowWriterProcessor, RowWriterProcessor)
method will be called.
-
addSwitchForValue
public void addSwitchForValue(java.lang.Object value, RowWriterProcessor<java.lang.Object[]> rowProcessor, java.lang.String... headersToUse)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.- Parameters:
value
- the value to match against the column of incoming output rows and trigger the usage of the given row writer processor implementation.rowProcessor
- the row writer processor implementation to use when the given value matches with the contents of the column provided in the constructor of this class.headersToUse
- the (optional) sequence of headers to assign to the given row writer processor
-
addSwitchForValue
public void addSwitchForValue(java.lang.Object value, RowWriterProcessor<java.lang.Object[]> rowProcessor)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.- Parameters:
value
- the value to match against the column of incoming output rows and trigger the usage of the given row writer processor implementation.rowProcessor
- the row writer processor implementation to use when the given value matches with the contents of the column provided in the constructor of this class.
-
addSwitchForValue
public void addSwitchForValue(java.lang.Object value, RowWriterProcessor<java.lang.Object[]> rowProcessor, int... indexesToUse)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.- Parameters:
value
- the value to match against the column of incoming output rows and trigger the usage of the given row writer processor implementation.rowProcessor
- the row writer processor implementation to use when the given value matches with the contents of the column provided in the constructor of this class.indexesToUse
- the (optional) sequence of column indexes to assign to the given row writer processor
-
addSwitchForType
public <T> void addSwitchForType(java.lang.Class<T> beanType, java.lang.String... headersToUse)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.- Type Parameters:
T
- the type of annotated java beans whose instances will be used to trigger the usage of a different format.- Parameters:
beanType
- the type of annotated java beans whose instances will be used to trigger the usage of a different format.headersToUse
- the (optional) sequence of headers to assign to the given row writer processor
-
addSwitchForType
public <T> void addSwitchForType(java.lang.Class<T> beanType, int... indexesToUse)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.- Type Parameters:
T
- the type of annotated java beans whose instances will be used to trigger the usage of a different format.- Parameters:
beanType
- the type of annotated java beans whose instances will be used to trigger the usage of a different format.indexesToUse
- the (optional) sequence of column indexes to assign to the given row writer processor
-
addSwitchForType
public <T> void addSwitchForType(java.lang.Class<T> beanType)
Associates aRowWriterProcessor
implementation with an expected value to be matched in the column provided in the constructor of this class.- Type Parameters:
T
- the type of annotated java beans whose instances will be used to trigger the usage of a different format.- Parameters:
beanType
- the type of annotated java beans whose instances will be used to trigger the usage of a different format.
-
addSwitch
private void addSwitch(OutputValueSwitch.Switch newSwitch)
-
getValue
private <V> V getValue(java.util.Map<?,V> map, int index)
-
getHeadersFromSwitch
private NormalizedString[] getHeadersFromSwitch(java.lang.Object value)
-
getHeaders
public NormalizedString[] getHeaders(java.lang.Object input)
Description copied from class:RowWriterProcessorSwitch
Returns the sequence of headers to use for processing an input record.- Specified by:
getHeaders
in classRowWriterProcessorSwitch
- Parameters:
input
- the record data- Returns:
- the sequence of headers to use when processing the given record.
-
getHeaders
public NormalizedString[] getHeaders(java.util.Map headerMapping, java.util.Map mapInput)
Description copied from class:RowWriterProcessorSwitch
Returns the sequence of headers to use for processing an input record represented by a map A map of headers can be optionally provided to assign a name to the keys of the input map. This is useful when the input map has keys will generate unwanted header names.- Specified by:
getHeaders
in classRowWriterProcessorSwitch
- Parameters:
headerMapping
- an optional map associating keys of the rowData map with expected header namesmapInput
- the record data- Returns:
- the sequence of headers to use when processing the given input map.
-
getColumnIndex
public int getColumnIndex()
Returns the column index whose values will be used to switching from a row processor to another.- Returns:
- the column index targeted by this switch.
-
getSwitchValues
private java.util.List<java.lang.Object> getSwitchValues()
-
describeSwitch
protected java.lang.String describeSwitch()
- Specified by:
describeSwitch
in classRowWriterProcessorSwitch
-
-