blob: 42be102f19ef6b069536fe946385d10d2f580a4b (
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
|
import java.util.*;
import org.aspectj.lang.annotation.*;
class Blob {}
public aspect TheBigOne extends ParentChildRelationship<Blob,Blob> {
public static void main(String []argv) {
Blob a = new Blob();
Blob b = new Blob();
Blob c = new Blob();
Blob d = new Blob();
Blob e = new Blob();
// arrange as follows: A contains B,C,D and B contains E
a.addChild(b);
a.addChild(c);
a.addChild(d);
b.addChild(e);
// now query the layout
if (!e.getParent().equals(b))
throw new RuntimeException("why is E not parent of B? "+e.getParent());
if (!d.getParent().equals(a))
throw new RuntimeException("why is A not parent of D? "+d.getParent());
if (a.getChildren().size()!=3)
throw new RuntimeException("A should have 3 children, not:"+a.getChildren().size());
}
}
abstract aspect ParentChildRelationship<Parent,Child> {
interface ParentHasChildren<C>{}
interface ChildHasParent<P>{}
declare parents: Parent implements ParentHasChildren<Child>;
declare parents: Child implements ChildHasParent<Parent>;
public List<E> ParentHasChildren<E>.children = new ArrayList<E>();
public P ChildHasParent<P>.parent;
public List<D> ParentHasChildren<D>.getChildren() {
return Collections.unmodifiableList(children);
}
public P ChildHasParent<P>.getParent() {
return parent;
}
public void ChildHasParent<R>.setParent(R parent) {
this.parent = parent;
ParentHasChildren phc = (ParentHasChildren)parent;
if (phc.getChildren().contains(this))
phc.addChild(this);
}
public void ParentHasChildren<X>.addChild(X child) {
if (((ChildHasParent)child).parent != null) {
((ParentHasChildren)((ChildHasParent)child).parent).removeChild(child);
} else {
((ChildHasParent)child).setParent((ParentHasChildren)this);
}
children.add(child);
}
public void ParentHasChildren<Y>.removeChild(Y child) {
if (children.remove(child)) {
((ChildHasParent)child).parent = null;
}
}
}
|