1 | package dmg.util; |
2 | |
3 | /** |
4 | * Utility class to generate a monotonically increasing sequence of |
5 | * values. |
6 | * |
7 | * Under the right conditions, this sequence has a high likelihood to |
8 | * also be monotonically increasing between restarts. This is the case |
9 | * when |
10 | * |
11 | * - Time goes forward. |
12 | * |
13 | * - The frequency at which values are generated between two restarts |
14 | * A and B is lower than 1000 Hz. |
15 | * |
16 | * The main reason for using time as a component is to avoid having to |
17 | * maintain persistent state. |
18 | */ |
19 | public class TimebasedCounter |
20 | { |
21 | private long _last; |
22 | |
23 | public synchronized long next() |
24 | { |
25 | return (_last = Math.max(_last + 1, System.currentTimeMillis())); |
26 | } |
27 | } |