fork download
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.CopyOnWriteArrayList;
  7.  
  8. class Ideone {
  9.  
  10. private static final Map<Class<?>, List<Object>> entities
  11. = new HashMap<>();
  12.  
  13. public static void main(String[] args) {
  14. SomeEnemy firstSomeEnemy = new SomeEnemy();
  15. add(firstSomeEnemy);
  16.  
  17. SomeEnemy secondSomeEnemy = new SomeEnemy();
  18. add(secondSomeEnemy);
  19.  
  20. AnotherEnemy firstAnotherEnemy = new AnotherEnemy();
  21. add(firstAnotherEnemy);
  22.  
  23. AnotherEnemy secondAnotherEnemy = new AnotherEnemy();
  24. add(secondAnotherEnemy);
  25.  
  26. String stringOne = "stringOne";
  27. add(stringOne);
  28.  
  29. String stringTwo = "stringTwo";
  30. add(stringTwo);
  31.  
  32. List<SomeEnemy> fetchOne = getAll(SomeEnemy.class);
  33. SomeEnemy firstFetched = fetchOne.get(0);
  34. fetchOne.add(new SomeEnemy());
  35. System.out.println(firstFetched == firstSomeEnemy);
  36.  
  37. remove(firstFetched);
  38. List<SomeEnemy> fetchTwo = getAll(SomeEnemy.class);
  39. SomeEnemy secondFetched = fetchTwo.get(0);
  40. System.out.println(secondFetched == secondSomeEnemy);
  41.  
  42. remove(secondSomeEnemy);
  43. List<SomeEnemy> fetchThree = getAll(SomeEnemy.class);
  44. System.out.println(fetchThree.isEmpty());
  45.  
  46. List<AnotherEnemy> fetchFour = getAll(AnotherEnemy.class);
  47. AnotherEnemy fourthFetched = fetchFour.get(0);
  48. System.out.println(fourthFetched == firstAnotherEnemy);
  49.  
  50. remove(firstAnotherEnemy);
  51. List<AnotherEnemy> fetchFive = getAll(AnotherEnemy.class);
  52. AnotherEnemy fifthFetched = fetchFive.get(0);
  53. System.out.println(fifthFetched == secondAnotherEnemy);
  54.  
  55. remove(secondAnotherEnemy);
  56. List<AnotherEnemy> fetchSix = getAll(AnotherEnemy.class);
  57. System.out.println(fetchSix.isEmpty());
  58.  
  59. List<String> fetchSeven = getAll(String.class);
  60. String seventFetched = fetchSeven.get(0);
  61. System.out.println(seventFetched.equals(stringOne));
  62.  
  63. remove(stringOne);
  64. List<String> fetchEight = getAll(String.class);
  65. String eightFetched = fetchEight.get(0);
  66. System.out.println(eightFetched.equals(stringTwo));
  67.  
  68. remove(stringTwo);
  69. List<String> ninthFetch = getAll(String.class);
  70. System.out.println(ninthFetch.isEmpty());
  71. }
  72.  
  73. static void add(Object entity) {
  74. Class<?> type = entity.getClass();
  75.  
  76. if (!entities.containsKey(type)) {
  77. entities.put(type, new CopyOnWriteArrayList<>());
  78. }
  79.  
  80. entities.get(type).add(entity);
  81. }
  82.  
  83. private static <T> List<T> getAll(Class<? extends T> type) {
  84. return new ArrayList<>(getListCasted(type));
  85. }
  86.  
  87. @SuppressWarnings("unchecked")
  88. private static <T> List<T> getListCasted(Class<? extends T> type) {
  89. return (List<T>) entities.getOrDefault(type, Collections.emptyList());
  90. }
  91.  
  92. public static void remove(Object entity) {
  93. getListCasted(entity.getClass()).remove(entity);
  94. }
  95. }
  96.  
  97. interface Entity {}
  98. interface Enemy extends Entity {}
  99.  
  100. class SomeEnemy implements Enemy {}
  101. class AnotherEnemy implements Enemy {}
Success #stdin #stdout 0.06s 32456KB
stdin
Standard input is empty
stdout
true
true
true
true
true
true
true
true
true