<commit_before>package com.redbullmediabase.resourcelayer.processing.ttt.webvtt.output;

import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.HeaderMetadata;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.Region;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueTiming;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueOrNote;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.Cue;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueTimestampNode;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CuePayload;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueTextNode;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueHtmlNode;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueRubyNode;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.Document;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.Note;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueHtmlNode.NodeType;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueSettings.CueSetting;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.RegionSettings.RegionSetting;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author <NAME>, (Michal.Samek at at.redbullmediahouse.com)
 */
public class Printer extends AbstractPrinter implements AutoCloseable {

    private final BufferedWriter writer;
    private final StringBuilder sb;

    public Printer(OutputStreamWriter writer) {
        this.writer = new BufferedWriter(writer);
        this.sb = new StringBuilder();
    }

    @Override
    public void print(Document webvtt) throws IOException {
        sb.append(Constants.WEBVTT_TAG);
        sb.append(Constants.LT);

        printMetadataBlock(webvtt.getMetadataBlock());
        sb.append(Constants.LT).append(Constants.LT);

        printCueBlock(webvtt.getCueBlock());
        sb.append(Constants.LT);

        writer.append(sb.toString());
        writer.flush();
    }

    private void printMetadataBlock(List<HeaderMetadata> metadataBlock) {
//        metadataBlock.stream()
//                .filter(m -> m.getClass().equals(Region.class))
//                .map(r -> Region.class.cast(r))
//                .forEach(r ->
//                    printRegion(r)
//                );
        for (HeaderMetadata m : metadataBlock) {
            if (m instanceof Region) {
                printRegion((Region) m);
            }
        }
    }

    private void printRegion(Region region) {
        sb.append(Constants.REGION_TAG)
                .append(Constants.METADATA_HEADER_VALUE_DELIMITER)
                .append(Constants.REGION_SETTINGS_DELIMITER);
        sb.append(Constants.REGION_ID_NAME)
                .append(Constants.REGION_SETTING_VALUE_DELIMITER)
                .append(region.getId())
                .append(Constants.REGION_SETTINGS_DELIMITER);

//        String settings = region.getSettings().stream()
//                //sort the settings, so that the order is deterministic
//                .sorted((a,b) -> a.getClass().getCanonicalName().compareTo(b.getClass().getCanonicalName()))
//                .map(setting -> RegionSettingPrinter.print(setting))
//                .reduce("", (a, b) -> a + Constants.REGION_SETTINGS_DELIMITER + b);
        
        List<RegionSetting> settingsList = new ArrayList<>(region.getSettings());
        //sort the settings, so that the order is deterministic
        settingsList.sort(new RegionSettingComparator());
        
        List<String> settingStrings = new ArrayList<>();
        for (RegionSetting s : settingsList) {
            settingStrings.add(RegionSettingPrinter.print(s));
        }
        String settings = StringUtils.join(settingStrings, Constants.REGION_SETTINGS_DELIMITER);
        
        sb.append(settings);
        sb.append(Constants.LT);
    }

    private void printCueBlock(List<CueOrNote> cueBlock) {
//        cueBlock.stream().forEach(
//                cueOrNote -> {
//                    if (cueOrNote instanceof Cue) {
//                        printCue((Cue) cueOrNote);
//                    } else {
//                        printNote((Note) cueOrNote);
//                    }
//                });
        for (CueOrNote cueOrNote : cueBlock) {
            if (cueOrNote instanceof Cue) {
                printCue((Cue) cueOrNote);
            } else {
                printNote((Note) cueOrNote);
            }
        }
    }

