001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2023 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.util.events;
020
021import java.nio.file.Path;
022import org.jgrapes.core.Channel;
023import org.jgrapes.core.Components;
024import org.jgrapes.core.Event;
025
026/**
027 * Informs about changes of a watched file.
028 */
029public class FileChanged extends Event<Void> {
030
031    /**
032     * The kind of change detected.
033     */
034    public enum Kind {
035        CREATED, MODIFIED, DELETED
036    }
037
038    private final Path path;
039    private final Kind change;
040
041    /**
042     * Instantiates a new event.
043     *
044     * @param path the watched path
045     * @param change the change
046     */
047    public FileChanged(Path path, Kind change) {
048        this.path = path;
049        this.change = change;
050    }
051
052    /**
053     * Return's the event's path. 
054     * 
055     * @return the path
056     */
057    public Path path() {
058        return path;
059    }
060
061    /**
062     * Returns the change.
063     * 
064     * @return the change
065     */
066    public Kind change() {
067        return change;
068    }
069
070    @Override
071    public String toString() {
072        StringBuilder builder = new StringBuilder();
073        builder.append(Components.objectName(this))
074            .append(" [").append(change).append(": ").append(path);
075        if (channels() != null) {
076            builder.append(", channels=");
077            builder.append(Channel.toString(channels()));
078        }
079        builder.append(']');
080        return builder.toString();
081    }
082}