KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > apples > samples > HanoiApple


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.flow.apples.samples;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Stack JavaDoc;
21
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.components.flow.apples.AppleController;
25 import org.apache.cocoon.components.flow.apples.AppleRequest;
26 import org.apache.cocoon.components.flow.apples.AppleResponse;
27
28 /**
29  * HanoiApple shows an apple maintaining the state of the fanous puzzle.
30  */

31 public class HanoiApple extends AbstractLogEnabled implements AppleController {
32
33     public static final int NONE = -1;
34     public static final int SRC = 0;
35     public static final int AUX = 1;
36     public static final int DST = 2;
37     
38     // full state of the puzzle is in the following variables
39
public Stack JavaDoc[] stacks;
40     public Object JavaDoc floatingDisk = null;
41     public int moves = 0;
42     public int puzzleSize = 0;
43
44
45     public String JavaDoc toString() {
46         return "HanoiApple[ stacks=" + this.stacks + " | floatingDisk=" + this.floatingDisk
47                 + " | moves = " + this.moves + "]";
48     }
49
50     
51     public void process(AppleRequest req, AppleResponse res) throws ProcessingException {
52
53         // processing
54
if (stacks == null) {
55             String JavaDoc requestSize = req.getCocoonRequest().getParameter("size");
56             if (requestSize != null) {
57                 try {
58                     int size = Integer.parseInt(requestSize);
59                     intializeStacks(size);
60                 } catch (NumberFormatException JavaDoc ignore) {
61                 }
62             }
63         } else {
64             // decide selected column
65
String JavaDoc requestStack = req.getCocoonRequest().getParameter("stack");
66             if (requestStack != null) {
67                 try {
68                     int stackNdx = Integer.parseInt(requestStack);
69                     if (this.floatingDisk != null) {
70                         // we are in the middle of a move --> complete move if it is allowed
71
if ( this.stacks[stackNdx].size() == 0
72                             || ((Integer JavaDoc)this.floatingDisk).intValue() < ((Integer JavaDoc)this.stacks[stackNdx].peek()).intValue()) {
73                                 
74                             this.stacks[stackNdx].push(this.floatingDisk);
75                             this.floatingDisk = null;
76                             this.moves++;
77                         }
78                     } else {
79                         if (this.stacks[stackNdx].size() != 0) {
80                             this.floatingDisk = this.stacks[stackNdx].pop();
81                         }
82                     }
83                 } catch (RuntimeException JavaDoc ignore) {
84                    //NUMBERFORMAT
85
//ARRAYINDEXOUTOFBOUNDS
86
}
87             }
88         }
89
90         getLogger().debug(toString());
91
92         //view generation
93
if (stacks == null) {
94             res.sendPage("hanoi/intro.jx", null);
95         } else {
96             Map JavaDoc bizdata = new HashMap JavaDoc();
97             bizdata.put("stacks" , this.stacks);
98             bizdata.put("moves" , "" + this.moves);
99             bizdata.put("floatingDisk", this.floatingDisk);
100             bizdata.put("nextMove" , this.floatingDisk==null ? "Lift it!" : "Drop it!");
101             bizdata.put("puzzleSize" , "" + this.puzzleSize);
102
103             res.sendPage("hanoi/hanoi.jx", bizdata);
104         }
105
106     }
107
108
109     private void intializeStacks(int size) {
110         if (size > 2) {
111             this.stacks = new Stack JavaDoc[3];
112             for (int i = 0; i < 3; i++) {
113                 this.stacks[i] = new Stack JavaDoc();
114             }
115             for (int i = size; i > 0; i--) {
116                 this.stacks[0].push(new Integer JavaDoc(i));
117             }
118             this.puzzleSize = size;
119         }
120     }
121
122 }
123
Popular Tags