Coverage Report - org.argouml.cognitive.critics.SnoozeOrder
 
Classes in this File Line Coverage Branch Coverage Complexity
SnoozeOrder
37%
9/24
0%
0/4
1.286
 
 1  
 /* $Id: SnoozeOrder.java 17816 2010-01-12 18:34:29Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    linus
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2006 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.cognitive.critics;
 40  
 
 41  
 import java.io.Serializable;
 42  
 import java.util.Date;
 43  
 
 44  
 import org.apache.log4j.Logger;
 45  
 
 46  
 /**
 47  
  * A Critic can be disabled for a certain amount of time by giving it
 48  
  * the snooze command.  Whereas most ControlMech's activate or deactivate
 49  
  * Critic's based on evidence of the Designer's state of mind, this
 50  
  * command allows the Designer to disable Critic's without stating any
 51  
  * reason.  However, after a period of time, the critic may become
 52  
  * active again.  We think this will often be convienent because
 53  
  * Designer's have a lot of tacit knowledge about their own state of
 54  
  * mind that is not worth making explicit.
 55  
  *
 56  
  * @author Jason Robbins
 57  
  */
 58  
 public class SnoozeOrder implements Serializable {
 59  
     /**
 60  
      * Logger.
 61  
      */
 62  900
     private static final Logger LOG = Logger.getLogger(SnoozeOrder.class);
 63  
 
 64  
     ////////////////////////////////////////////////////////////////
 65  
     // constants
 66  
     /**
 67  
      * The initial sleeping time.
 68  
      *
 69  
      * Ten minutes.
 70  
      */
 71  
     private static final long INITIAL_INTERVAL_MS = 1000 * 60 * 10;
 72  
 
 73  
     ////////////////////////////////////////////////////////////////
 74  
     // instance variables
 75  
 
 76  
     /**
 77  
      * Critic should sleep until this time.
 78  
      */
 79  
     private Date snoozeUntil;
 80  
 
 81  
     /**
 82  
      * If the designer snoozees the critics again before this time, then
 83  
      * go to sleep for even longer.
 84  
      */
 85  
     private Date snoozeAgain;
 86  
 
 87  
     /**
 88  
      * The sleeping time, including the effects of repeated snoozeing.
 89  
      */
 90  
     private long interval;
 91  
 
 92  97200
     private Date now = new Date();
 93  
     private Date getNow() {
 94  3187560
         now.setTime(System.currentTimeMillis());
 95  3187560
         return now;
 96  
     }
 97  
 
 98  
     /**
 99  
      * The constructor.
 100  
      *
 101  
      */
 102  97200
     public SnoozeOrder() {
 103  
         /* in the past, 0 milliseconds after January 1, 1970, 00:00:00 GMT. */
 104  97200
         snoozeUntil =  new Date(0);
 105  97200
         snoozeAgain =  new Date(0);
 106  97200
     }
 107  
 
 108  
     ////////////////////////////////////////////////////////////////
 109  
     // accessors
 110  
 
 111  
     /**
 112  
      * @return true if snoozed
 113  
      */
 114  
     public boolean getSnoozed() {
 115  3187560
         return snoozeUntil.after(getNow());
 116  
     }
 117  
 
 118  
     /**
 119  
      * @param h if true, then snooze, else unsnooze
 120  
      */
 121  
     public void setSnoozed(boolean h) {
 122  0
         if (h) {
 123  0
             snooze();
 124  
         } else {
 125  0
             unsnooze();
 126  
         }
 127  0
     }
 128  
 
 129  
     ////////////////////////////////////////////////////////////////
 130  
     // criticism control
 131  
 
 132  
     /**
 133  
      * Snooze the critic.
 134  
      */
 135  
     public void snooze() {
 136  0
         if (snoozeAgain.after(getNow())) {
 137  0
             interval = nextInterval(interval);
 138  
         } else {
 139  0
             interval = INITIAL_INTERVAL_MS;
 140  
         }
 141  0
         long n = (getNow()).getTime();
 142  0
         snoozeUntil.setTime(n + interval);
 143  0
         snoozeAgain.setTime(n + interval + INITIAL_INTERVAL_MS);
 144  0
         LOG.info("Setting snooze order to: " + snoozeUntil.toString());
 145  0
     }
 146  
 
 147  
     /**
 148  
      * Unsnooze the critic.
 149  
      */
 150  
     public void unsnooze() {
 151  
         /* in the past, 0 milliseconds after January 1, 1970, 00:00:00 GMT. */
 152  0
         snoozeUntil =  new Date(0);
 153  0
     }
 154  
 
 155  
     /**
 156  
      * @param last the previous interval
 157  
      * @return the next longer interval
 158  
      */
 159  
     protected long nextInterval(long last) {
 160  
         /* by default, double the snooze interval each time */
 161  0
         return last * 2;
 162  
     }
 163  
 
 164  
     /**
 165  
      * The UID.
 166  
      */
 167  
     private static final long serialVersionUID = -7133285313405407967L;
 168  
 } /* end class SnoozeOrder */