<commit_before>/*******************************************************************************
 * Copyright 2012 EMBL-EBI, Hinxton outstation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package uk.ac.ebi.embl.flatfile.writer.embl;

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.Vector;

import uk.ac.ebi.embl.flatfile.EmblPadding;
import uk.ac.ebi.embl.api.entry.Entry;
import uk.ac.ebi.embl.api.entry.feature.Feature;
import uk.ac.ebi.embl.flatfile.writer.FeatureWriter;
import uk.ac.ebi.embl.flatfile.writer.FlatFileWriter;
import uk.ac.ebi.embl.flatfile.writer.WrapType;

/** Flat file writer for the feature table.
 */
public class FTWriter extends FlatFileWriter {

	public FTWriter(Entry entry, boolean sortFeatures,
					boolean sortQualifiers, WrapType wrapType) {
		super(entry, wrapType);
		this.sortFeatures = sortFeatures;
		this.sortQualifiers = sortQualifiers;
	}

	public FTWriter(Entry entry, boolean sortFeatures,
					boolean sortQualifiers, boolean excludeSource, WrapType wrapType) {
		super(entry, wrapType);
		this.sortFeatures = sortFeatures;
		this.sortQualifiers = sortQualifiers;
		this.excludeSource = excludeSource;
	}

	private boolean sortFeatures;
	private boolean sortQualifiers;
	private boolean excludeSource;

	public boolean write(Writer writer) throws IOException {
		Vector<Feature> features = new Vector<Feature>(entry.getFeatures());
		if (features == null ||
			features.size() == 0) {
			return false;
		}
		if (excludeSource &&
				features.size() == 1 &&
				isSourceFeature(features.get(0))) {
			return false;
		}
		if (sortFeatures) {
			Collections.sort(features);
		}
		new FHWriter(entry).write(writer);
		for (Feature feature : features) {
			if (excludeSource && isSourceFeature(feature))
				continue;
            writeFeature(writer, feature);
        }
		return true;		
	}

	private boolean isSourceFeature(Feature feature) {
		return feature.getName().equals(Feature.SOURCE_FEATURE_NAME);
	}

    protected void writeFeature(Writer writer, Feature feature) throws IOException {
        new FeatureWriter(entry, feature, sortQualifiers, wrapType,
        		EmblPadding.FEATURE_PADDING,
        		EmblPadding.QUALIFIER_PADDING).write(writer);
    }<commit_msg>handle multiple source feature in ff<commit_after>/*******************************************************************************
 * Copyright 2012 EMBL-EBI, Hinxton outstation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package uk.ac.ebi.embl.flatfile.writer.embl;

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.Vector;

import uk.ac.ebi.embl.flatfile.EmblPadding;
import uk.ac.ebi.embl.api.entry.Entry;
import uk.ac.ebi.embl.api.entry.feature.Feature;
import uk.ac.ebi.embl.flatfile.writer.FeatureWriter;
import uk.ac.ebi.embl.flatfile.writer.FlatFileWriter;
import uk.ac.ebi.embl.flatfile.writer.WrapType;

/** Flat file writer for the feature table.
 */
public class FTWriter extends FlatFileWriter {

	public FTWriter(Entry entry, boolean sortFeatures,
					boolean sortQualifiers, WrapType wrapType) {
		super(entry, wrapType);
		this.sortFeatures = sortFeatures;
		this.sortQualifiers = sortQualifiers;
	}

	public FTWriter(Entry entry, boolean sortFeatures,
					boolean sortQualifiers, boolean excludeSource, WrapType wrapType) {
		super(entry, wrapType);
		this.sortFeatures = sortFeatures;
		this.sortQualifiers = sortQualifiers;
		this.excludeSource = excludeSource;
	}

	private boolean sortFeatures;
	private boolean sortQualifiers;
	private boolean excludeSource;

	public boolean write(Writer writer) throws IOException {
		Vector<Feature> features = new Vector<Feature>(entry.getFeatures());
		if (features == null ||
			features.size() == 0) {
			return false;
		}
		if (excludeSource && allSourceFeatures(features)) {
			return false;
		}
		if (sortFeatures) {
			Collections.sort(features);
		}
		new FHWriter(entry).write(writer);
		for (Feature feature : features) {
			if (excludeSource && isSourceFeature(feature))
				continue;
            writeFeature(writer, feature);
        }
		return true;		
	}

	private boolean isSourceFeature(Feature feature) {
		return feature.getName().equals(Feature.SOURCE_FEATURE_NAME);
	}

	private boolean allSourceFeatures(Vector<Feature> features) {
		for (Feature feature : features) {
			if (!isSourceFeature(feature))
				return false;
		}
		return true;
	}

    protected void writeFeature(Writer writer, Feature feature) throws IOException {
        new FeatureWriter(entry, feature, sortQualifiers, wrapType,
        		EmblPadding.FEATURE_PADDING,
        		EmblPadding.QUALIFIER_PADDING).write(writer);
    }
{'fraction_non_alphanumeric': 0.14370647188720911, 'fraction_numerical': 0.0033973161202649907, 'mean_word_length': 4.446808510638298, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3108543', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1622, 'n_words': 463}