01: package net.innig.macker.example.conventions;
02:
03: import java.util.*;
04:
05: public final class RandomWalk {
06: public static void walk(Tree tree, int hops) {
07: while (hops-- > 0) {
08: System.out.println("hop: " + tree);
09: if (tree.getParent() != null && rand.nextInt(4) == 0)
10: tree = tree.getParent();
11: else {
12: List children = new ArrayList(tree.getChildren());
13: if (children.isEmpty())
14: tree = tree.getParent();
15: else
16: tree = (Tree) children.get(rand.nextInt(children
17: .size()));
18: }
19: if (tree == null)
20: return;
21: }
22: }
23:
24: private static Random rand = new Random();
25:
26: private RandomWalk() {
27: } // In the future, Macker will be able to require this, too
28: }
|