Id { public static synchronized int pollId(final Class<?> clazz) { Class<?> matchClass = null; if (COUNTERS.containsKey(clazz)) { matchClass = clazz; } else if (!NO_COUNTERS.contains(clazz)) { for (Class<?> key : COUNTERS.keySet()) { if (key.isAssignableFrom(clazz)) { matchClass = key; break; } } } int result = -1; if (matchClass == null) { NO_COUNTERS.add(clazz); result = pollGlobalId(); } else { result = COUNTERS.get(matchClass); COUNTERS.put(matchClass, result + 1); } if (result < 0) { throw new IllegalStateException("The generated id for class:" + clazz.getName() + " is negative. Possible integer overflow."); } return result; } private  Id(); static synchronized int pollId(final Class<?> clazz);  }
@Test public void testPollId() { Set<Integer> cloudletIds = new HashSet<>(); cloudletIds.add(Id.pollId(Cloudlet.class)); cloudletIds.add(Id.pollId(Cloudlet.class)); cloudletIds.add(Id.pollId(CloudLetA.class)); cloudletIds.add(Id.pollId(CloudLetA.class)); cloudletIds.add(Id.pollId(CloudLetB.class)); assertEquals(5, cloudletIds.size()); Set<Integer> ids = new HashSet<>(); ids.add(Id.pollId(X.class)); ids.add(Id.pollId(Y.class)); ids.add(Id.pollId(Object.class)); ids.add(Id.pollId(X.class)); ids.add(Id.pollId(String.class)); ids.add(Id.pollId(Y.class)); assertEquals(6, ids.size()); }