Fix #1890 "NPE with low XForms cache size"
Use BitSet in IdGenerator

- move to Scala
- move conditional code for testing duplicates

See also #1890 and #1892.