- more work on generic pools
- generic inlist
This commit is contained in:
19
src/org/oscim/utils/pool/LList.java
Normal file
19
src/org/oscim/utils/pool/LList.java
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2013 Hannes Janetzek
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.utils.pool;
|
||||
|
||||
public class LList<T> extends Inlist<LList<T>> {
|
||||
T data;
|
||||
}
|
||||
@@ -14,26 +14,44 @@
|
||||
*/
|
||||
package org.oscim.utils.pool;
|
||||
|
||||
public class Pool<T extends Inlist<T>> {
|
||||
public abstract class Pool<T extends Inlist<T>> {
|
||||
|
||||
T pool;
|
||||
|
||||
/**
|
||||
* @param item release resources
|
||||
*/
|
||||
protected void clearItem(T item) {
|
||||
|
||||
}
|
||||
|
||||
// release 'item' to pool.
|
||||
// make sure that item is not in any other Inlist!
|
||||
public void release(T item) {
|
||||
if (item == null)
|
||||
return;
|
||||
|
||||
clearItem(item);
|
||||
|
||||
item.next = pool;
|
||||
pool = item;
|
||||
}
|
||||
|
||||
// remove 'item' from 'list' and add back to pool
|
||||
public void release(T list, T item) {
|
||||
public T release(T list, T item) {
|
||||
if (item == null)
|
||||
return;
|
||||
return list;
|
||||
|
||||
clearItem(item);
|
||||
|
||||
|
||||
if (item == list) {
|
||||
T ret = item.next;
|
||||
|
||||
item.next = pool;
|
||||
pool = item;
|
||||
return;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
for (T prev = list, it = list.next; it != null; it = it.next) {
|
||||
@@ -43,15 +61,20 @@ public class Pool<T extends Inlist<T>> {
|
||||
|
||||
item.next = pool;
|
||||
pool = item;
|
||||
return;
|
||||
break;
|
||||
}
|
||||
prev = it;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
protected abstract T createItem();
|
||||
|
||||
public T get() {
|
||||
|
||||
if (pool == null)
|
||||
return null;
|
||||
return createItem();
|
||||
|
||||
T ret = pool;
|
||||
pool = pool.next;
|
||||
|
||||
@@ -71,6 +71,7 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
||||
if (fill > maxFill)
|
||||
while (item != null) {
|
||||
clearItem(item);
|
||||
|
||||
item = item.next;
|
||||
count--;
|
||||
}
|
||||
@@ -99,6 +100,8 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
||||
|
||||
T ret = list;
|
||||
|
||||
clearItem(item);
|
||||
|
||||
if (item == list) {
|
||||
ret = item.next;
|
||||
} else {
|
||||
@@ -110,8 +113,6 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
||||
}
|
||||
}
|
||||
|
||||
clearItem(item);
|
||||
|
||||
if (fill < maxFill) {
|
||||
synchronized (this) {
|
||||
fill++;
|
||||
@@ -127,12 +128,13 @@ public abstract class SyncPool<T extends Inlist<T>> {
|
||||
}
|
||||
|
||||
public T get() {
|
||||
if (pool == null){
|
||||
count++;
|
||||
return createItem();
|
||||
}
|
||||
|
||||
synchronized (this) {
|
||||
if (pool == null){
|
||||
count++;
|
||||
return createItem();
|
||||
}
|
||||
|
||||
fill--;
|
||||
|
||||
T ret = pool;
|
||||
|
||||
Reference in New Issue
Block a user