Code simplification for java class