KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > event > ThrowingListener


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002-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.event;
22
23 import net.innig.macker.rule.RuleSet;
24 import net.innig.macker.rule.RuleSeverity;
25
26 import java.util.*;
27
28 public class ThrowingListener
29     implements MackerEventListener
30     {
31     public ThrowingListener(
32             RuleSeverity throwOnFirstThreshold,
33             RuleSeverity throwOnFinishThreshold)
34         {
35         this.throwOnFirstThreshold = throwOnFirstThreshold;
36         this.throwOnFinishThreshold = throwOnFinishThreshold;
37         clear();
38         }
39     
40     public void mackerStarted(RuleSet ruleSet)
41         {
42         if(ruleSet.getParent() == null)
43             {
44             if(inUse)
45                 throw new IllegalStateException JavaDoc("This ThrowingListener is already in use");
46             inUse = true;
47             }
48         }
49     
50     public void mackerFinished(RuleSet ruleSet)
51         throws MackerIsMadException
52         {
53         if(ruleSet.getParent() == null)
54             inUse = false;
55         }
56
57     public void mackerAborted(RuleSet ruleSet)
58         { events = null; }
59     
60     public void handleMackerEvent(RuleSet ruleSet, MackerEvent event)
61         throws MackerIsMadException
62         {
63         if(event instanceof ForEachEvent)
64             return;
65         
66         RuleSeverity severity = event.getRule().getSeverity();
67         if(maxSeverity == null || severity.compareTo(maxSeverity) >= 0)
68             maxSeverity = severity;
69
70         if(throwOnFinishThreshold != null && severity.compareTo(throwOnFinishThreshold) >= 0)
71             events.add(event);
72
73         timeToGetMad(throwOnFirstThreshold);
74         }
75     
76     public void timeToGetMad(RuleSeverity threshold)
77         throws MackerIsMadException
78         {
79         if(threshold != null && maxSeverity != null && maxSeverity.compareTo(threshold) >= 0)
80             timeToGetMad();
81         }
82
83     public void timeToGetMad()
84         throws MackerIsMadException
85         {
86         if(!events.isEmpty())
87             throw new MackerIsMadException(events);
88         }
89     
90     public void clear()
91         { events = new LinkedList(); }
92     
93     public String JavaDoc toString()
94         { return "ThrowingListener"; }
95     
96     private final RuleSeverity throwOnFirstThreshold, throwOnFinishThreshold;
97     private RuleSeverity maxSeverity;
98     private List events;
99     private boolean inUse;
100     }
101
Popular Tags