-
Notifications
You must be signed in to change notification settings - Fork 0
Custom Methods
This is the page on how to create custom methods. For the Java Documentation, go to JavaDoc@FlowentFunction
You may have heard about the fact that Flowent supports the creation of external methods and the ability to call them from within the method chain, but you didn't here about how to do that. Wether you're just scrolling around or you clicked on a link that took you here, you are about to learn how to make custom methods, compatible with the Flowent method chain.
To start off, we will be using an interface called FlowentFunction. It is the object that you must pass in in order to run a custom method. So to start off, we'll need another import from the flowent package. Go ahead and add:
import flowent.FlowentFunction;Now were ready!
Let's enter this into the class, outside of any methods, and we'll run through it together:
private FlowentFunction helloWorld = new FlowentFunction() {
@Override
public void execute(Flowent f) throws Exception {
f.println("Hello, world!");
}
};Alright, let's run through this custom method! First line!
private FlowentFunction helloWorld = new FlowentFunction() { ... };In this line, we create a new variable called helloWorld of type FlowentFunction. Not very interesting, but the next few lines are!
@Override
public void execute(Flowent f) throws Exception { ... }These few line override the method execute(Flowent) in type FlowentFunction. This is the method that is called when you call your custom function through the method chain. The Flowent object that is passed in is the same Flowent object that called the execute method. Don't worry about the throws Exception part. That will be explained in the Exception Handling section.
f.println("Hello, world!");If you aren't familiar with this line yet, then you need to take it back a few steps. This line prints the string "Hello, world!" to the console and then terminates the line. How about that? Other than that, this line needs no explaining.
REMEMBER! We are still defining a variable, so you need to add a semicolon (;) to the end.
If you have been looking through the source code, you may have noticed another method in the FlowentFunction interface. This method is called onError(Flowent f, Exception e, String location). This is what that throws Exception part was for by the way. I think this is also a good time to say that error handling is already defaulted in the FlowentFunction interface, and that you don't have to use every single parameter you are given. So don't freak out.
By default, the onError method looks just like an actual exception in the console, except for one thing; Flowent revives itself afterwards. So you can still see what else fails in the program and not immediately quit. Afterwards, checking the console for information about each problem.
Let's walk through what is passed in for each argument:
- Flowent f - The object that tried to execute the method, but failed.
- Exception e - The cause of the problem. Use
instanceofto check for specific exception types. - String location - The location where the method was called in the first place.
- e.g.
flowent.helloworld.MyClass.<init>(MyClass.java:30)
- e.g.
Now, with this information, you should be able to create custom error logs with a breeze!
Just remember, you can still use try catches in your execute() method.
Note: The first exception found will be the one reported.
If you have studied Java enough, you will know that there is more to exceptions than just exceptions. Exception extends Throwable. But there is another class that extends Throwable. The Error class. There are all sorts of these errors. But the main point is that no good application should actually try to catch one. Yes, they are catchable, but they are generally thrown by the JVM for things like deep recursion or being out of memory.
The point here is, that if you get an error, maybe you should listen to it as opposed to trying to avoid it.
Yes, you may have learned how to make a custom method, but you still don't know how to run it from within the method chain! It's simple.
Use the method:
Flowent.runMethod(myMethod); // Do not use this in a static context.In this code, myMethod is the FlowentFunction that we are referring to. Like the comment says, don't use the method in a static context. By that, I mean don't use Flowent. At the very least, use new Flowent() instead.
Do you have a bug report or feature request you want to file? File an issue here!