29 |
|
static class PDelay implements Delayed { |
30 |
|
int pseudodelay; |
31 |
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
32 |
< |
public int compareTo(Object y) { |
32 |
> |
public int compareTo(PDelay y) { |
33 |
|
int i = pseudodelay; |
34 |
|
int j = ((PDelay)y).pseudodelay; |
35 |
|
if (i < j) return -1; |
37 |
|
return 0; |
38 |
|
} |
39 |
|
|
40 |
< |
public int compareTo(PDelay y) { |
40 |
> |
public int compareTo(Delayed y) { |
41 |
|
int i = pseudodelay; |
42 |
|
int j = ((PDelay)y).pseudodelay; |
43 |
|
if (i < j) return -1; |
74 |
|
NanoDelay(long i) { |
75 |
|
trigger = System.nanoTime() + i; |
76 |
|
} |
77 |
< |
public int compareTo(Object y) { |
77 |
> |
public int compareTo(NanoDelay y) { |
78 |
|
long i = trigger; |
79 |
|
long j = ((NanoDelay)y).trigger; |
80 |
|
if (i < j) return -1; |
82 |
|
return 0; |
83 |
|
} |
84 |
|
|
85 |
< |
public int compareTo(NanoDelay y) { |
85 |
> |
public int compareTo(Delayed y) { |
86 |
|
long i = trigger; |
87 |
|
long j = ((NanoDelay)y).trigger; |
88 |
|
if (i < j) return -1; |
592 |
|
for (int i = 0; i < SIZE; ++i) { |
593 |
|
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
594 |
|
q.poll(); |
595 |
< |
assertTrue(q.peek() == null || |
596 |
< |
i != ((PDelay)q.peek()).intValue()); |
595 |
> |
if (q.isEmpty()) |
596 |
> |
assertNull(q.peek()); |
597 |
> |
else |
598 |
> |
assertTrue(i != ((PDelay)q.peek()).intValue()); |
599 |
|
} |
600 |
|
assertNull(q.peek()); |
601 |
|
} |
667 |
|
assertTrue(q.isEmpty()); |
668 |
|
assertEquals(0, q.size()); |
669 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
670 |
< |
q.add(new PDelay(1)); |
670 |
> |
PDelay x = new PDelay(1); |
671 |
> |
q.add(x); |
672 |
|
assertFalse(q.isEmpty()); |
673 |
+ |
assertTrue(q.contains(x)); |
674 |
|
q.clear(); |
675 |
|
assertTrue(q.isEmpty()); |
676 |
|
} |
885 |
|
} |
886 |
|
} |
887 |
|
|
888 |
+ |
/** |
889 |
+ |
* peek of a non-empty queue returns non-null even if not expired |
890 |
+ |
*/ |
891 |
+ |
public void testPeekDelayed() { |
892 |
+ |
DelayQueue q = new DelayQueue(); |
893 |
+ |
q.add(new NanoDelay(Long.MAX_VALUE)); |
894 |
+ |
assert(q.peek() != null); |
895 |
+ |
} |
896 |
+ |
|
897 |
+ |
/** |
898 |
+ |
* poll of a non-empty queue returns null if no expired elements. |
899 |
+ |
*/ |
900 |
+ |
public void testPollDelayed() { |
901 |
+ |
DelayQueue q = new DelayQueue(); |
902 |
+ |
q.add(new NanoDelay(Long.MAX_VALUE)); |
903 |
+ |
assertNull(q.poll()); |
904 |
+ |
} |
905 |
|
|
906 |
|
/** |
907 |
|
* drainTo(null) throws NPE |
931 |
|
* drainTo(c) empties queue into another collection c |
932 |
|
*/ |
933 |
|
public void testDrainTo() { |
934 |
< |
DelayQueue q = populatedQueue(SIZE); |
934 |
> |
DelayQueue q = new DelayQueue(); |
935 |
> |
PDelay[] elems = new PDelay[SIZE]; |
936 |
> |
for (int i = 0; i < SIZE; ++i) { |
937 |
> |
elems[i] = new PDelay(i); |
938 |
> |
q.add(elems[i]); |
939 |
> |
} |
940 |
|
ArrayList l = new ArrayList(); |
941 |
|
q.drainTo(l); |
942 |
|
assertEquals(q.size(), 0); |
943 |
< |
assertEquals(l.size(), SIZE); |
943 |
> |
for (int i = 0; i < SIZE; ++i) |
944 |
> |
assertEquals(l.get(i), elems[i]); |
945 |
> |
q.add(elems[0]); |
946 |
> |
q.add(elems[1]); |
947 |
> |
assertFalse(q.isEmpty()); |
948 |
> |
assertTrue(q.contains(elems[0])); |
949 |
> |
assertTrue(q.contains(elems[1])); |
950 |
> |
l.clear(); |
951 |
> |
q.drainTo(l); |
952 |
> |
assertEquals(q.size(), 0); |
953 |
> |
assertEquals(l.size(), 2); |
954 |
> |
for (int i = 0; i < 2; ++i) |
955 |
> |
assertEquals(l.get(i), elems[i]); |
956 |
|
} |
957 |
|
|
958 |
|
/** |
971 |
|
q.drainTo(l); |
972 |
|
assertTrue(l.size() >= SIZE); |
973 |
|
t.join(); |
974 |
< |
assertTrue(q.size() + l.size() == SIZE+1); |
974 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
975 |
|
} catch(Exception e){ |
976 |
|
unexpectedException(); |
977 |
|
} |