Segment Tree with custom function implemented in Java