]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7820282802aedf6f0aaea40c09381226cb6defa6
[tmp/jakarta-migration.git] /
1
2 package org.collectionspace.services.id.part;
3
4 import java.util.Random;
5
6 public class JavaRandomNumberIDPartAlgorithm implements IDPartAlgorithm {
7
8     // @TODO Verify whether this simple singleton pattern is
9     // achieving the goal of using a single instance of the random
10     // number generator.
11
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.
15
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
18     // java.util.Random.
19
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();
24
25     private JavaRandomNumberIDPartAlgorithm() {
26     }
27
28     // See http://en.wikipedia.org/wiki/Singleton_pattern
29     private static class SingletonHolder {
30         private static final JavaRandomNumberIDPartAlgorithm INSTANCE =
31             new JavaRandomNumberIDPartAlgorithm();
32     }
33
34     public static JavaRandomNumberIDPartAlgorithm getInstance() {
35         return SingletonHolder.INSTANCE;
36     }
37
38     // @TODO Allow setting a maximum value.
39     @Override
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));
44     }
45
46 }