Guest User

Untitled

a guest
Jul 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public class IntIDGenerator {
  2.  
  3. private static int sequence = 1;
  4.  
  5. public synchronized static int generate(){
  6. sequence++;
  7. return sequence;
  8. }
  9.  
  10. }
  11.  
  12. public final class SDGIntIDGenerator {
  13.  
  14. private static final AtomicInteger sequence = new AtomicInteger(1);
  15.  
  16. private SDGIntIDGenerator() {}
  17.  
  18. public static int generate(){
  19. return sequence.getAndIncrement();
  20. }
  21.  
  22. }
  23.  
  24. public final class SDGIntIDGenerator {
  25.  
  26. private static final ConcurrentHashMap<Class,AtomicInteger> mapper = new ConcurrentHashMap<Class,AtomicInteger>();
  27.  
  28. private SDGIntIDGenerator () {}
  29.  
  30. public static int generateId (Class _class) {
  31. mapper.putIfAbsent(_class, new AtomicInteger(1));
  32. return mapper.get(_class).getAndIncrement();
  33. }
  34. }
Add Comment
Please, Sign In to add comment