Java: Unification of functions