KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > prevayler > implementation > clock > BrokenClock


1 //Prevayler(TM) - The Free-Software Prevalence Layer.
2
//Copyright (C) 2001-2003 Klaus Wuestefeld
3
//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.
4

5 package org.prevayler.implementation.clock;
6
7 import java.util.Date JavaDoc;
8
9 import org.prevayler.*;
10
11
12 /** A deterministic Clock that always returns the same time until it is forced to advance. This class is useful as a Clock mock in order to run automated tests involving date/time related rules. A new BrokenClock's time() starts off at new Date(0);
13  */

14 public class BrokenClock implements Clock {
15
16     private Date JavaDoc _time;
17     protected long _millis;
18
19     public BrokenClock() {
20             this(new Date JavaDoc(0));
21     }
22
23     public BrokenClock(Date JavaDoc time) {
24         _time = time;
25         _millis = time.getTime();
26     }
27
28     public Date JavaDoc time() { return _time; }
29
30     public synchronized void advanceTo(Date JavaDoc newTime) {
31         long newMillis = newTime.getTime();
32         if (newMillis == _millis) return;
33         _millis = newMillis;
34         _time = newTime;
35     }
36
37 }
38
Popular Tags