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; |
57 |
< |
if (i > j) return 1; |
58 |
< |
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 |
|
} |
60 |
– |
|
58 |
|
public int compareTo(Delayed y) { |
59 |
|
return compareTo((PDelay)y); |
60 |
|
} |
64 |
– |
|
61 |
|
public boolean equals(Object other) { |
62 |
< |
return equals((PDelay)other); |
63 |
< |
} |
68 |
< |
public boolean equals(PDelay other) { |
69 |
< |
return other.pseudodelay == pseudodelay; |
62 |
> |
return (other instanceof PDelay) && |
63 |
> |
this.pseudodelay == ((PDelay)other).pseudodelay; |
64 |
|
} |
71 |
– |
|
65 |
|
public long getDelay(TimeUnit ignore) { |
66 |
< |
return pseudodelay; |
74 |
< |
} |
75 |
< |
public int intValue() { |
76 |
< |
return pseudodelay; |
66 |
> |
return Integer.MIN_VALUE + pseudodelay; |
67 |
|
} |
78 |
– |
|
68 |
|
public String toString() { |
69 |
|
return String.valueOf(pseudodelay); |
70 |
|
} |
112 |
|
} |
113 |
|
|
114 |
|
/** |
115 |
< |
* Create a queue of given size containing consecutive |
115 |
> |
* Returns a new queue of given size containing consecutive |
116 |
|
* PDelays 0 ... n. |
117 |
|
*/ |
118 |
|
private DelayQueue<PDelay> populatedQueue(int n) { |
482 |
|
} |
483 |
|
|
484 |
|
/** |
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 |
– |
/** |
485 |
|
* contains(x) reports true when elements added but not yet removed |
486 |
|
*/ |
487 |
|
public void testContains() { |
620 |
|
it.next(); |
621 |
|
it.remove(); |
622 |
|
it = q.iterator(); |
623 |
< |
assertEquals(it.next(), new PDelay(2)); |
624 |
< |
assertEquals(it.next(), new PDelay(3)); |
623 |
> |
assertEquals(new PDelay(2), it.next()); |
624 |
> |
assertEquals(new PDelay(3), it.next()); |
625 |
|
assertFalse(it.hasNext()); |
626 |
|
} |
627 |
|
|
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))); |
662 |
< |
} |
634 |
> |
for (Object e : q) |
635 |
> |
assertTrue(s.contains(e.toString())); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
718 |
|
} |
719 |
|
ArrayList l = new ArrayList(); |
720 |
|
q.drainTo(l); |
721 |
< |
assertEquals(q.size(), 0); |
721 |
> |
assertEquals(0, q.size()); |
722 |
|
for (int i = 0; i < SIZE; ++i) |
723 |
< |
assertEquals(l.get(i), elems[i]); |
723 |
> |
assertEquals(elems[i], l.get(i)); |
724 |
|
q.add(elems[0]); |
725 |
|
q.add(elems[1]); |
726 |
|
assertFalse(q.isEmpty()); |
728 |
|
assertTrue(q.contains(elems[1])); |
729 |
|
l.clear(); |
730 |
|
q.drainTo(l); |
731 |
< |
assertEquals(q.size(), 0); |
732 |
< |
assertEquals(l.size(), 2); |
731 |
> |
assertEquals(0, q.size()); |
732 |
> |
assertEquals(2, l.size()); |
733 |
|
for (int i = 0; i < 2; ++i) |
734 |
< |
assertEquals(l.get(i), elems[i]); |
734 |
> |
assertEquals(elems[i], l.get(i)); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
761 |
|
ArrayList l = new ArrayList(); |
762 |
|
q.drainTo(l, i); |
763 |
|
int k = (i < SIZE) ? i : SIZE; |
764 |
< |
assertEquals(q.size(), SIZE-k); |
765 |
< |
assertEquals(l.size(), k); |
764 |
> |
assertEquals(SIZE-k, q.size()); |
765 |
> |
assertEquals(k, l.size()); |
766 |
|
} |
767 |
|
} |
768 |
|
|