    private void printCue(Cue cue) {
        //Cue identifier
        String id = cue.getId();
        if (id != null && !id.isEmpty()) {
            sb.append(id).append(Constants.LT);
        }

        //Cue timing
        printCueTiming(cue.getTiming());

        //Cue settings
        sb.append(Constants.CUE_SETTINGLIST_DELIMITER);
//        String settings = cue.getSettings().stream()
//                //sort the settings, so that the order is deterministic
//                .sorted((a,b) -> a.getClass().getCanonicalName().compareTo(b.getClass().getCanonicalName()))
//                .map(setting -> CueSettingPrinter.print(setting))
//                .reduce("", (a, b) -> a + Constants.CUE_SETTINGS_DELIMITER + b);
        List<CueSetting> settingsList = new ArrayList<>(cue.getSettings());
        settingsList.sort(new CueSettingComparator());
        List<String> settingStrings = new ArrayList<>();
        for (CueSetting s : settingsList) {
            settingStrings.add(CueSettingPrinter.print(s));
        }
        <commit_msg>Removed java 8 List:sort method<commit_after>package com.redbullmediabase.resourcelayer.processing.ttt.webvtt.output;

import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.HeaderMetadata;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.Region;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueTiming;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueOrNote;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.Cue;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueTimestampNode;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CuePayload;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueTextNode;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueHtmlNode;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueRubyNode;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.Document;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.Note;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueHtmlNode.NodeType;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.CueSettings.CueSetting;
import com.redbullmediabase.resourcelayer.processing.ttt.webvtt.model.RegionSettings.RegionSetting;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author <NAME>, (Michal.Samek at at.redbullmediahouse.com)
 */
public class Printer extends AbstractPrinter implements AutoCloseable {

    private final BufferedWriter writer;
    private final StringBuilder sb;

    public Printer(OutputStreamWriter writer) {
        this.writer = new BufferedWriter(writer);
        this.sb = new StringBuilder();
    }

    @Override
    public void print(Document webvtt) throws IOException {
        sb.append(Constants.WEBVTT_TAG);
        sb.append(Constants.LT);

        printMetadataBlock(webvtt.getMetadataBlock());
        sb.append(Constants.LT).append(Constants.LT);

        printCueBlock(webvtt.getCueBlock());
        sb.append(Constants.LT);

        writer.append(sb.toString());
        writer.flush();
    }

    private void printMetadataBlock(List<HeaderMetadata> metadataBlock) {
//        metadataBlock.stream()
//                .filter(m -> m.getClass().equals(Region.class))
//                .map(r -> Region.class.cast(r))
//                .forEach(r ->
//                    printRegion(r)
//                );
        for (HeaderMetadata m : metadataBlock) {
            if (m instanceof Region) {
                printRegion((Region) m);
            }
        }
    }

    private void printRegion(Region region) {
        sb.append(Constants.REGION_TAG)
                .append(Constants.METADATA_HEADER_VALUE_DELIMITER)
                .append(Constants.REGION_SETTINGS_DELIMITER);
        sb.append(Constants.REGION_ID_NAME)
                .append(Constants.REGION_SETTING_VALUE_DELIMITER)
                .append(region.getId())
                .append(Constants.REGION_SETTINGS_DELIMITER);

//        String settings = region.getSettings().stream()
//                //sort the settings, so that the order is deterministic
//                .sorted((a,b) -> a.getClass().getCanonicalName().compareTo(b.getClass().getCanonicalName()))
//                .map(setting -> RegionSettingPrinter.print(setting))
//                .reduce("", (a, b) -> a + Constants.REGION_SETTINGS_DELIMITER + b);
        
        List<RegionSetting> settingsList = new ArrayList<>(region.getSettings());
        //sort the settings, so that the order is deterministic
        Collections.sort(settingsList, new RegionSettingComparator());
        
        List<String> settingStrings = new ArrayList<>();
        for (RegionSetting s : settingsList) {
            settingStrings.add(RegionSettingPrinter.print(s));
        }
        String settings = StringUtils.join(settingStrings, Constants.REGION_SETTINGS_DELIMITER);
        
        sb.append(settings);
        sb.append(Constants.LT);
    }

    private void printCueBlock(List<CueOrNote> cueBlock) {
//        cueBlock.stream().forEach(
//                cueOrNote -> {
//                    if (cueOrNote instanceof Cue) {
//                        printCue((Cue) cueOrNote);
//                    } else {
//                        printNote((Note) cueOrNote);
//                    }
//                });
        for (CueOrNote cueOrNote : cueBlock) {
            if (cueOrNote instanceof Cue) {
                printCue((Cue) cueOrNote);
            } else {
                printNote((Note) cueOrNote);
            }
        }
    }

    private void printCue(Cue cue) {
        //Cue identifier
        String id = cue.getId();
        if (id != null && !id.isEmpty()) {
            sb.append(id).append(Constants.LT);
        }

        //Cue timing
        printCueTiming(cue.getTiming());

        //Cue settings
        sb.append(Constants.CUE_SETTINGLIST_DELIMITER);
//        String settings = cue.getSettings().stream()
//                //sort the settings, so that the order is deterministic
//                .sorted((a,b) -> a.getClass().getCanonicalName().compareTo(b.getClass().getCanonicalName()))
//                .map(setting -> CueSettingPrinter.print(setting))
//                .reduce("", (a, b) -> a + Constants.CUE_SETTINGS_DELIMITER + b);
        List<CueSetting> settingsList = new ArrayList<>(cue.getSettings());
        Collections.sort(settingsList, new CueSettingComparator());
        List<String> settingStrings = new ArrayList<>();
        for (CueSetting s : settingsList) {
            settingStrings.add(CueSettingPrinter.print(s));
        }
        
{'fraction_non_alphanumeric': 0.1157556270096463, 'fraction_numerical': 0.0002538500592316805, 'mean_word_length': 2.5891284542969935, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 45, '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': '3146724', 'n_tokens_mistral': 3537, 'n_tokens_neox': 3427, 'n_words': 526}