All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.eBusx.io.EFileNotification Maven / Gradle / Ivy

//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General
// Public License along with this library; if not, write to the
//
// Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330,
// Boston, MA
// 02111-1307 USA
//
// The Initial Developer of the Original Code is Charles W. Rapp.
// Portions created by Charles W. Rapp are
// Copyright 2013, 2016, 2019, 2020. Charles W. Rapp
// All Rights Reserved.
//

package net.sf.eBusx.io;

import java.io.File;
import java.io.Serializable;
import net.sf.eBus.messages.ELocalOnly;
import net.sf.eBus.messages.ENotificationMessage;

/**
 * Used to transmit a Java {@link java.nio.file.WatchEvent}
 * asynchronously to all interested listeners. The message
 * subject is the file or directory path. The message contains
 * the how the file changed (created, modified, or deleted), the
 * file modification timestamp (in Java millisecond epoch time),
 * and the file length in byte.
 *
 * @author Charles Rapp
 */

@ELocalOnly
public final class EFileNotification
    extends ENotificationMessage
    implements Serializable
{
//---------------------------------------------------------------
// Enums.
//

    /**
     * The watched file was either created, modified, or deleted.
     */
    public enum EventType
    {
    //-----------------------------------------------------------
    // Member methods.
    //

        /**
         * The file was newly created.
         */
        CREATE,

        /**
         * The file was modified.
         */
        MODIFY,

        /**
         * The file event was deleted. The file
         * {@link #lastModified last modify timestamp} and
         * {@link #length} will be zero.
         */
        DELETE
    } // end of enum EventType

//---------------------------------------------------------------
// Member data.
//

    //-----------------------------------------------------------
    // Constants.
    //

    /**
     * Serialization version identifier.
     */
    private static final long serialVersionUID = 0x050200L;

    //-----------------------------------------------------------
    // Locals.
    //

    /**
     * The notification applies to this file instance.
     */
    public final File file;

    /**
     * This notification signifies that either the file or
     * directory was created, modified, or deleted.
     */
    public final EventType eventType;

    /**
     * The date and time when the {@link #file} was last
     * modified. Will be zero if {@link #eventType} is
     * {@link EventType#DELETE}.
     */
    public final long lastModified;

    /**
     * {@link #file} length in bytes. Will be zero if
     * {@link #eventType} is {@link EventType#DELETE}.
     */
    public final long length;

//---------------------------------------------------------------
// Member methods.
//

    //-----------------------------------------------------------
    // Constructors.
    //

    /**
     * Creates a new file notification instance for the given
     * parameters.
     * @param subject the notification subject.
     * @param file the event applies to this file.
     * @param eventType {@code file} was either created,
     * modified, or deleted.
     * @param modifyTime the file modify time. Will be
     * {@code null} when {@code eventType} is
     * {@link EventType#DELETE}.
     * @param length the file length. Will be zero for a delete
     * event type.
     *
     * @deprecated use {@link Builder} to create this message.
     */
    @Deprecated
    /* package */ EFileNotification(final String subject,
                                    final File file,
                                    final EventType eventType,
                                    final long modifyTime,
                                    final long length)
    {
        this (subject,
              System.currentTimeMillis(),
              file,
              eventType,
              modifyTime,
              length);
    } // end of EFileNotification(...)

    /**
     * Creates a new file notification instance from the
     * de-serialized parameters.
     * @param subject the notification subject.
     * @param timestamp the message timestamp.
     * @param file the event applies to this file.
     * @param eventType {@code file} was either created,
     * modified, or deleted.
     * @param modifyTime the file modify time. Will be
     * {@code null} when {@code eventType} is
     * {@link EventType#DELETE}.
     * @param length the file length. Will be zero for a delete
     * event type.
     *
     * @deprecated use {@link Builder} to create this message.
     */
    @Deprecated
    public  EFileNotification(final String subject,
                              final long timestamp,
                              final File file,
                              final EventType eventType,
                              final long modifyTime,
                              final long length)
    {
        super (subject, timestamp);

        this.file = file;
        this.eventType = eventType;
        this.lastModified = modifyTime;
        this.length = length;
    } // end of EFileNotification(...)

    /**
     * Creates a new file notification from the given message
     * builder.
     * @param builder file notification message builder.
     */
    private EFileNotification(final Builder builder)
    {
        super (builder);

        this.file = builder.mFile;
        this.eventType = builder.mEventType;
        this.lastModified = builder.mLastModified;
        this.length = builder.mLength;
    } // end of EFileNotification(Builder)

    //
    // end of Constructors.
    //-----------------------------------------------------------

    /**
     * Returns an {@code EFileNotification} message builder.
     * @return message builder.
     */
    public static Builder builder()
    {
        return (new Builder());
    } // end of builder()

//---------------------------------------------------------------
// Inner classes.
//

    public static final class Builder
        extends ENotificationMessage.Builder
    {
    //-----------------------------------------------------------
    // Member data.
    //

        //-------------------------------------------------------
        // Locals.
        //

        private File mFile;
        private EventType mEventType;
        private long mLastModified;
        private long mLength;

    //-----------------------------------------------------------
    // Member methods.
    //

        //-------------------------------------------------------
        // Constructors.
        //

        private Builder()
        {
            super (EFileNotification.class);
        } // end of Builder()

        //
        // end of Constructors.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Builder Method Overrides.
        //

        @Override
        protected EFileNotification buildImpl()
        {
            return (new EFileNotification(this));
        } // end of buildImpl()

        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mFile, "file")
                         .requireNotNull(mEventType,
                                         "eventType"));
        } // end of validate(Validator)

        //
        // end of Builder Method Overrides.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Set Methods.
        //

        /**
         * Sets the file associated with the notification.
         * @param file notification applies to this file.
         * @return {@code this Builder}
         * @throws NullPointerException
         * if {@code file} is {@code null}.
         */
        public Builder file(final File file)
        {
            if (file == null)
            {
                throw (new NullPointerException("file is null"));
            }

            mFile = file;

            return (this);
        } // end of file(File)

        /**
         * Sets the detected file event type.
         * @param type detected event type.
         * @return {@code this Builder}.
         * @throws NullPointerException
         * if {@code type} is {@code null}.
         */
        public Builder eventType(final EventType type)
        {
            if (type == null)
            {
                throw (new NullPointerException("type is null"));
            }

            mEventType = type;

            return (this);
        } // end of eventType(EventType)

        /**
         * Sets the event timestamp.
         * @param timestamp event timestamp.
         * @return {@code this Builder}.
         */
        public Builder lastModified(final long timestamp)
        {
            mLastModified = timestamp;

            return (this);
        } // end of lastModified(long)

        /**
         * Sets the file length.
         * @param length file length.
         * @return {@code this Builder}.
         */
        public Builder length(final long length)
        {
            mLength = length;

            return (this);
        } // end of length(long)

        //
        // end of Set Methods.
        //-------------------------------------------------------
    } // end of class Builder
} // end of class EFileNotification




© 2015 - 2024 Weber Informatics LLC | Privacy Policy