I'm developing a Java 8 + JSF application, where all the view classes extend from an abstract class called Navegable.
All the methods called from the view and intended to modify the DB in any way, have to add an information message if everything goes OK or an error message otherwise. This involves wrapping many of my methods in a try-catch clause. In order to avoid that, I've tought I could use functional programming and prepare some generic function in the parent class instead. This way:
public abstract class Navegable implements Serializable {
protected FacesUtils facesUtils;
protected void execute(Function<Void, Void> function) {
try {
function.apply(null);
facesUtils.addInfoMessage("PROPERLY_SAVED");
} catch (Exception ex) {
facesUtils.addErrorMessage("ERROR");
throw ex;
}
}
public Navegable(FacesUtils facesUtils) {
super();
this.facesUtils = facesUtils;
}
}
Then I'd have to call it from each of my methods modifying the DB in children classes:
public class NavegableEquipment extends Navegable {
public void assignWorker() {
execute(new Function<Void, Void>() {
@Override
public Void apply(Void t) {
assignWorker(selectedEquipment);
reloadEquipmentById(selectedEquipment.get_Id());
return null;
}
});
}
//More methods
}
However, the caller method still looks like a bit verbose in its implementation. Do I have any other choice or am I limited by the language itself?