* An action is performed when a {@link Trigger} determines that two given
* {@link State}s of a {@link Query} signify a change.
*
- * @param <S>
- * The type of the state
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public interface Action<S extends State> {
+public interface Action {
/**
* Performs the action.
* @param previousState
* The previous state of the system
*/
- void execute(S currentState, S previousState);
+ void execute(State currentState, State previousState);
}
/**
* A query is used to retrieve the current {@link State} of a system.
*
- * @param <S>
- * The type of the state
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public interface Query<S extends State> {
+public interface Query {
/**
* Retrieves the current state of the system. The returned state is never
*
* @return The current state of the system.
*/
- public S state();
+ public State state();
}
* A {@code Reaction} binds together {@link Query}s, {@link Trigger}s, and
* {@link Action}s, and it stores the intermediary {@link State}s.
*
- * @param <S>
- * The type of the state
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class Reaction<S extends State> {
+public class Reaction {
/** The query to run. */
- private final Query<S> query;
+ private final Query query;
/** The trigger to detect changes. */
- private final Trigger<S> trigger;
+ private final Trigger trigger;
/** The action to perform. */
- private final Action<S> action;
-
- /** The current state of the query. */
- private S currentState;
-
- /** The previous state of the query. */
- private S previousState;
+ private final Action action;
/** The interval in which to run queries (in milliseconds). */
private long updateInterval;
* @param action
* The action to perform
*/
- public Reaction(Query<S> query, Trigger<S> trigger, Action<S> action) {
+ public Reaction(Query query, Trigger trigger, Action action) {
this.query = query;
this.trigger = trigger;
this.action = action;
*
* @return The query to run
*/
- public Query<S> query() {
+ public Query query() {
return query;
}
*
* @return The trigger to detect changes
*/
- public Trigger<S> trigger() {
+ public Trigger trigger() {
return trigger;
}
*
* @return The action to perform
*/
- public Action<S> action() {
+ public Action action() {
return action;
}
/**
- * Returns the current state of the query.
- *
- * @return The current state of the query
- */
- public S currentState() {
- return currentState;
- }
-
- /**
- * Returns the previous state of the query.
- *
- * @return The previous state of the query
- */
- public S previousState() {
- return previousState;
- }
-
- /**
* Returns the update interval of this reaction.
*
* @return The update interval of this reaction (in milliseconds)
* The update interval of this reaction (in milliseconds)
* @return This reaction
*/
- public Reaction<?> setUpdateInterval(long updateInterval) {
+ public Reaction setUpdateInterval(long updateInterval) {
this.updateInterval = updateInterval;
return this;
}
* sizes but a trigger might only care about whether the file appeared or
* disappeared since the last check.
*
- * @param <S>
- * The type of the state
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public interface Trigger<S extends State> {
+public interface Trigger {
/**
* Checks whether the given states warrant a change trigger.
* @return {@code true} if the given states warrant a change trigger,
* {@code false} otherwise
*/
- boolean triggers(S currentState, S previousState);
+ boolean triggers(State currentState, State previousState);
}
import java.io.File;
import net.pterodactylus.reactor.Query;
+import net.pterodactylus.reactor.State;
import net.pterodactylus.reactor.states.FileState;
/**
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class FileQuery implements Query<FileState> {
+public class FileQuery implements Query {
/** The name of the file to query. */
private final String filename;
* {@inheritDoc}
*/
@Override
- public FileState state() {
+ public State state() {
File file = new File(filename);
if (!file.exists()) {
return new FileState(false, false, -1, -1);
package net.pterodactylus.reactor.triggers;
+import net.pterodactylus.reactor.State;
import net.pterodactylus.reactor.Trigger;
import net.pterodactylus.reactor.states.FileState;
+import com.google.common.base.Preconditions;
+
/**
* A trigger that detects changes in the existence of a file.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public class FileExistenceTrigger implements Trigger<FileState> {
+public class FileExistenceTrigger implements Trigger {
//
// TRIGGER METHODS
* {@inheritDoc}
*/
@Override
- public boolean triggers(FileState previousState, FileState currentState) {
- return previousState.exists() != currentState.exists();
+ public boolean triggers(State previousState, State currentState) {
+ Preconditions.checkState(previousState instanceof FileState, "previousState is not a FileState");
+ Preconditions.checkState(currentState instanceof FileState, "currentState is not a FileState");
+ return ((FileState) previousState).exists() != ((FileState) currentState).exists();
}
}