You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Registry.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. Registry.java
  13. Part of the Spacewar system.
  14. */
  15. package spacewar;
  16. import java.util.Vector;
  17. import java.util.Hashtable;
  18. import java.util.Enumeration;
  19. /**
  20. * The Registry keeps track of all the space objects that are floating around.
  21. * It basically supports register, unregister and contents type operations.
  22. *
  23. * The synchronization is done by the RegistrySynchronization aspect.
  24. */
  25. class Registry {
  26. private Hashtable table;
  27. private Game game;
  28. Game getGame() { return game; }
  29. Registry (Game theGame) {
  30. game = theGame;
  31. table = new Hashtable();
  32. }
  33. void register(SpaceObject object) {
  34. table.put(object, object);
  35. }
  36. void unregister(SpaceObject object) {
  37. table.remove(object);
  38. }
  39. /*
  40. * It is an invariant of the design that only two points in SpaceObject
  41. * should call register and unregister. This aspect enforces that.
  42. *
  43. * Unfortunately, in the current compiler, we get a static warning when
  44. * there are no illegal calls that this advice has no targets. That will
  45. * be fixed in a future release. For the time being the dummy method
  46. * just below this fixes that.
  47. */
  48. static aspect RegistrationProtection {
  49. after() returning():
  50. (call(void Registry.register(SpaceObject)) ||
  51. call(void Registry.unregister(SpaceObject))) &&
  52. !(within(SpaceObject) && (withincode(new(..)) ||
  53. withincode(void die()))) {
  54. throw new IllegalAccessError(
  55. "This is an illegal call to " + thisJoinPoint + "\n" +
  56. "Only the constructor and the die() on SpaceObject\n" +
  57. "should call the primitive registry operations.");
  58. }
  59. }
  60. void dummy() { // see comment above
  61. register(getObjects()[0]);
  62. unregister(getObjects()[0]);
  63. }
  64. SpaceObject[] getObjects() {
  65. SpaceObject[] allObjects = new SpaceObject[table.size()];
  66. Enumeration elements = table.elements();
  67. for(int i = 0; elements.hasMoreElements(); i++) {
  68. allObjects[i] = (SpaceObject)(elements.nextElement());
  69. }
  70. return allObjects;
  71. }
  72. Ship[] getShips() {
  73. //
  74. // First we have to put just the Ships into a vector, then we can put
  75. // them into an array of exactly the right length.
  76. //
  77. Ship[] arrayOfShips;
  78. Vector vectorOfShips = new Vector();
  79. Enumeration elements = table.elements();
  80. while (elements.hasMoreElements()) {
  81. Object object = elements.nextElement();
  82. if (object instanceof Ship) {
  83. vectorOfShips.addElement(object);
  84. }
  85. }
  86. arrayOfShips = new Ship[(vectorOfShips.size())];
  87. vectorOfShips.copyInto(arrayOfShips);
  88. return arrayOfShips;
  89. }
  90. Hashtable getTable() { return table; }
  91. //
  92. // The protocol for clockTick is that it automatically cascades.
  93. //
  94. void clockTick() {
  95. Enumeration elements = table.elements();
  96. while (elements.hasMoreElements()) {
  97. ((SpaceObject)elements.nextElement()).clockTick();
  98. }
  99. }
  100. }