2 package org.collectionspace.services.id.part;
4 import java.util.Random;
6 public class JavaRandomNumberIDPartAlgorithm implements IDPartAlgorithm {
8 // @TODO Verify whether this simple singleton pattern is
9 // achieving the goal of using a single instance of the random
12 // @TODO Check whether we might need to store a serialization
13 // of this class, once instantiated, between invocations, and
14 // load the class from its serialized state.
16 // @TODO Look into whether we may have some user stories or use cases
17 // that require the use of java.security.SecureRandom, rather than
20 // Starting with Java 5, the default instantiation of Random()
21 // sets the seed "to a value very likely to be distinct from any
22 // other invocation of this constructor."
23 private Random r = new Random();
25 private JavaRandomNumberIDPartAlgorithm() {
28 // See http://en.wikipedia.org/wiki/Singleton_pattern
29 private static class SingletonHolder {
30 private static final JavaRandomNumberIDPartAlgorithm INSTANCE =
31 new JavaRandomNumberIDPartAlgorithm();
34 public static JavaRandomNumberIDPartAlgorithm getInstance() {
35 return SingletonHolder.INSTANCE;
38 // @TODO Allow setting a maximum value.
40 public String generateID(){
41 // Returns a value between 0 (inclusive) and the
42 // maximum value of an int.
43 return Integer.toString(r.nextInt(Integer.MAX_VALUE));