#108 Consistent package naming throughout the examples

This commit is contained in:
Ilkka Seppala
2015-07-24 11:32:22 +03:00
parent af92d8dde5
commit 3d488ec15a
128 changed files with 963 additions and 873 deletions

View File

@ -1,45 +1,45 @@
package com.iluwatar.objectpool;
/**
*
* When it is necessary to work with a large number of objects that are particularly expensive to instantiate
* and each object is only needed for a short period of time, the performance of an entire application may be
* adversely affected. An object pool design pattern may be deemed desirable in cases such as these.
*
* The object pool design pattern creates a set of objects that may be reused. When a new object is needed, it
* is requested from the pool. If a previously prepared object is available it is returned immediately, avoiding
* the instantiation cost. If no objects are present in the pool, a new item is created and returned. When the
* object has been used and is no longer needed, it is returned to the pool, allowing it to be used again in the
* future without repeating the computationally expensive instantiation process. It is important to note that
* once an object has been used and returned, existing references will become invalid.
*
* In this example we have created OliphauntPool inheriting from generic ObjectPool. Oliphaunts can be checked
* out from the pool and later returned to it. The pool tracks created instances and their status (available,
* inUse).
*
*/
public class App {
public static void main( String[] args ) {
OliphauntPool pool = new OliphauntPool();
System.out.println(pool);
Oliphaunt oliphaunt1 = pool.checkOut();
System.out.println("Checked out " + oliphaunt1);
System.out.println(pool);
Oliphaunt oliphaunt2 = pool.checkOut();
System.out.println("Checked out " + oliphaunt2);
Oliphaunt oliphaunt3 = pool.checkOut();
System.out.println("Checked out " + oliphaunt3);
System.out.println(pool);
System.out.println("Checking in " + oliphaunt1);
pool.checkIn(oliphaunt1);
System.out.println("Checking in " + oliphaunt2);
pool.checkIn(oliphaunt2);
System.out.println(pool);
Oliphaunt oliphaunt4 = pool.checkOut();
System.out.println("Checked out " + oliphaunt4);
Oliphaunt oliphaunt5 = pool.checkOut();
System.out.println("Checked out " + oliphaunt5);
System.out.println(pool);
}
}
package com.iluwatar.object.pool;
/**
*
* When it is necessary to work with a large number of objects that are particularly expensive to instantiate
* and each object is only needed for a short period of time, the performance of an entire application may be
* adversely affected. An object pool design pattern may be deemed desirable in cases such as these.
*
* The object pool design pattern creates a set of objects that may be reused. When a new object is needed, it
* is requested from the pool. If a previously prepared object is available it is returned immediately, avoiding
* the instantiation cost. If no objects are present in the pool, a new item is created and returned. When the
* object has been used and is no longer needed, it is returned to the pool, allowing it to be used again in the
* future without repeating the computationally expensive instantiation process. It is important to note that
* once an object has been used and returned, existing references will become invalid.
*
* In this example we have created OliphauntPool inheriting from generic ObjectPool. Oliphaunts can be checked
* out from the pool and later returned to it. The pool tracks created instances and their status (available,
* inUse).
*
*/
public class App {
public static void main( String[] args ) {
OliphauntPool pool = new OliphauntPool();
System.out.println(pool);
Oliphaunt oliphaunt1 = pool.checkOut();
System.out.println("Checked out " + oliphaunt1);
System.out.println(pool);
Oliphaunt oliphaunt2 = pool.checkOut();
System.out.println("Checked out " + oliphaunt2);
Oliphaunt oliphaunt3 = pool.checkOut();
System.out.println("Checked out " + oliphaunt3);
System.out.println(pool);
System.out.println("Checking in " + oliphaunt1);
pool.checkIn(oliphaunt1);
System.out.println("Checking in " + oliphaunt2);
pool.checkIn(oliphaunt2);
System.out.println(pool);
Oliphaunt oliphaunt4 = pool.checkOut();
System.out.println("Checked out " + oliphaunt4);
Oliphaunt oliphaunt5 = pool.checkOut();
System.out.println("Checked out " + oliphaunt5);
System.out.println(pool);
}
}

View File

@ -1,37 +1,37 @@
package com.iluwatar.objectpool;
import java.util.HashSet;
/**
*
* Generic object pool
*
* @param <T>
*/
public abstract class ObjectPool<T> {
private HashSet<T> available = new HashSet<>();
private HashSet<T> inUse = new HashSet<>();
protected abstract T create();
public synchronized T checkOut() {
if (available.size() <= 0) {
available.add(create());
}
T instance = available.iterator().next();
available.remove(instance);
inUse.add(instance);
return instance;
}
public synchronized void checkIn(T instance) {
inUse.remove(instance);
available.add(instance);
}
@Override
public String toString() {
return String.format("Pool available=%d inUse=%d", available.size(), inUse.size());
}
}
package com.iluwatar.object.pool;
import java.util.HashSet;
/**
*
* Generic object pool
*
* @param <T>
*/
public abstract class ObjectPool<T> {
private HashSet<T> available = new HashSet<>();
private HashSet<T> inUse = new HashSet<>();
protected abstract T create();
public synchronized T checkOut() {
if (available.size() <= 0) {
available.add(create());
}
T instance = available.iterator().next();
available.remove(instance);
inUse.add(instance);
return instance;
}
public synchronized void checkIn(T instance) {
inUse.remove(instance);
available.add(instance);
}
@Override
public String toString() {
return String.format("Pool available=%d inUse=%d", available.size(), inUse.size());
}
}

View File

@ -1,31 +1,31 @@
package com.iluwatar.objectpool;
/**
*
* Oliphaunts are expensive to create
*
*/
public class Oliphaunt {
private static int counter = 1;
private final int id;
public Oliphaunt() {
id = counter++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public int getId() {
return id;
}
@Override
public String toString() {
return String.format("Oliphaunt id=%d", id);
}
}
package com.iluwatar.object.pool;
/**
*
* Oliphaunts are expensive to create
*
*/
public class Oliphaunt {
private static int counter = 1;
private final int id;
public Oliphaunt() {
id = counter++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public int getId() {
return id;
}
@Override
public String toString() {
return String.format("Oliphaunt id=%d", id);
}
}

View File

@ -1,14 +1,14 @@
package com.iluwatar.objectpool;
/**
*
* Oliphaunt object pool
*
*/
public class OliphauntPool extends ObjectPool<Oliphaunt> {
@Override
protected Oliphaunt create() {
return new Oliphaunt();
}
}
package com.iluwatar.object.pool;
/**
*
* Oliphaunt object pool
*
*/
public class OliphauntPool extends ObjectPool<Oliphaunt> {
@Override
protected Oliphaunt create() {
return new Oliphaunt();
}
}

View File

@ -1,14 +1,14 @@
package com.iluwatar.objectpool;
import org.junit.Test;
import com.iluwatar.objectpool.App;
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}
package com.iluwatar.object.pool;
import org.junit.Test;
import com.iluwatar.object.pool.App;
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}