<commit_before>/**
 * 
 */
package de.bkusche.bktail2.logfilehandler;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author bjornkusche
 *
 */
public enum S_LogfileHandler {

	INSTANCE;

	private ExecutorService service;
	private List<I_LogfileEventListener> logfileEventListeners;
	private S_LogfileHandler() {
		service = Executors.newCachedThreadPool();
		logfileEventListeners = new LinkedList<>();
	}

	public static S_LogfileHandler getInstance() {
		return INSTANCE;
	}

	public void addFileToWatch(File filepath) {
		//TODO implement evaluations
		service.execute( () -> {
			try (WatchService watchService = FileSystems.getDefault().newWatchService()) {

				Path logfile = Paths.get(filepath.getParentFile().getCanonicalPath());
				
				logfile.register(watchService, 
						StandardWatchEventKinds.ENTRY_CREATE, 
						StandardWatchEventKinds.ENTRY_MODIFY,
						StandardWatchEventKinds.ENTRY_DELETE);
				
				while (true) {
					WatchKey wk = watchService.take();
					if( wk == null ) {
						Thread.sleep(100L);
						continue;
					}
					wk.pollEvents().forEach(we ->{
						if( we.context() instanceof Path){
							Path p = (Path) we.context();
							if( p.getFileName().toString().equals(filepath.getName())){
								
								if( we.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)){
									logfileEventListeners.forEach( l -> l.onCreate(new LogfileEvent(filepath.getName(), p, 0) ) );
								} else if( we.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)){
									logfileEventListeners.forEach( l -> l.onModify(new LogfileEvent(filepath.getName(), p, 0) ) );
								} else if( we.kind().equals(StandardWatchEventKinds.ENTRY_DELETE)){
									logfileEventListeners.forEach( l -> l.onDelete(new LogfileEvent(filepath.getName(), p, 0) ) );
								}
							}
						}
					});
					wk.reset();
				}
			} catch (Throwable e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		});
	}
	
	public void addLogfileEventListener( I_LogfileEventListener l ){
		logfileEventListeners.add(l);
	}
	
	public void removeLogfileEventListener( I_LogfileEventListener l ){
		logfileEventListeners.remove(l);
	}
}<commit_msg>MOD: get rid of the WatchService<commit_after>/**
 * 
 */
package de.bkusche.bktail2.logfilehandler;

import java.io.File;
import java.nio.file.Files;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * @author bjornkusche
 * 
 */
public enum S_LogfileHandler {

	INSTANCE;

	private ExecutorService service;
	private List<I_LogfileEventListener> logfileEventListeners;
	private List<LogfileEvent> logfileEvents;
	
	private Function<File, Long> countLines = t -> {
		try (Stream<String> stream = Files.lines(t.toPath())) {
			return stream.count();
		} catch(Throwable e){}
		return 0L;
	};
	
	private Predicate<File> containsLogFile = t -> {
		return logfileEvents.stream()
			.filter( f -> f.getName().equals(t.getName() ) )
			.filter( f -> f.getPath().toString().equals(t.toPath().toString()))
			.count() > 0;
	};
	
	private Function<File, LogfileEvent> getLogfileEvent = t -> {
		return logfileEvents.stream()
			.filter( f -> f.getName().equals(t.getName() ) )
			.filter( f -> f.getPath().toString().equals(t.toPath().toString()))
			.findFirst().get();
	};
	
	private S_LogfileHandler() {
		service = Executors.newCachedThreadPool();
		logfileEventListeners = new LinkedList<>();
		logfileEvents = new LinkedList<>();
	}

	public static S_LogfileHandler getInstance() {
		return INSTANCE;
	}

	public void addFileToWatch(File filepath) {
		//TODO implement evaluations
		service.execute( () -> {
			while( true ){
				try {
					if (!containsLogFile.test(filepath) && filepath.exists()) {
						LogfileEvent logfileEvent = new LogfileEvent( filepath.getName(), 
								filepath.toPath(),
								countLines.apply(filepath));
						logfileEventListeners.forEach(l -> l.onCreate(
								logfileEvent));
						logfileEvents.add(logfileEvent);
					} else if (containsLogFile.test(filepath) && filepath.exists()) {
						LogfileEvent logfileEvent = getLogfileEvent.apply(filepath);
						long lines = countLines.apply(filepath);
						if( logfileEvent.getLength() != lines ){
							logfileEvent.setLength(lines);
							logfileEventListeners.forEach(l -> l.onModify(
									logfileEvent));
						}
					} else if (containsLogFile.test(filepath) && !filepath.exists()) {
						LogfileEvent logfileEvent = getLogfileEvent.apply(filepath);
						logfileEvents.remove(logfileEvent);
						logfileEventListeners.forEach(l ->l.onDelete(logfileEvent));
					}
						
					Thread.sleep(100L);
				} catch (Throwable e) {
					// TODO: handle exception
				}
			}
		});
	}
	
	public void addLogfileEventListener( I_LogfileEventListener l ){
		logfileEventListeners.add(l);
	}
	
	public void removeLogfileEventListener( I_LogfileEventListener l ){
		logfileEventListeners.remove(l);
	}
}
{'fraction_non_alphanumeric': 0.13071895424836602, 'fraction_numerical': 0.002427637721755369, 'mean_word_length': 3.7524401064773736, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 30, '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': '2282752', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1673, 'n_words': 307}