Coverage Report - org.openpermis.editor.policy.gui.component.TimeStampModel
 
Classes in this File Line Coverage Branch Coverage Complexity
TimeStampModel
96%
57/59
100%
6/6
1.176
 
 1  
 /*
 2  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 3  
  * All rights reserved.
 4  
  * 
 5  
  * Licensed under the Open Permis License which accompanies this distribution, 
 6  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 7  
  */
 8  
 package org.openpermis.editor.policy.gui.component;
 9  
 
 10  
 import java.beans.PropertyChangeListener;
 11  
 import java.beans.PropertyChangeSupport;
 12  
 import java.util.Calendar;
 13  
 import java.util.Date;
 14  
 
 15  
 import org.openpermis.policy.TimeStamp;
 16  
 
 17  
 
 18  
 /**
 19  
  * @since 0.3.0
 20  
  */
 21  
 public class TimeStampModel {
 22  
         
 23  
         //---- Static
 24  
 
 25  
         /**
 26  
          * @since 0.3.0
 27  
          */
 28  
         private static final int MILLIS_PER_SECOND = 1000;
 29  
 
 30  
         /**
 31  
          * @since 0.3.0
 32  
          */
 33  
         private static final int SECONDS_PER_MINUTE = 60;
 34  
 
 35  
         /**
 36  
          * @since 0.3.0
 37  
          */
 38  
         private static final int MINUTES_PER_HOUR = 60;
 39  
 
 40  
         //---- State
 41  
 
 42  
         /**
 43  
          * @since 0.3.0
 44  
          */
 45  
         private final PropertyChangeSupport changeSupport;
 46  
         
 47  
         /**
 48  
          * @since 0.3.0
 49  
          */
 50  
         private TimeStamp timeStamp;
 51  
         
 52  
         //---- Constructors
 53  
 
 54  
         /**
 55  
          * Creates a model for a time stamp.
 56  
          * @param timeStamp the initial time stamp value for the model.
 57  
          * @since 0.3.0
 58  
          */
 59  6
         public TimeStampModel (TimeStamp timeStamp) {
 60  6
                 this.changeSupport = new PropertyChangeSupport(this);
 61  6
                 this.timeStamp = timeStamp;
 62  6
         }
 63  
 
 64  
         //---- Methods
 65  
         
 66  
         /**
 67  
          * Gets the current time stamp value.
 68  
          * @return the current time stamp.
 69  
          * @since 0.3.0
 70  
          */
 71  
         public TimeStamp getTimeStamp () {
 72  7
                 return this.timeStamp;
 73  
         }
 74  
         
 75  
         /**
 76  
          * Sets the current time stamp value.
 77  
          * @param timeStamp the new time stamp value.
 78  
          * @since 0.3.0
 79  
          */
 80  
         public void setTimeStamp (TimeStamp timeStamp) {
 81  1
                 setTimeStamp(timeStamp, true);
 82  1
         }
 83  
 
 84  
         /**
 85  
          * @since 0.3.0
 86  
          */
 87  
         private void setTimeStamp (TimeStamp newValue, boolean propagate) {
 88  3
                 final TimeStamp oldValue = this.timeStamp;
 89  
                 
 90  3
                 if (propagate) {
 91  1
                         final Date newDate = newValue.getDate();
 92  1
                         setDate(newDate, false);
 93  1
                         setTimeOfDay(getTimeOfDay(getCalendar(newDate)), false);
 94  
                 }
 95  
 
 96  3
                 this.timeStamp = newValue;
 97  3
                 firePropertyChanged("timeStamp", oldValue, newValue);
 98  3
         }
 99  
 
 100  
         /**
 101  
          * Gets the date part of the current time stamp.
 102  
          * @return a date with time of day set to midnight.
 103  
          * @since 0.3.0
 104  
          */
 105  
         public Date getDate () {
 106  7
                 final Calendar cal = getCalendar();
 107  7
                 clearTimeOfDay(cal);
 108  7
                 return cal.getTime();
 109  
         }
 110  
 
 111  
         /**
 112  
          * @since 0.3.0
 113  
          */
 114  
         private void clearTimeOfDay (Calendar cal) {
 115  11
                 cal.set(Calendar.HOUR_OF_DAY, 0);
 116  11
                 cal.set(Calendar.MINUTE, 0);
 117  11
                 cal.set(Calendar.SECOND, 0);
 118  11
                 cal.set(Calendar.MILLISECOND, 0);
 119  11
         }
 120  
 
 121  
         /**
 122  
          * Sets the date part of the current time stamp.
 123  
          * @param date the date to set. Its time of day will be replaced by the current time of day.
 124  
          * @since 0.3.0
 125  
          */
 126  
         public void setDate (Date date) {
 127  1
                 setDate(date, true);
 128  1
         }
 129  
         
 130  
         /**
 131  
          * @since 0.3.0
 132  
          */
 133  
         private void setDate (Date newDate, boolean propagate) {
 134  2
                 final Date oldDate = getDate();
 135  2
                 final Calendar cal = getCalendar(newDate);
 136  2
                 clearTimeOfDay(cal);
 137  
 
 138  2
                 if (propagate) {
 139  1
                         final Calendar timeCal = getCalendar();
 140  1
                         timeCal.set(
 141  
                                 cal.get(Calendar.YEAR),
 142  
                                 cal.get(Calendar.MONTH),
 143  
                                 cal.get(Calendar.DAY_OF_MONTH)
 144  
                         );
 145  1
                         setTimeStamp(TimeStamp.fromCalendar(timeCal), false);
 146  
                 }
 147  
 
 148  2
                 firePropertyChanged("date", oldDate, cal.getTime());
 149  2
         }
 150  
 
 151  
         /**
 152  
          * Gets the current time of day in milliseconds.
 153  
          * @return the number of milliseconds that have passed since last midnight.
 154  
          * @since 0.3.0
 155  
          */
 156  
         public long getTimeOfDay () {
 157  7
                 return getTimeOfDay(getCalendar());
 158  
         }
 159  
 
 160  
         /**
 161  
          * @since 0.3.0
 162  
          */
 163  
         private long getTimeOfDay (Calendar cal) {
 164  10
                 final long hours = cal.get(Calendar.HOUR_OF_DAY);
 165  10
                 final long minutes = MINUTES_PER_HOUR * hours + cal.get(Calendar.MINUTE);
 166  10
                 final long seconds = SECONDS_PER_MINUTE * minutes + cal.get(Calendar.SECOND);
 167  10
                 return MILLIS_PER_SECOND * seconds + cal.get(Calendar.MILLISECOND);
 168  
         }
 169  
         
 170  
         /**
 171  
          * Sets the current time of day in milliseconds.
 172  
          * @param timeOfDay the number of milliseconds since midnight. Date information
 173  
          * will be ignored.
 174  
          * @since 0.3.0
 175  
          */
 176  
         public void setTimeOfDay (long timeOfDay) {
 177  1
                 setTimeOfDay(timeOfDay, true);
 178  1
         }
 179  
 
 180  
         /**
 181  
          * @since 0.3.0
 182  
          */
 183  
         private void setTimeOfDay (long timeOfDay, boolean propagate) {
 184  2
                 final long oldTime = getTimeOfDay();
 185  2
                 final Calendar cal = getCalendar();
 186  2
                 clearTimeOfDay(cal);
 187  2
                 cal.set(Calendar.MILLISECOND, (int) timeOfDay);
 188  
                 
 189  2
                 if (propagate) {
 190  1
                         setTimeStamp(TimeStamp.fromCalendar(cal), false);
 191  
                 }
 192  
                 
 193  2
                 firePropertyChanged(
 194  
                         "timeOfDay",
 195  
                         Long.valueOf(oldTime),
 196  
                         Long.valueOf(getTimeOfDay(cal))
 197  
                 );
 198  2
         }
 199  
 
 200  
         /**
 201  
          * @since 0.3.0
 202  
          */
 203  
         private Calendar getCalendar () {
 204  17
                 return getCalendar(this.timeStamp.getDate());
 205  
         }
 206  
 
 207  
         /**
 208  
          * @since 0.3.0
 209  
          */
 210  
         private Calendar getCalendar (Date date) {
 211  20
                 final Calendar cal = Calendar.getInstance(this.timeStamp.getTimeZone());
 212  20
                 cal.setTime(date);
 213  20
                 return cal;
 214  
         }
 215  
 
 216  
         /**
 217  
          * Registers a property change listener on this model.
 218  
          * @param property the property whose changes to observe.
 219  
          * @param listener the listener to register.
 220  
          * @since 0.3.0
 221  
          */
 222  
         public void addPropertyChangeListener (String property, PropertyChangeListener listener) {
 223  9
                 this.changeSupport.addPropertyChangeListener(property, listener);
 224  9
         }
 225  
         
 226  
         /**
 227  
          * Deregisters a property change listener on this model.
 228  
          * @param property the property whose changes to no longer observe.
 229  
          * @param listener the listener to deregister.
 230  
          * @since 0.3.0
 231  
          */
 232  
         public void removePropertyChangeListener (String property, PropertyChangeListener listener) {
 233  0
                 this.changeSupport.removePropertyChangeListener(property, listener);
 234  0
         }
 235  
         
 236  
         /**
 237  
          * Notifies listeners that a property changed its value.
 238  
          * @param property the property whose value was changed.
 239  
          * @param oldValue the previous value of the property.
 240  
          * @param newValue the new value of the property.
 241  
          * @since 0.3.0
 242  
          */
 243  
         protected void firePropertyChanged (String property, Object oldValue, Object newValue) {
 244  7
                 this.changeSupport.firePropertyChange(property, oldValue, newValue);
 245  7
         }
 246  
         
 247  
 }