9 |
|
import junit.framework.*; |
10 |
|
import java.util.Arrays; |
11 |
|
import java.util.ArrayList; |
12 |
– |
import java.util.Collection; |
12 |
|
import java.util.Iterator; |
13 |
|
import java.util.NoSuchElementException; |
14 |
|
import java.util.concurrent.BlockingQueue; |
49 |
|
*/ |
50 |
|
static class PDelay implements Delayed { |
51 |
|
int pseudodelay; |
52 |
< |
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
53 |
< |
public int compareTo(PDelay y) { |
54 |
< |
int i = pseudodelay; |
55 |
< |
int j = y.pseudodelay; |
56 |
< |
if (i < j) return -1; |
58 |
< |
if (i > j) return 1; |
59 |
< |
return 0; |
52 |
> |
PDelay(int i) { pseudodelay = i; } |
53 |
> |
public int compareTo(PDelay other) { |
54 |
> |
int a = this.pseudodelay; |
55 |
> |
int b = other.pseudodelay; |
56 |
> |
return (a < b) ? -1 : (a > b) ? 1 : 0; |
57 |
|
} |
61 |
– |
|
58 |
|
public int compareTo(Delayed y) { |
59 |
|
return compareTo((PDelay)y); |
60 |
|
} |
65 |
– |
|
61 |
|
public boolean equals(Object other) { |
62 |
< |
return equals((PDelay)other); |
62 |
> |
return (other instanceof PDelay) && |
63 |
> |
this.pseudodelay == ((PDelay)other).pseudodelay; |
64 |
|
} |
69 |
– |
public boolean equals(PDelay other) { |
70 |
– |
return other.pseudodelay == pseudodelay; |
71 |
– |
} |
72 |
– |
|
65 |
|
public long getDelay(TimeUnit ignore) { |
66 |
< |
return pseudodelay; |
75 |
< |
} |
76 |
< |
public int intValue() { |
77 |
< |
return pseudodelay; |
66 |
> |
return Integer.MIN_VALUE + pseudodelay; |
67 |
|
} |
79 |
– |
|
68 |
|
public String toString() { |
69 |
|
return String.valueOf(pseudodelay); |
70 |
|
} |
482 |
|
} |
483 |
|
|
484 |
|
/** |
497 |
– |
* remove(x) removes x and returns true if present |
498 |
– |
*/ |
499 |
– |
public void testRemoveElement() { |
500 |
– |
DelayQueue q = populatedQueue(SIZE); |
501 |
– |
for (int i = 1; i < SIZE; i+=2) { |
502 |
– |
assertTrue(q.remove(new PDelay(i))); |
503 |
– |
} |
504 |
– |
for (int i = 0; i < SIZE; i+=2) { |
505 |
– |
assertTrue(q.remove(new PDelay(i))); |
506 |
– |
assertFalse(q.remove(new PDelay(i+1))); |
507 |
– |
} |
508 |
– |
assertTrue(q.isEmpty()); |
509 |
– |
} |
510 |
– |
|
511 |
– |
/** |
485 |
|
* contains(x) reports true when elements added but not yet removed |
486 |
|
*/ |
487 |
|
public void testContains() { |
631 |
|
public void testToString() { |
632 |
|
DelayQueue q = populatedQueue(SIZE); |
633 |
|
String s = q.toString(); |
634 |
< |
for (int i = 0; i < SIZE; ++i) { |
635 |
< |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
663 |
< |
} |
634 |
> |
for (Object e : q) |
635 |
> |
assertTrue(s.contains(e.toString())); |
636 |
|
} |
637 |
|
|
638 |
|
/** |