Coverage Report - us.daveread.basicquery.gui.InsertTime
 
Classes in this File Line Coverage Branch Coverage Complexity
InsertTime
92%
22/24
100%
1/1
1.6
 
 1  
 package us.daveread.basicquery.gui;
 2  
 
 3  
 import java.text.DecimalFormat;
 4  
 import java.text.NumberFormat;
 5  
 import java.util.Date;
 6  
 import javax.swing.JLabel;
 7  
 
 8  
 import org.apache.log4j.Logger;
 9  
 
 10  
 /**
 11  
  * <p>
 12  
  * Title: Insert time
 13  
  * </p>
 14  
  * <p>
 15  
  * Description: Places the count of elapsed seconds into a JLabel
 16  
  * </p>
 17  
  * <p>
 18  
  * Copyright: Copyright (c) 2004, David Read
 19  
  * </p>
 20  
  * <p>
 21  
  * This program is free software; you can redistribute it and/or modify it under
 22  
  * the terms of the GNU General Public License as published by the Free Software
 23  
  * Foundation; either version 2 of the License, or (at your option) any later
 24  
  * version.
 25  
  * </p>
 26  
  * <p>
 27  
  * This program is distributed in the hope that it will be useful, but WITHOUT
 28  
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 29  
  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 30  
  * details.
 31  
  * </p>
 32  
  * <p>
 33  
  * You should have received a copy of the GNU General Public License along with
 34  
  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 35  
  * Place, Suite 330, Boston, MA 02111-1307 USA
 36  
  * </p>
 37  
  * <p>
 38  
  * </p>
 39  
  * 
 40  
  * @author David Read
 41  
  */
 42  
 
 43  
 public class InsertTime implements Runnable {
 44  
   /**
 45  
    * Logger
 46  
    */
 47  1
   private static final Logger LOGGER = Logger.getLogger(InsertTime.class);
 48  
   
 49  
   /**
 50  
    * Number of miiliseconds per second
 51  
    */
 52  
   private static final int MILLISECONDS_PER_SECOND = 1000;
 53  
 
 54  
   /**
 55  
    * Number of seconds per minute
 56  
    */
 57  
   private static final int SECONDS_PER_MINUTE = 60;
 58  
 
 59  
   /**
 60  
    * Number of minutes per hour
 61  
    */
 62  
   private static final int MINUTES_PER_HOUR = 60;
 63  
 
 64  
   /**
 65  
    * The component backing this instance
 66  
    */
 67  
   private JLabel label;
 68  
 
 69  
   /**
 70  
    * The starting time offset (e.g. epoch for this instance)
 71  
    */
 72  
   private long epoch;
 73  
 
 74  
   /**
 75  
    * Time to delay between updates of the label
 76  
    */
 77  
   private int msDelay;
 78  
 
 79  
   /**
 80  
    * Format for the label to display its value
 81  
    */
 82  1
   private static final NumberFormat NUMBER_FORMAT = new DecimalFormat("00");
 83  
 
 84  
   /**
 85  
    * Create a new InsertTime instance associated with a JLabel.
 86  
    * 
 87  
    * @param aComp
 88  
    *          The JLabel that this instance will update
 89  
    * @param aMSZeroTime
 90  
    *          The time considered as the start time.
 91  
    * @param aMSDelay
 92  
    *          The number of milliseconds between updates of the time value.
 93  
    */
 94  1
   public InsertTime(JLabel aComp, long aMSZeroTime, int aMSDelay) {
 95  1
     label = aComp;
 96  1
     epoch = aMSZeroTime;
 97  1
     msDelay = aMSDelay;
 98  1
   }
 99  
 
 100  
   /**
 101  
    * Calculate the number of hours represented by the number of milliseconds
 102  
    * provided.
 103  
    * 
 104  
    * @param ms
 105  
    *          Milliseconds
 106  
    * 
 107  
    * @return The number of hours represented by the milliseconds parameter
 108  
    */
 109  
   private int hours(long ms) {
 110  2
     return (int) (ms / (MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE * MINUTES_PER_HOUR));
 111  
   }
 112  
 
 113  
   /**
 114  
    * Calculate the number of minutes represented by the number of milliseconds
 115  
    * provided.
 116  
    * 
 117  
    * @param ms
 118  
    *          Milliseconds
 119  
    * 
 120  
    * @return The number of minutes represented by the milliseconds parameter
 121  
    */
 122  
   private int minutes(long ms) {
 123  2
     return (int) (ms / (MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE))
 124  
         % SECONDS_PER_MINUTE;
 125  
   }
 126  
 
 127  
   /**
 128  
    * Calculate the number of seconds represented by the number of milliseconds
 129  
    * provided.
 130  
    * 
 131  
    * @param ms
 132  
    *          Milliseconds
 133  
    * 
 134  
    * @return The number of seconds represented by the milliseconds parameter
 135  
    */
 136  
   private int seconds(long ms) {
 137  2
     return (int) (ms / MILLISECONDS_PER_SECOND) % SECONDS_PER_MINUTE;
 138  
   }
 139  
 
 140  
   /**
 141  
    * Track the elapsed seconds, updating the JLabel until an interrupt
 142  
    * is received.
 143  
    */
 144  
   public void run() {
 145  
     boolean keepGoing;
 146  
 
 147  1
     keepGoing = true;
 148  
 
 149  
     try {
 150  3
       while (keepGoing && !Thread.currentThread().isInterrupted()) {
 151  2
         final long ms = new Date().getTime() - epoch;
 152  2
         label.setText(NUMBER_FORMAT.format(hours(ms)) + ":"
 153  
             + NUMBER_FORMAT.format(minutes(ms)) + ":"
 154  
             + NUMBER_FORMAT.format(seconds(ms)));
 155  
         // component.setText("" + (new Date().getTime() - msZeroTime) / 1000);
 156  
         try {
 157  2
           Thread.sleep(msDelay);
 158  1
         } catch (InterruptedException ie) {
 159  1
           keepGoing = false;
 160  1
         }
 161  2
       }
 162  0
     } catch (Throwable any) {
 163  0
         LOGGER.warn("Error maintaining the timer", any);
 164  
     } finally {
 165  1
       label.setText("");
 166  1
     }
 167  1
   }
 168  
 }