Convert to java and added tests