<commit_before>package ru.dolika.experiment.Analyzer;

import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitor;

import ru.dolika.experiment.measurement.Measurement;
import ru.dolika.experiment.measurement.Temperature;
import ru.dolika.experiment.measurement.TemperatureConductivity;
import ru.dolika.experiment.sample.Sample;
import ru.dolika.experiment.signalID.AdjustmentSignalID;
import ru.dolika.experiment.signalID.BaseSignalID;
import ru.dolika.experiment.signalID.DCsignalID;
import ru.dolika.experiment.signalID.SignalIdentifier;
import ru.dolika.experiment.workspace.Workspace;
import ru.dolika.experiment.zeroCrossing.ZeroCrossing;

public class ExperimentComputer implements Callable<Measurement> {

	public double truncatePositive(double value) {
		while (value < 0) {
			value += Math.PI * 2.0;
		}
		while (value > Math.PI * 2.0) {
			value -= Math.PI * 2.0;
		}
		return value;
	}

	public static Measurement computeFolder(File folder, Workspace workspace, JFrame parent) {
		if (!folder.isDirectory())
			return null;
		if (!folder.exists())
			return null;

		File[] files = folder.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().matches("^[0-9]+.txt$");
			}
		});
		if (files.length <= 0)
			return null;
		BufferedWriter bw;
		File resultFile;
		try {
			resultFile = new File(folder, "result-" + folder.getName() + ".tsv");
			if (resultFile.exists()) {
				boolean exception = false;
				do {
					exception = false;
					try {
						Files.delete(resultFile.toPath());
					} catch (java.nio.file.FileSystemException e) {
						e.printStackTrace();
						exception = true;
						JOptionPane.showMessageDialog(null, resultFile.toString(), "Close the file!!!",
								JOptionPane.ERROR_MESSAGE);
						System.err.println("Please, close the file: " + resultFile.toString());
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
					}
				} while (exception);
			}
			bw = Files.newBufferedWriter(resultFile.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);

		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
		Vector<Future<Measurement>> set = new Vector<Future<Measurement>>();
		ProgressMonitor pm = new ProgressMonitor(parent, "Папка обрабатывается слишком долго", "", 0, 1);
		pm.setMaximum(files.length);
		for (File f : files) {
			Future<Measurement> future = pool.submit(new ExperimentComputer(f, workspace));
			set.add(future);
		}

		int currentProgress = 0;
		boolean header = true;
		for (Future<Measurement> future : set) {
			try {
				Measurement answer = future.get();
				if (answer != null) {
					Sample sample;
					if ((sample = workspace.getSample()) != null) {
						if (sample.measurements != null) {
							sample.measurements.add(answer);
						}
					}
					if (header) {
						header = false;
						bw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }));
						bw.write(String.format("%s%n", answer.getHeader()));
					}
					bw.write(String.format("%s%n", answer));
				}
				pm.setProgress(++currentProgress);
			} catch (InterruptedException | ExecutionException | IOException e) {
				e.printStackTrace();
			}
		}
		pm.close();
		pool.shutdown();

		try {
			bw.flush();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (Desktop.isDesktopSupported()) {
			try {
				Desktop.getDesktop().open(resultFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}

	public static SignalParameters[] getAllSignalParameters(double[][] signals, int frequency) {
		SignalParameters[] params = new SignalParameters[signals.length];
		for (int i = 0; i < signals.length; i++) {
			double[] signal = signals[i];
			params[i] = getSignalParameters(signal, frequency);
		}
		return params;
	}

	public static SignalParameters getSignalParameters(double[] signal, int frequency) {
		double[] fourierForFreq = FFT.getFourierForIndex(signal, frequency);<commit_msg>Обновлён код, который смотрит файлы в папке. Небольшой рефакторинг для улучшения читабельности кода<commit_after>package ru.dolika.experiment.Analyzer;

import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitor;

import ru.dolika.experiment.measurement.Measurement;
import ru.dolika.experiment.measurement.Temperature;
import ru.dolika.experiment.measurement.TemperatureConductivity;
import ru.dolika.experiment.sample.Sample;
import ru.dolika.experiment.signalID.AdjustmentSignalID;
import ru.dolika.experiment.signalID.BaseSignalID;
import ru.dolika.experiment.signalID.DCsignalID;
import ru.dolika.experiment.signalID.SignalIdentifier;
import ru.dolika.experiment.workspace.Workspace;
import ru.dolika.experiment.zeroCrossing.ZeroCrossing;

public class ExperimentComputer implements Callable<Measurement> {

	public double truncatePositive(double value) {
		while (value < 0) {
			value += Math.PI * 2.0;
		}
		while (value > Math.PI * 2.0) {
			value -= Math.PI * 2.0;
		}
		return value;
	}

	public static ArrayList<Measurement> computeFolder(File folder, Workspace workspace, JFrame parent) {
		if (!folder.isDirectory())
			return null;
		if (!folder.exists())
			return null;

		ArrayList<File> files = new ArrayList<File>();
		files.addAll(Arrays.asList(folder.listFiles(pathname -> {
			return pathname.getName().matches("^[0-9]+.txt$");
		})));
		files.removeAll(null);
		if (files.size() <= 0)
			return null;
		BufferedWriter bw = null;
		File resultFile = tryToCreateResultFile(folder);
		if (resultFile == null)
			return null;
		try {
			bw = Files.newBufferedWriter(resultFile.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if (bw == null)
			return null;

		ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
		ArrayList<Future<Measurement>> futuresSet = new ArrayList<Future<Measurement>>();
		ProgressMonitor pm = new ProgressMonitor(parent, "Папка обрабатывается слишком долго", "", 0, 1);
		pm.setMaximum(files.size());
		files.forEach(f -> futuresSet.add(pool.submit(new ExperimentComputer(f, workspace))));

		int currentProgress = 0;
		boolean header = true;
		ArrayList<Measurement> measurements = new ArrayList<Measurement>();
		for (Future<Measurement> future : futuresSet) {
			try {
				Measurement answer = future.get();
				if (answer != null) {
					Sample sample;
					if ((sample = workspace.getSample()) != null) {
						measurements.add(answer);
						if (sample.measurements != null) {
							sample.measurements.add(answer);
						}
					}
					if (header) {
						header = false;
						bw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }));
						bw.write(String.format("%s%n", answer.getHeader()));
					}
					bw.write(String.format("%s%n", answer));
				}
				pm.setProgress(++currentProgress);
			} catch (InterruptedException | ExecutionException | IOException e) {
				e.printStackTrace();
			}
		}
		pm.close();
		pool.shutdown();

		try {
			bw.flush();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (Desktop.isDesktopSupported()) {
			try {
				Desktop.getDesktop().open(resultFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return measurements;
	}

	public static File tryToCreateResultFile(File folder) {
		File resultFile;
		final String formatStringOfReulstFile = "result-%s.tsv";
		try {
			resultFile = new File(folder, String.format(formatStringOfReulstFile, folder.getName()));
			if (resultFile.exists()) {
				boolean exception = false;
				do {
					exception = false;
					try {
						Files.delete(resultFile.toPath());
					} catch (java.nio.file.FileSystemException e) {
						e.printStackTrace();
						exception = true;

						JOptionPane.showMessageDialog(null, resultFile.toString(),
								"Пожалуйста, закройте файл с результатами.\n"
										+ "Иначе я не смогу записать туда новые результаты.\n"
										+ "При необходимости Вы можете сохранить копию файла вручную\n"
										+ "Я подожду и не буду трогать этот файл, пока Вы не закроете это окно\n",
								JOptionPane.ERROR_MESSAGE);
						System.err.printf("Пожалуйста, закройте файл: %s", resultFile.toString());
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
					}
				} while (exception);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return resultFile;
	}

	public static SignalParameters[] getAllSignalParameters(double[][] signals, int frequency) {
		SignalParameters[] params = new SignalParameters[signals.length];
		for (int i = 0; i < signals.length; i++) {
			double[] signal = signals[i];
			params[i] = getSignalParameters(signal, frequency);
		}
		return params;
	}

	public static SignalParameters getSignalParameters(double[] signal, int frequency) {
		double[] fourierForFreq = FFT.getFourierForIndex(signal, frequency);
{'fraction_non_alphanumeric': 0.12808124630250445, 'fraction_numerical': 0.004929994084007099, 'mean_word_length': 3.9867256637168142, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, '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': '4330101', 'n_tokens_mistral': 3811, 'n_tokens_neox': 3221, 'n_words': 678}