Minor refactorings and code style changes. 1) Removed several use of raw types 2) Removed unnecessary throws clauses 3) Used lambda expressions wherever applicable 4) Used apt assertion methods for readability 5) Use of try with resources wherever applicable 6) Corrected incorrect order of assertXXX arguments
This commit is contained in:
@@ -22,6 +22,8 @@
|
||||
*/
|
||||
package com.iluwatar.object.pool;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
*
|
||||
* Oliphaunts are expensive to create
|
||||
@@ -29,7 +31,7 @@ package com.iluwatar.object.pool;
|
||||
*/
|
||||
public class Oliphaunt {
|
||||
|
||||
private static int counter = 1;
|
||||
private static AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
private final int id;
|
||||
|
||||
@@ -37,7 +39,7 @@ public class Oliphaunt {
|
||||
* Constructor
|
||||
*/
|
||||
public Oliphaunt() {
|
||||
id = counter++;
|
||||
id = counter.incrementAndGet();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
|
@@ -49,23 +49,23 @@ public class OliphauntPoolTest {
|
||||
public void testSubsequentCheckinCheckout() {
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final OliphauntPool pool = new OliphauntPool();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=0");
|
||||
assertEquals("Pool available=0 inUse=0", pool.toString());
|
||||
|
||||
final Oliphaunt expectedOliphaunt = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=1");
|
||||
assertEquals("Pool available=0 inUse=1", pool.toString());
|
||||
|
||||
pool.checkIn(expectedOliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=1 inUse=0");
|
||||
assertEquals("Pool available=1 inUse=0", pool.toString());
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
final Oliphaunt oliphaunt = pool.checkOut();
|
||||
assertEquals(pool.toString(), "Pool available=0 inUse=1");
|
||||
assertEquals("Pool available=0 inUse=1", pool.toString());
|
||||
assertSame(expectedOliphaunt, oliphaunt);
|
||||
assertEquals(expectedOliphaunt.getId(), oliphaunt.getId());
|
||||
assertEquals(expectedOliphaunt.toString(), oliphaunt.toString());
|
||||
|
||||
pool.checkIn(oliphaunt);
|
||||
assertEquals(pool.toString(), "Pool available=1 inUse=0");
|
||||
assertEquals("Pool available=1 inUse=0", pool.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user