1 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 ("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 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 |