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