-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
105 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package vproxybase.util.time; | ||
|
||
public interface TimeElem<T> { | ||
T get(); | ||
|
||
/** | ||
* this method should always be called on the event loop | ||
*/ | ||
void removeSelf(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package vproxybase.util.time; | ||
|
||
import vproxybase.util.time.impl.TimeQueueImpl; | ||
|
||
public interface TimeQueue<T> { | ||
static <T> TimeQueue<T> create() { | ||
return new TimeQueueImpl<>(); | ||
} | ||
|
||
TimeElem<T> add(long current, int timeout, T elem); | ||
|
||
/** | ||
* @return element of the nearest timeout event, or null if no elements. this method ignores whether the event is timed-out. | ||
*/ | ||
T poll(); | ||
|
||
boolean isEmpty(); | ||
|
||
/** | ||
* @param current current timestamp millis | ||
* @return time left to the nearest timeout, or 0 if the timeout event triggers, must not < 0, Integer.MAX_VALUE means no timer event | ||
*/ | ||
int nextTime(long current); | ||
} |
24 changes: 24 additions & 0 deletions
24
base/src/main/java/vproxybase/util/time/impl/TimeElemImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package vproxybase.util.time.impl; | ||
|
||
import vproxybase.util.time.TimeElem; | ||
|
||
public class TimeElemImpl<T> implements TimeElem<T> { | ||
public final long triggerTime; | ||
public final T elem; | ||
private final TimeQueueImpl<T> queue; | ||
|
||
TimeElemImpl(long triggerTime, T elem, TimeQueueImpl<T> queue) { | ||
this.triggerTime = triggerTime; | ||
this.elem = elem; | ||
this.queue = queue; | ||
} | ||
|
||
@Override | ||
public T get() { | ||
return elem; | ||
} | ||
|
||
public void removeSelf() { | ||
queue.queue.remove(this); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
base/src/main/java/vproxybase/util/time/impl/TimeQueueImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package vproxybase.util.time.impl; | ||
|
||
import vproxybase.util.time.TimeElem; | ||
import vproxybase.util.time.TimeQueue; | ||
|
||
import java.util.PriorityQueue; | ||
|
||
public class TimeQueueImpl<T> implements TimeQueue<T> { | ||
PriorityQueue<TimeElemImpl<T>> queue = new PriorityQueue<>((a, b) -> (int) (a.triggerTime - b.triggerTime)); | ||
|
||
@Override | ||
public TimeElem<T> add(long currentTimestamp, int timeout, T elem) { | ||
TimeElemImpl<T> event = new TimeElemImpl<>(currentTimestamp + timeout, elem, this); | ||
queue.add(event); | ||
return event; | ||
} | ||
|
||
@Override | ||
public T poll() { | ||
TimeElemImpl<T> elem = queue.poll(); | ||
if (elem == null) | ||
return null; | ||
return elem.elem; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return queue.isEmpty(); | ||
} | ||
|
||
@Override | ||
public int nextTime(long currentTimestamp) { | ||
TimeElemImpl<T> elem = queue.peek(); | ||
if (elem == null) | ||
return Integer.MAX_VALUE; | ||
long triggerTime = elem.triggerTime; | ||
return Math.max((int) (triggerTime - currentTimestamp), 0); | ||
} | ||
} |