01: // Prevayler(TM) - The Open-Source Prevalence Layer.
02: // Copyright (C) 2001 Klaus Wuestefeld.
03: // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
04:
05: package org.prevayler.implementation;
06:
07: import org.prevayler.*;
08: import java.util.Date;
09:
10: /**
11: * An AlarmClock that uses the local system clock as its current-time source.
12: * This class can be extended so that other time sources can be used.
13: * @see #currentTimeMillis()
14: */
15: public class SystemClock implements AlarmClock {
16:
17: private Date time;
18: private long millis;
19: private boolean isPaused = true;
20:
21: /**
22: * A newly created SystemClock starts off paused with time() equal to new Date(Long.MIN_VALUE).
23: */
24: public SystemClock() {
25: set(new Date(Long.MIN_VALUE));
26: }
27:
28: public synchronized Date time() {
29: if (isPaused)
30: return time;
31:
32: long currentMillis = currentTimeMillis();
33: if (currentMillis != millis) {
34: set(new Date(currentMillis));
35: }
36:
37: return time;
38: }
39:
40: /**
41: * Causes time() to return always the same value as if the clock had stopped.
42: * 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.
43: * @see #resume()
44: */
45: synchronized void pause() {
46: if (isPaused)
47: throw new IllegalStateException(
48: "AlarmClock was already paused.");
49:
50: time(); //Guarantees the time is up-to-date.
51: isPaused = true;
52: }
53:
54: /**
55: * 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.
56: * @see #pause()
57: */
58: synchronized void resume() {
59: if (!isPaused)
60: throw new IllegalStateException(
61: "AlarmClock was not paused.");
62:
63: isPaused = false;
64: }
65:
66: /**
67: * 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.
68: * @param newMillis the new time in milliseconds. Cannot be earlier than time().getTime() and cannot be later than currentTimeMillis().
69: */
70: synchronized void recover(long newMillis) {
71: if (!isPaused)
72: throw new IllegalStateException(
73: "AlarmClock must be paused for recovering.");
74:
75: if (newMillis == millis)
76: return;
77: if (newMillis < millis)
78: throw new IllegalArgumentException(
79: "AlarmClock's time cannot be set backwards.");
80: if (newMillis > currentTimeMillis())
81: throw new IllegalArgumentException(
82: "AlarmClock's time cannot be set after the current time.");
83:
84: set(new Date(newMillis));
85: }
86:
87: /**
88: * Returns System.currentTimeMillis(). Override this method if you want to use a different time source for your system.
89: */
90: protected long currentTimeMillis() {
91: return System.currentTimeMillis();
92: }
93:
94: private void set(Date time) {
95: this.time = time;
96: this.millis = time.getTime();
97: }
98:
99: }
|