当前位置: 技术问答>java相关
thinking in java中的一个例子
来源: 互联网 发布时间:2015-02-08
本文导语: thinking in java中,内部类部分最后一个例子。实在看不懂 EventSet中的getnext是如何工作的,它是怎样控制event数组的? abstract public class Event { private long evtTime; public Event(long eventTime) { evtTime = eventTime; ...
thinking in java中,内部类部分最后一个例子。实在看不懂
EventSet中的getnext是如何工作的,它是怎样控制event数组的?
abstract public class Event {
private long evtTime;
public Event(long eventTime) {
evtTime = eventTime;
}
public boolean ready() {
return System.currentTimeMillis() >= evtTime;
}
abstract public void action();
abstract public String description();
}
class EventSet {
private Event[] events = new Event[100];
private int index = 0;
private int next = 0;
public void add(Event e) {
if(index >= events.length)
return; // (In real life, throw exception)
events[index++] = e;
}
public Event getNext() {
boolean looped = false;
int start = next;
do {
next = (next + 1) % events.length;
// See if it has looped to the beginning:
if(start == next) looped = true;
// If it loops past start, the list
// is empty:
if((next == (start + 1) % events.length)
&& looped)
return null;
} while(events[next] == null);
return events[next];
}
public void removeCurrent() {
events[next] = null;
}
}
public class Controller {
private EventSet es = new EventSet();
public void addEvent(Event c) { es.add(c); }
public void run() {
Event e;
while((e = es.getNext()) != null) {
if(e.ready()) {
e.action();
System.out.println(e.description());
es.removeCurrent();
}
}
}
}
EventSet中的getnext是如何工作的,它是怎样控制event数组的?
abstract public class Event {
private long evtTime;
public Event(long eventTime) {
evtTime = eventTime;
}
public boolean ready() {
return System.currentTimeMillis() >= evtTime;
}
abstract public void action();
abstract public String description();
}
class EventSet {
private Event[] events = new Event[100];
private int index = 0;
private int next = 0;
public void add(Event e) {
if(index >= events.length)
return; // (In real life, throw exception)
events[index++] = e;
}
public Event getNext() {
boolean looped = false;
int start = next;
do {
next = (next + 1) % events.length;
// See if it has looped to the beginning:
if(start == next) looped = true;
// If it loops past start, the list
// is empty:
if((next == (start + 1) % events.length)
&& looped)
return null;
} while(events[next] == null);
return events[next];
}
public void removeCurrent() {
events[next] = null;
}
}
public class Controller {
private EventSet es = new EventSet();
public void addEvent(Event c) { es.add(c); }
public void run() {
Event e;
while((e = es.getNext()) != null) {
if(e.ready()) {
e.action();
System.out.println(e.description());
es.removeCurrent();
}
}
}
}
|
getNext() just tries to get the next non-null Event (using an index "next" is bad, EventSet should provide an Enumerator or Iterator):
public Event getNext() {
boolean looped = false;
int start = next; //the current index is in "next", assume already processed, rememberd it
do {
next = (next + 1) % events.length; //go to next entry, go to the first entry if necessary
// See if it has looped to the beginning:
if(start == next) looped = true; //if back to where it starts, we are in a loop, apparently all the rest entries are empty (null)
// If it loops past start, the list
// is empty:
if((next == (start + 1) % events.length)
&& looped)
return null;
} while(events[next] == null);
//got here? it means, we got a non-null entry
return events[next];
}
public Event getNext() {
boolean looped = false;
int start = next; //the current index is in "next", assume already processed, rememberd it
do {
next = (next + 1) % events.length; //go to next entry, go to the first entry if necessary
// See if it has looped to the beginning:
if(start == next) looped = true; //if back to where it starts, we are in a loop, apparently all the rest entries are empty (null)
// If it loops past start, the list
// is empty:
if((next == (start + 1) % events.length)
&& looped)
return null;
} while(events[next] == null);
//got here? it means, we got a non-null entry
return events[next];
}