comparison src/org/prevayler/implementation/SystemClock.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 /*
2 The copyright of all source code included in this Prevayler distribution is
3 held by Klaus Wuestefeld, except the files that specifically state otherwise.
4 All rights are reserved. "PREVAYLER" is a trademark of Klaus Wuestefeld.
5
6
7 BSD License:
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 - Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
14
15 - Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 - Neither the name of Prevayler nor the names of its contributors may be used
20 to endorse or promote products derived from this software without specific
21 prior written permission.
22
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 package org.prevayler.implementation;
38
39 import org.prevayler.*;
40 import java.util.Date;
41
42 /**
43 * An AlarmClock that uses the local system clock as its current-time source.
44 * This class can be extended so that other time sources can be used.
45 * @see #currentTimeMillis()
46 */
47 public class SystemClock implements AlarmClock {
48
49 private Date time;
50 private long millis;
51 private boolean isPaused = true;
52
53
54 /**
55 * A newly created SystemClock starts off paused with time() equal to new Date(Long.MIN_VALUE).
56 */
57 public SystemClock() {
58 set(new Date(Long.MIN_VALUE));
59 }
60
61
62 public synchronized Date time() {
63 if (isPaused) return time;
64
65 long currentMillis = currentTimeMillis();
66 if (currentMillis != millis) {
67 set(new Date(currentMillis));
68 }
69
70 return time;
71 }
72
73
74 /**
75 * Causes time() to return always the same value as if the clock had stopped.
76 * The clock does NOT STOP internally though. This method is called by Prevayler before each Command is executed so that it can be executed in a known moment in time.
77 * @see #resume()
78 */
79 synchronized void pause() {
80 if (isPaused) throw new IllegalStateException("AlarmClock was already paused.");
81
82 time(); //Guarantees the time is up-to-date.
83 isPaused = true;
84 }
85
86
87 /**
88 * Causes time() to return the current time again. This method is called by Prevayler after each Command is executed so that the clock can start running again.
89 * @see #pause()
90 */
91 synchronized void resume() {
92 if (!isPaused) throw new IllegalStateException("AlarmClock was not paused.");
93
94 isPaused = false;
95 }
96
97
98 /**
99 * Sets the time forward, recovering some of the time that was "lost" since the clock was paused. The clock must be paused. This method is called by Prevayler when recovering commands from the commandLog file so that they can be re-executed in the "same" time as they had been originally.
100 * @param newMillis the new time in milliseconds. Cannot be earlier than time().getTime() and cannot be later than currentTimeMillis().
101 */
102 synchronized void recover(long newMillis) {
103 if (!isPaused) throw new IllegalStateException("AlarmClock must be paused for recovering.");
104
105 if (newMillis == millis) return;
106 if (newMillis < millis) throw new IllegalArgumentException("AlarmClock's time cannot be set backwards.");
107 if (newMillis > currentTimeMillis()) throw new IllegalArgumentException("AlarmClock's time cannot be set after the current time.");
108
109 set(new Date(newMillis));
110 }
111
112
113 /**
114 * Returns System.currentTimeMillis(). Override this method if you want to use a different time source for your system.
115 */
116 protected long currentTimeMillis() {
117 return System.currentTimeMillis();
118 }
119
120
121 private void set(Date time) {
122 this.time = time;
123 this.millis = time.getTime();
124
125 // System.out.println("Time set to: " + millis);
126 }
127
128 }