3 import java.util.HashMap;
4 import java.util.HashSet;
8 import jalview.bin.Console;
13 * id generating tools avoiding Random.nextLong() for JalviewJS. Avoids
17 public static enum IdType
22 private static int count = 0;
24 private static Map<IdType, Set<Long>> typeMap = new HashMap<>();
28 for (IdType t : IdType.values())
30 typeMap.put(t, new HashSet<>());
34 public static long newId()
36 return newId(IdType.GENERAL, null);
39 public static long newId(IdType t)
41 return newId(t, null);
44 public static long newId(IdType t, Object o)
46 Set<Long> idSet = typeMap.get(t);
50 // get a new hashCode -- not tied to an object.
51 // Adding Integer.MAX_VALUE should avoid collisions with object generated
53 newId = Integer.MAX_VALUE + t.hashCode() + System.currentTimeMillis()
55 while (idSet.contains(newId))
62 // generate the hashcode tied to this object for this type
63 newId = t.hashCode() + o.hashCode();
64 if (idSet.contains(newId))
66 Console.debug("Using an existing id for Type " + t.name()
67 + " and object " + o.toString() + ": '" + newId + "'");
78 public static void NOTremoveId(IdType t, Object o)
84 Set<Long> idSet = typeMap.get(t);
85 long id = t.hashCode() + o.hashCode();
89 public static void NOTremoveId(IdType t, long id)
91 Set<Long> idSet = typeMap.get(t);