Some coding style fixes
[jpcrr.git] / org / jpc / emulator / Timer.java
blob1389ab32bd40058cab6e8f420da91975b17e6216
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org.jpc.emulator;
32 import java.io.*;
34 import org.jpc.emulator.Clock;
36 /**
37 * This class provides for the triggering of events on <code>TimerResponsive</code>
38 * objects at defined and reconfigurable times.
39 * @author Chris Dennis
41 public class Timer implements Comparable<Timer>, SRDumpable
43 private long expireTime;
44 private TimerResponsive callback;
45 private boolean enabled;
46 private Clock myOwner;
48 /**
49 * Constructs a <code>Timer</code> which fires events on the specified
50 * <code>TimerReponsive</code> object using the specified <code>Clock</code>
51 * object as a time-source.
52 * <p>
53 * The constructed timer is initially disabled.
54 * @param target object on which to fire callbacks.
55 * @param parent time-source used to test expiry.
57 public Timer(TimerResponsive target, Clock parent)
59 myOwner = parent;
60 callback = target;
61 enabled = false;
64 public void dumpStatusPartial(StatusDumper output)
66 output.println("\texpireTime " + expireTime + " enabled " + enabled);
67 output.println("\tcallback <object #" + output.objectNumber(callback) + ">"); if(callback != null) callback.dumpStatus(output);
68 output.println("\tmyOwner <object #" + output.objectNumber(myOwner) + ">"); if(myOwner != null) myOwner.dumpStatus(output);
71 public void dumpStatus(StatusDumper output)
73 if(output.dumped(this))
74 return;
76 output.println("#" + output.objectNumber(this) + ": Timer:");
77 dumpStatusPartial(output);
78 output.endObject();
81 public void dumpSRPartial(SRDumper output) throws IOException
83 output.dumpLong(expireTime);
84 output.dumpBoolean(enabled);
85 output.dumpObject(callback);
86 output.dumpObject(myOwner);
89 public Timer(SRLoader input) throws IOException
91 input.objectCreated(this);
92 expireTime = input.loadLong();
93 enabled = input.loadBoolean();
94 callback = (TimerResponsive)input.loadObject();
95 myOwner = (Clock)input.loadObject();
98 public int getTimerType() {
99 return callback.getTimerType();
103 * Returns <code>true</code> if this timer will expire at some point in the
104 * future.
105 * @return <code>true</code> if this timer is enabled.
107 public synchronized boolean enabled()
109 return enabled;
113 * Disables this timer. Following a call to <code>disable</code> the timer
114 * cannot ever fire again unless a call is made to <code>setExpiry</code>
116 public synchronized void disable()
118 setStatus(false);
122 * Sets the expiry time for and enables this timer.
123 * <p>
124 * No restrictions are set on the value of the expiry time. Times in the past
125 * will fire a callback at the next check. Times in the future will fire on
126 * the first call to check after their expiry time has passed. Time units are
127 * decided by the implementation of <code>Clock</code> used by this timer.
128 * @param time absolute time of expiry for this timer.
130 public synchronized void setExpiry(long time)
132 expireTime = time;
133 setStatus(true);
137 * Returns <code>true</code> and fires the targets callback method if this timer is enabled
138 * and its expiry time is earlier than the supplied time.
139 * @param time value of time to check against.
140 * @return <code>true</code> if timer had expired and callback was fired.
142 public synchronized boolean check(long time)
144 if(this.enabled && (time >= expireTime)) {
145 disable();
146 callback.callback();
147 return true;
148 } else
149 return false;
152 private void setStatus(boolean status)
154 enabled = status;
155 myOwner.update(this);
158 public long getExpiry()
160 return expireTime;
163 public String toString()
165 if(this.enabled)
166 return "Timer for " + callback.toString() + " [" + expireTime + "]";
167 else
168 return "Timer for " + callback.toString() + " [Disabled]";
171 public int compareTo(Timer o)
173 if(getExpiry() - o.getExpiry() < 0)
174 return -1;
175 else if(getExpiry() == o.getExpiry())
176 return 0;
177 else
178 return 1;
181 public int hashCode()
183 int hash = 7;
184 hash = 67 * hash + (int) (this.expireTime ^ (this.expireTime >>> 32));
185 hash = 67 * hash + (this.enabled ? 1 : 0);
186 return hash;
189 public boolean equals(Object o)
191 if(!(o instanceof Timer))
192 return false;
194 Timer t = (Timer)o;
196 return (t.enabled() == enabled()) && (t.getExpiry() == getExpiry());