net.sf.eBusx.io.EFileNotification Maven / Gradle / Ivy
//
// Copyright 2013, 2016, 2019, 2020 Charles W. Rapp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
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;
import net.sf.eBus.util.Validator;
/**
* 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 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