Operator Overloading Hack in Java, or o24j 📎
I could write of course a custom language for this purpose, but because of my laziness I just passed the objects as well as the formula to groovy, which interpreted and executed the formula for me.
During the review of the new JSR-310 (the Date And Time API for Java) and especially the fluent api, I got the next idea: simulation of the operator overloading for Java-Objects.
So I wanted to do something like this:
BigDecimal two = new BigDecimal(2);
BigDecimal three = new BigDecimal(3);
BigDecimal minusOne = Overload.eval("p1-p2", two,three);
assertEquals(-1,minusOne.intValue());
I materialized the idea in the o24j opensource project. It already works - the sources are checked in.
I use groovy internally because it already provides operator overloading. I would like to do the same with JavaScript.
Then no additional libraries would be required.
o24j works with JSR-310 as well :-).