<commit_before>package de.frosner.datagenerator.export;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

import de.frosner.datagenerator.features.FeatureDefinition;
import de.frosner.datagenerator.features.FeatureValue;
import de.frosner.datagenerator.generator.Instance;

/**
 * Connection for exporting instances to a CSV file.
 */
public final class CsvExportConnection extends ExportConnection {

	private final BufferedWriter _out;
	private final boolean _exportFeatureNames;
	private final boolean _exportInstanceIds;
	private final String _exportLocation;

	public CsvExportConnection(OutputStream out, boolean exportFeatureNames, boolean exportInstanceIds, String location) {
		_out = new BufferedWriter(new OutputStreamWriter(out));
		_exportFeatureNames = exportFeatureNames;
		_exportInstanceIds = exportInstanceIds;
		_exportLocation = location;
	}

	public CsvExportConnection(OutputStream out, boolean exportFeatureNames, boolean exportInstanceIds) {
		this(out, exportFeatureNames, exportInstanceIds, out.toString());
	}

	@Override
	public void close() {
		try {
			_out.close();
		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}

	@Override
	protected void exportInstanceStrategy(Instance instance) {
		try {
			if (_exportInstanceIds) {
				_out.write(instance.getId() + ",");
			}
			Iterator<FeatureValue> values = instance.iterator();
			while (values.hasNext()) {
				_out.write(values.next().getValueAsString());
				if (values.hasNext()) {
					_out.write(",");
				}
			}
			_out.write("\n");
		} catch (IOException e) {<commit_msg>#130 document CsvExportConnection<commit_after>package de.frosner.datagenerator.export;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

import de.frosner.datagenerator.features.FeatureDefinition;
import de.frosner.datagenerator.features.FeatureValue;
import de.frosner.datagenerator.generator.Instance;

/**
 * Connection for exporting generated data in comma separated value (CSV) format to a given {@link OutputStream}.
 * Optionally a textual representation of the export location can be specified to make error messages readable.
 */
public final class CsvExportConnection extends ExportConnection {

	private final BufferedWriter _out;
	private final boolean _exportFeatureNames;
	private final boolean _exportInstanceIds;
	private final String _exportLocation;

	/**
	 * Creates a new {@link CsvExportConnection} exporting to the specified {@link OutputStream}. The specified location
	 * is used to display log and error messages.
	 * 
	 * @param outputStream
	 *            to export to
	 * @param exportFeatureNames
	 *            should feature names also be exported?
	 * @param exportInstanceIds
	 *            should instances have their ID exported as well?
	 * @param location
	 *            textual representation of the export location
	 */
	public CsvExportConnection(OutputStream outputStream, boolean exportFeatureNames, boolean exportInstanceIds,
			String location) {
		_out = new BufferedWriter(new OutputStreamWriter(outputStream));
		_exportFeatureNames = exportFeatureNames;
		_exportInstanceIds = exportInstanceIds;
		_exportLocation = location;
	}

	/**
	 * Creates a new {@link CsvExportConnection} exporting to the specified {@link OutputStream}.
	 * 
	 * @param outputStream
	 *            to export to
	 * @param exportFeatureNames
	 *            should feature names also be exported?
	 * @param exportInstanceIds
	 *            should instances have their ID exported as well?
	 **/
	public CsvExportConnection(OutputStream outputStream, boolean exportFeatureNames, boolean exportInstanceIds) {
		this(outputStream, exportFeatureNames, exportInstanceIds, outputStream.toString());
	}

	@Override
	public void close() {
		try {
			_out.close();
		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}

	@Override
	protected void exportInstanceStrategy(Instance instance) {
		try {
			if (_exportInstanceIds) {
				_out.write(instance.getId() + ",");
			}
			Iterator<FeatureValue> values = instance.iterator();
			while (values.hasNext()) {
				_out.write(values.next().getValueAsString());
				if (values.hasNext()) {
					_out.write(",");
				}
			}
			_out.write("\n");
		} catch (IOException e) {
{'fraction_non_alphanumeric': 0.09145661387463752, 'fraction_numerical': 0.0006691947356680794, 'mean_word_length': 4.448359659781288, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '680153', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1208, 'n_words': 341}