Coverage Report - org.mule.util.timer.EventTimerTask
 
Classes in this File Line Coverage Branch Coverage Complexity
EventTimerTask
0%
0/36
0%
0/14
1.455
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.util.timer;
 8  
 
 9  
 import java.util.ArrayList;
 10  
 import java.util.List;
 11  
 import java.util.TimerTask;
 12  
 
 13  
 /**
 14  
  * <code>EventTimerTask</code> is a task that causes TimeEvent to be fired to
 15  
  * listening objects when a specific number of milliseconds have passed. This
 16  
  * implementation is based on the java.util.TimerTask.
 17  
  */
 18  
 public class EventTimerTask extends TimerTask
 19  
 {
 20  
     /**
 21  
      * A list of listeners on this task
 22  
      */
 23  0
     private List listeners = null;
 24  
 
 25  
     /**
 26  
      * The name of the task
 27  
      */
 28  0
     private String name = null;
 29  
 
 30  
     /**
 31  
      * Determines if the task has been started
 32  
      */
 33  0
     private boolean started = true;
 34  
 
 35  
     /**
 36  
      * Constructs a EventTimeTask and registers a listener with it
 37  
      * 
 38  
      * @param listener the listener to register
 39  
      */
 40  
     public EventTimerTask(TimeEventListener listener)
 41  
     {
 42  0
         super();
 43  0
         addListener(listener);
 44  0
         this.name = "EventTimerTask." + hashCode();
 45  0
     }
 46  
 
 47  
     /**
 48  
      * Constructs a EventTimeTask and registers a listener with it
 49  
      * 
 50  
      * @param listener the listener to register
 51  
      * @param name the name for the task
 52  
      */
 53  
     public EventTimerTask(TimeEventListener listener, String name)
 54  
     {
 55  0
         super();
 56  0
         addListener(listener);
 57  0
         this.name = name;
 58  0
     }
 59  
 
 60  
     /**
 61  
      * The action to be performed by this timer task. The fireTime event method is
 62  
      * called.
 63  
      */
 64  
     public void run()
 65  
     {
 66  
 
 67  0
         TimeEvent event = new TimeEvent(this, getName(), scheduledExecutionTime());
 68  0
         fireTimerEvent(event);
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Gets the task name (this is also the timer thread name)
 73  
      * 
 74  
      * @return the task name
 75  
      */
 76  
     public String getName()
 77  
     {
 78  0
         return name;
 79  
     }
 80  
 
 81  
     public void removeListener(TimeEventListener listener)
 82  
     {
 83  0
         if (listeners != null && listeners.contains(listener))
 84  
         {
 85  0
             listeners.remove(listener);
 86  
         }
 87  0
     }
 88  
 
 89  
     public void removeAllListeners()
 90  
     {
 91  0
         listeners = new ArrayList();
 92  0
     }
 93  
 
 94  
     public void addListener(TimeEventListener listener)
 95  
     {
 96  0
         if (listeners == null)
 97  
         {
 98  0
             listeners = new ArrayList();
 99  0
             listeners.add(listener);
 100  
         }
 101  0
         else if (!listeners.contains(listener))
 102  
         {
 103  0
             listeners.add(listener);
 104  
         }
 105  0
     }
 106  
 
 107  
     protected void fireTimerEvent(TimeEvent event)
 108  
     {
 109  0
         if (listeners != null && started)
 110  
         {
 111  0
             int count = listeners.size();
 112  0
             for (int i = 0; i < count; i++)
 113  
             {
 114  0
                 ((TimeEventListener) listeners.get(i)).timeExpired(event);
 115  
             }
 116  
         }
 117  0
     }
 118  
 
 119  
     public void stop()
 120  
     {
 121  0
         started = false;
 122  0
     }
 123  
 
 124  
     public void start()
 125  
     {
 126  0
         started = true;
 127  0
     }
 128  
 
 129  
     public boolean isStarted()
 130  
     {
 131  0
         return started;
 132  
     }
 133  
 }