01: package org.kohsuke.rngom.digested;
02:
03: import java.util.Iterator;
04:
05: /**
06: * A pattern that can contain other patterns.
07: *
08: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
09: */
10: public abstract class DContainerPattern extends DPattern implements
11: Iterable<DPattern> {
12: private DPattern head;
13: private DPattern tail;
14:
15: public DPattern firstChild() {
16: return head;
17: }
18:
19: public DPattern lastChild() {
20: return tail;
21: }
22:
23: public int countChildren() {
24: int i = 0;
25: for (DPattern p = firstChild(); p != null; p = p.next)
26: i++;
27: return i;
28: }
29:
30: public Iterator<DPattern> iterator() {
31: return new Iterator<DPattern>() {
32: DPattern next = head;
33:
34: public boolean hasNext() {
35: return next != null;
36: }
37:
38: public DPattern next() {
39: DPattern r = next;
40: next = next.next;
41: return r;
42: }
43:
44: public void remove() {
45: throw new UnsupportedOperationException();
46: }
47: };
48: }
49:
50: void add(DPattern child) {
51: if (tail == null) {
52: child.prev = child.next = null;
53: head = tail = child;
54: } else {
55: child.prev = tail;
56: tail.next = child;
57: child.next = null;
58: tail = child;
59: }
60: }
61: }
|