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