All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.etlunit.io.file.OrderedDataFileWriterImpl Maven / Gradle / Ivy

There is a newer version: 1.6.9
Show newest version
package org.etlunit.io.file;

import java.io.IOException;
import java.util.*;

class OrderedDataFileWriterImpl implements DataFileWriter
{
	private final RelationalDataFileWriterImpl writerImpl;
	private final DataFileSchema schema;
	private final FlatFile flatFile;
	private final List names;

	public OrderedDataFileWriterImpl(DataFile.DataConverter converter, FlatFile flatFile) throws IOException
	{
		writerImpl = new RelationalDataFileWriterImpl(converter, flatFile);
		schema = flatFile.getDataFileSchema();
		names = schema.getColumnNames();
		this.flatFile = flatFile;
	}

	@Override
	public void close() throws IOException
	{
		// run a sort operation on the set, then pass on to the relational writer
		Collections.sort(setData, new Comparator>()
		{
			@Override
			public int compare(Map o1, Map o2)
			{
				Map m1 = (Map) o1;
				Map m2 = (Map) o2;

				// get the two hash codes
				OrderKey keyOne = (OrderKey) m1.get(FlatFile.KEY_NULL);
				OrderKey keyTwo = (OrderKey) m2.get(FlatFile.KEY_NULL);

				return keyOne.compareTo(keyTwo);
			}
		});

		// pass all the rows to the writer
		for (Map row : setData)
		{
			writerImpl.addRow(row);
		}

		writerImpl.close();
	}

	List> setData = new ArrayList>();

	@Override
	public void addRow(Map rowData) throws IOException
	{
		Map colData = new HashMap();
		colData.putAll(rowData);

		OrderKey ok = FlatFile.addOrderKey(flatFile.getDataFileSchema(), colData, null);
		colData.put(FlatFile.KEY_NULL, ok);

		setData.add(colData);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy