draft implementation
just a naive translation from Go code to Java