IdCounter.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. /**
  4. * Static counter
  5. * ID-Counter for all Cps Objects.
  6. *
  7. * @author Gruppe14
  8. */
  9. public class IdCounter {
  10. @Expose
  11. private static int counterObjects = 1;
  12. @Expose
  13. private static int counterElements = 1;
  14. public enum CounterType {
  15. Object, Element
  16. }
  17. /**
  18. * Return the next ID and increment the ID counter by 1.
  19. * @return the next ID
  20. */
  21. public static synchronized int nextId(CounterType type) {
  22. switch(type){
  23. case Element:
  24. return counterObjects++;
  25. case Object:
  26. default:
  27. return counterElements++;
  28. }
  29. }
  30. /**
  31. * Return the Counter.
  32. *
  33. * @return the counter
  34. */
  35. public static int actualId(CounterType type) {
  36. switch(type){
  37. case Element:
  38. return counterObjects;
  39. case Object:
  40. default:
  41. return counterElements;
  42. }
  43. }
  44. /**
  45. * Set the Counter.
  46. *
  47. * @param counter
  48. * the counter to set
  49. */
  50. public static void setCounter(int counter, CounterType type) {
  51. switch(type){
  52. case Element:
  53. counterElements = counter;
  54. case Object:
  55. default:
  56. counterObjects = counter;
  57. }
  58. }
  59. /**
  60. * Reset the Counter.
  61. */
  62. public static void resetObjectCounter(CounterType type) {
  63. switch(type){
  64. case Element:
  65. counterObjects = 1;
  66. case Object:
  67. default:
  68. counterElements = 1;
  69. }
  70. }
  71. }