9 |
|
import junit.framework.*; |
10 |
|
import java.util.Arrays; |
11 |
|
import java.util.ArrayList; |
12 |
+ |
import java.util.Collection; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.NoSuchElementException; |
15 |
|
import java.util.concurrent.BlockingQueue; |
50 |
|
*/ |
51 |
|
static class PDelay implements Delayed { |
52 |
|
int pseudodelay; |
53 |
< |
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
54 |
< |
public int compareTo(PDelay y) { |
55 |
< |
int i = pseudodelay; |
56 |
< |
int j = y.pseudodelay; |
57 |
< |
if (i < j) return -1; |
57 |
< |
if (i > j) return 1; |
58 |
< |
return 0; |
53 |
> |
PDelay(int i) { pseudodelay = i; } |
54 |
> |
public int compareTo(PDelay other) { |
55 |
> |
int a = this.pseudodelay; |
56 |
> |
int b = other.pseudodelay; |
57 |
> |
return (a < b) ? -1 : (a > b) ? 1 : 0; |
58 |
|
} |
60 |
– |
|
59 |
|
public int compareTo(Delayed y) { |
60 |
|
return compareTo((PDelay)y); |
61 |
|
} |
64 |
– |
|
62 |
|
public boolean equals(Object other) { |
63 |
< |
return equals((PDelay)other); |
64 |
< |
} |
68 |
< |
public boolean equals(PDelay other) { |
69 |
< |
return other.pseudodelay == pseudodelay; |
63 |
> |
return (other instanceof PDelay) && |
64 |
> |
this.pseudodelay == ((PDelay)other).pseudodelay; |
65 |
|
} |
66 |
< |
|
66 |
> |
// suppress [overrides] javac warning |
67 |
> |
public int hashCode() { return pseudodelay; } |
68 |
|
public long getDelay(TimeUnit ignore) { |
69 |
< |
return pseudodelay; |
69 |
> |
return Integer.MIN_VALUE + pseudodelay; |
70 |
|
} |
75 |
– |
public int intValue() { |
76 |
– |
return pseudodelay; |
77 |
– |
} |
78 |
– |
|
71 |
|
public String toString() { |
72 |
|
return String.valueOf(pseudodelay); |
73 |
|
} |
100 |
|
return other.trigger == trigger; |
101 |
|
} |
102 |
|
|
103 |
+ |
// suppress [overrides] javac warning |
104 |
+ |
public int hashCode() { return (int) trigger; } |
105 |
+ |
|
106 |
|
public long getDelay(TimeUnit unit) { |
107 |
|
long n = trigger - System.nanoTime(); |
108 |
|
return unit.convert(n, TimeUnit.NANOSECONDS); |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
< |
* Create a queue of given size containing consecutive |
121 |
> |
* Returns a new queue of given size containing consecutive |
122 |
|
* PDelays 0 ... n. |
123 |
|
*/ |
124 |
|
private DelayQueue<PDelay> populatedQueue(int n) { |
488 |
|
} |
489 |
|
|
490 |
|
/** |
496 |
– |
* remove(x) removes x and returns true if present |
497 |
– |
*/ |
498 |
– |
public void testRemoveElement() { |
499 |
– |
DelayQueue q = populatedQueue(SIZE); |
500 |
– |
for (int i = 1; i < SIZE; i+=2) { |
501 |
– |
assertTrue(q.remove(new PDelay(i))); |
502 |
– |
} |
503 |
– |
for (int i = 0; i < SIZE; i+=2) { |
504 |
– |
assertTrue(q.remove(new PDelay(i))); |
505 |
– |
assertFalse(q.remove(new PDelay(i+1))); |
506 |
– |
} |
507 |
– |
assertTrue(q.isEmpty()); |
508 |
– |
} |
509 |
– |
|
510 |
– |
/** |
491 |
|
* contains(x) reports true when elements added but not yet removed |
492 |
|
*/ |
493 |
|
public void testContains() { |
626 |
|
it.next(); |
627 |
|
it.remove(); |
628 |
|
it = q.iterator(); |
629 |
< |
assertEquals(it.next(), new PDelay(2)); |
630 |
< |
assertEquals(it.next(), new PDelay(3)); |
629 |
> |
assertEquals(new PDelay(2), it.next()); |
630 |
> |
assertEquals(new PDelay(3), it.next()); |
631 |
|
assertFalse(it.hasNext()); |
632 |
|
} |
633 |
|
|
637 |
|
public void testToString() { |
638 |
|
DelayQueue q = populatedQueue(SIZE); |
639 |
|
String s = q.toString(); |
640 |
< |
for (int i = 0; i < SIZE; ++i) { |
641 |
< |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
662 |
< |
} |
640 |
> |
for (Object e : q) |
641 |
> |
assertTrue(s.contains(e.toString())); |
642 |
|
} |
643 |
|
|
644 |
|
/** |
652 |
|
public void realRun() throws InterruptedException { |
653 |
|
assertNull(q.poll()); |
654 |
|
threadsStarted.await(); |
655 |
< |
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
655 |
> |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
656 |
|
checkEmpty(q); |
657 |
|
}}); |
658 |
|
|
724 |
|
} |
725 |
|
ArrayList l = new ArrayList(); |
726 |
|
q.drainTo(l); |
727 |
< |
assertEquals(q.size(), 0); |
727 |
> |
assertEquals(0, q.size()); |
728 |
|
for (int i = 0; i < SIZE; ++i) |
729 |
< |
assertEquals(l.get(i), elems[i]); |
729 |
> |
assertEquals(elems[i], l.get(i)); |
730 |
|
q.add(elems[0]); |
731 |
|
q.add(elems[1]); |
732 |
|
assertFalse(q.isEmpty()); |
734 |
|
assertTrue(q.contains(elems[1])); |
735 |
|
l.clear(); |
736 |
|
q.drainTo(l); |
737 |
< |
assertEquals(q.size(), 0); |
738 |
< |
assertEquals(l.size(), 2); |
737 |
> |
assertEquals(0, q.size()); |
738 |
> |
assertEquals(2, l.size()); |
739 |
|
for (int i = 0; i < 2; ++i) |
740 |
< |
assertEquals(l.get(i), elems[i]); |
740 |
> |
assertEquals(elems[i], l.get(i)); |
741 |
|
} |
742 |
|
|
743 |
|
/** |
767 |
|
ArrayList l = new ArrayList(); |
768 |
|
q.drainTo(l, i); |
769 |
|
int k = (i < SIZE) ? i : SIZE; |
770 |
< |
assertEquals(q.size(), SIZE-k); |
771 |
< |
assertEquals(l.size(), k); |
770 |
> |
assertEquals(SIZE-k, q.size()); |
771 |
> |
assertEquals(k, l.size()); |
772 |
|
} |
773 |
|
} |
774 |
|
|
775 |
+ |
/** |
776 |
+ |
* remove(null), contains(null) always return false |
777 |
+ |
*/ |
778 |
+ |
public void testNeverContainsNull() { |
779 |
+ |
Collection<?> q = populatedQueue(SIZE); |
780 |
+ |
assertFalse(q.contains(null)); |
781 |
+ |
assertFalse(q.remove(null)); |
782 |
+ |
} |
783 |
|
} |