org.requirementsascode.SystemReaction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of requirementsascodecore Show documentation
Show all versions of requirementsascodecore Show documentation
Enables you to define and run executable use case specifications, in your code.
package org.requirementsascode;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* An instance of this class represents a system reaction as a function, even if it has been specified as
* a Consumer or Runnable by the user of the library. This approach simplifies development of the rest of the API.
*
* An instance of this class also allows access to the original object (i.e. Consumer, Runnable, or Function)
* specified via .system(..) or .systemPublish() by the user of the library.
* That element is called modelObject.
*
* @author b_muth
*
* @param the kind of message that is the input for this system reactions
*/
public class SystemReaction implements Function {
private Object modelObject;
private Function super T, ?> internalFunction;
SystemReaction(Consumer super T> modelObject) {
this.modelObject = Objects.requireNonNull(modelObject);
Function super T, Object> nonPublishingReaction = message -> {
modelObject.accept(message);
return null;
};
this.internalFunction = nonPublishingReaction;
}
SystemReaction(Runnable modelObject) {
this((Consumer super T>) ignoredRunner -> modelObject.run());
this.modelObject = modelObject;
}
SystemReaction(Supplier super T> modelObject) {
this.modelObject = Objects.requireNonNull(modelObject);
Function super T, Object> publishingReaction = (Function super T, Object>) message -> modelObject.get();
this.internalFunction = publishingReaction;
}
SystemReaction(Function super T, ?> modelObject) {
Objects.requireNonNull(modelObject);
this.modelObject = modelObject;
this.internalFunction = modelObject;
}
public Object getModelObject() {
return modelObject;
}
@Override
public Object apply(T message) {
return internalFunction.apply(message);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy