KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > recording > ForEachRecording


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2003 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.recording;
22
23 import net.innig.macker.event.*;
24 import net.innig.macker.rule.Rule;
25 import net.innig.macker.rule.RuleSet;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.util.*;
29
30 import org.jdom.Element;
31
32 import net.innig.collect.CollectionDiff;
33
34 public class ForEachRecording
35     extends EventRecording
36     {
37     public ForEachRecording(EventRecording parent)
38         {
39         super(parent);
40         iterations = new TreeMap();
41         }
42     
43     public EventRecording record(MackerEvent event)
44         {
45         if(!(event instanceof ForEachEvent))
46             return getParent().record(event);
47         
48         if(event instanceof ForEachStarted)
49             var = ((ForEachEvent) event).getForEach().getVariableName();
50         
51         if(event instanceof ForEachIterationStarted)
52             {
53             String JavaDoc value = ((ForEachIterationStarted) event).getVariableValue();
54             RuleSetRecording ruleSetRec = new RuleSetRecording(this);
55             iterations.put(value, ruleSetRec);
56             return ruleSetRec;
57             }
58         
59         if(event instanceof ForEachFinished)
60             return getParent();
61         
62         return this;
63         }
64         
65     public void read(Element elem)
66         {
67         var = elem.getAttributeValue("var");
68         for(Iterator childIter = elem.getChildren("iteration").iterator(); childIter.hasNext(); )
69             {
70             Element iterElem = (Element) childIter.next();
71             
72             String JavaDoc varValue = iterElem.getAttributeValue("value");
73             EventRecording iter = new RuleSetRecording(this);
74             iter.read(iterElem);
75             iterations.put(varValue, iter);
76             }
77         }
78     
79     public boolean compare(EventRecording actual, PrintWriter JavaDoc out)
80         {
81         if(!super.compare(actual, out))
82             return false;
83         
84         boolean match = true;
85         
86         ForEachRecording actualForEach = (ForEachRecording) actual;
87         if(!var.equals(actualForEach.var))
88             {
89             out.println("Expected " + this + ", but got " + actual);
90             match = false;
91             }
92         
93         CollectionDiff diff = new CollectionDiff(iterations.keySet(), actualForEach.iterations.keySet());
94         if(!diff.getRemoved().isEmpty())
95             out.println(this + ": missing iterations: " + diff.getRemoved());
96         if(!diff.getAdded().isEmpty())
97             out.println(this + ": unexpected iterations: " + diff.getAdded());
98         match = match && diff.getRemoved().isEmpty() && diff.getAdded().isEmpty();
99         
100         for(Iterator i = diff.getSame().iterator(); i.hasNext(); )
101             {
102             String JavaDoc varValue = (String JavaDoc) i.next();
103 // out.println("(comparing " + var + "=" + varValue + ")");
104
RuleSetRecording iterExpected = (RuleSetRecording) iterations.get(varValue);
105             RuleSetRecording iterActual = (RuleSetRecording) actualForEach.iterations.get(varValue);
106             match = iterExpected.compare(iterActual, out) && match;
107             }
108         return match;
109         }
110     
111     public String JavaDoc toString()
112         { return "[foreach:" + var + "]"; }
113     
114     public void dump(PrintWriter JavaDoc out, int indent)
115         {
116         super.dump(out, indent);
117         for(Iterator entryIter = iterations.entrySet().iterator(); entryIter.hasNext(); )
118             {
119             Map.Entry entry = (Map.Entry) entryIter.next();
120             for(int n = -3; n < indent; n++)
121                 out.print(' ');
122             out.println("[iteration:" + entry.getKey() + "]");
123             ((EventRecording) entry.getValue()).dump(out, indent+6);
124             }
125         }
126     
127     private String JavaDoc var;
128     private Map/*<String,RuleSetRecording>*/ iterations;
129     }
130
131
132
133
Popular Tags