Updating java code snippet and refactoring