56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using System;
|
|
|
|
namespace BCG
|
|
{
|
|
/// <summary>
|
|
/// Maps (multiplexes) columns values which belong to a given set into a single string
|
|
/// value in a new column. Values that are not in the set are converted to strings
|
|
/// and copied to the output column.
|
|
/// </summary>
|
|
public class MuxStream : ViewStream, IStreamMapper
|
|
{
|
|
private string myOutColumn;
|
|
private string myInColumn;
|
|
private string myOutValue;
|
|
private object[] myMuxedValues;
|
|
BlueTableSchema? mySchema;
|
|
|
|
public MuxStream(ViewStream inStream, string outColumn, string outValue, string inColumn, params object[] muxedValues)
|
|
{
|
|
myOutColumn = outColumn;
|
|
myOutValue = outValue;
|
|
|
|
myInColumn = inColumn;
|
|
myMuxedValues = (object[])muxedValues.Clone();
|
|
mySetRealSource(new MapStream(inStream, this));
|
|
}
|
|
|
|
#region IStreamMapper Members
|
|
|
|
IBlueTableSchema IStreamMapper.getOutputSchema(IBlueTableSchema inputSchema)
|
|
{
|
|
ColumnInfo[] cis = new ColumnInfo[inputSchema.columnInfo.Length + 1];
|
|
Array.Copy(inputSchema.columnInfo, cis, inputSchema.columnInfo.Length);
|
|
cis[cis.Length - 1] = new ColumnInfo(myOutColumn, cis.Length - 1, typeof(string));
|
|
mySchema = new BlueTableSchema(cis, String.Format("muxed {0} to {1} in ({2})", myInColumn, myOutColumn, inputSchema.tableName));
|
|
return mySchema;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDynamicStreamMapper Members
|
|
|
|
IBlueRow IDynamicStreamMapper.map(IBlueRow row)
|
|
{
|
|
MutableBlueRow r = new MutableBlueRow(mySchema, row);
|
|
if (Array.IndexOf(myMuxedValues, r[myInColumn]) != -1) // muxable value?
|
|
r.setValue(myOutColumn, myOutValue);
|
|
else
|
|
r.setValue(myOutColumn, row[myInColumn]?.ToString());
|
|
return r;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|