cleanup: Pool
This commit is contained in:
parent
0f02215e63
commit
45b851c55c
@ -51,7 +51,7 @@ public class Inlist<T extends Inlist<T>> {
|
||||
*/
|
||||
static <T extends Inlist<T>> int size(T list) {
|
||||
int count = 0;
|
||||
for (Inlist<T> l = list; l != null; l = l.next)
|
||||
for (T l = list; l != null; l = l.next)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
@ -71,7 +71,7 @@ public class Inlist<T extends Inlist<T>> {
|
||||
return head;
|
||||
}
|
||||
|
||||
for (Inlist<T> prev = list, it = list.next; it != null; it = it.next) {
|
||||
for (T prev = list, it = list.next; it != null; it = it.next) {
|
||||
if (it == item) {
|
||||
prev.next = item.next;
|
||||
item.next = null;
|
||||
@ -121,8 +121,7 @@ public class Inlist<T extends Inlist<T>> {
|
||||
if (list == null)
|
||||
return item;
|
||||
|
||||
Inlist<T> it = list;
|
||||
|
||||
T it = list;
|
||||
while (it.next != null)
|
||||
it = it.next;
|
||||
|
||||
@ -147,15 +146,14 @@ public class Inlist<T extends Inlist<T>> {
|
||||
if (list == other)
|
||||
return list;
|
||||
|
||||
Inlist<T> it = list;
|
||||
|
||||
while (it.next != null) {
|
||||
if (it.next == other)
|
||||
for (T it = list;; it = it.next) {
|
||||
if (it.next == null) {
|
||||
it.next = other;
|
||||
break;
|
||||
} else if (it.next == other) {
|
||||
throw new IllegalArgumentException("'other' alreay in 'list'");
|
||||
|
||||
it = it.next;
|
||||
}
|
||||
}
|
||||
it.next = other;
|
||||
|
||||
return list;
|
||||
}
|
||||
@ -198,7 +196,7 @@ public class Inlist<T extends Inlist<T>> {
|
||||
return item;
|
||||
}
|
||||
|
||||
Inlist<T> it = list;
|
||||
T it = list;
|
||||
|
||||
while (it != null && it.next != other)
|
||||
it = it.next;
|
||||
|
||||
@ -21,6 +21,8 @@ import javax.annotation.CheckReturnValue;
|
||||
public abstract class Pool<T extends Inlist<T>> {
|
||||
|
||||
protected T pool;
|
||||
protected int limit;
|
||||
protected int fill;
|
||||
|
||||
/**
|
||||
* @param item release resources
|
||||
@ -75,41 +77,20 @@ public abstract class Pool<T extends Inlist<T>> {
|
||||
return null;
|
||||
}
|
||||
|
||||
// remove 'item' from 'list' and add back to pool
|
||||
/** remove 'item' from 'list' and add back to pool */
|
||||
public T release(T list, T item) {
|
||||
if (item == null)
|
||||
return list;
|
||||
|
||||
clearItem(item);
|
||||
|
||||
if (item == list) {
|
||||
T ret = item.next;
|
||||
|
||||
item.next = pool;
|
||||
pool = item;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (T prev = list, it = list.next; it != null; it = it.next) {
|
||||
|
||||
if (it == item) {
|
||||
prev.next = it.next;
|
||||
|
||||
item.next = pool;
|
||||
pool = item;
|
||||
break;
|
||||
}
|
||||
prev = it;
|
||||
}
|
||||
Inlist.remove(list, item);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
protected abstract T createItem();
|
||||
|
||||
/** get an item from pool */
|
||||
public T get() {
|
||||
|
||||
if (pool == null)
|
||||
return createItem();
|
||||
|
||||
@ -119,4 +100,6 @@ public abstract class Pool<T extends Inlist<T>> {
|
||||
ret.next = null;
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected abstract T createItem();
|
||||
}
|
||||
|
||||
@ -39,17 +39,21 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
||||
}
|
||||
|
||||
/**
|
||||
* To be implemented by subclass.
|
||||
*
|
||||
* @param items
|
||||
* number of initial items. NOTE: default does nothing!
|
||||
* number of initial items
|
||||
*/
|
||||
public void init(int items) {
|
||||
fill = 0;
|
||||
pool = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param item
|
||||
* set initial state
|
||||
* @return true when item should be added back to pool
|
||||
* when returning false freeItem will not be called.
|
||||
* @return 'true' when item should be added back to pool,
|
||||
* 'false' when freeItem should be called.
|
||||
*/
|
||||
protected boolean clearItem(T item) {
|
||||
return true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user