- more work on generic pools

- generic inlist
This commit is contained in:
Hannes Janetzek
2013-04-10 01:56:38 +02:00
parent ab56cc4b18
commit 129a780c41
7 changed files with 151 additions and 125 deletions

View File

@@ -112,8 +112,8 @@ public final class GeometryUtils {
// The lines are parallel.
// Check if they're collinear.
double y3LessY1 = y3 - y1;
double collinearityTestForP3 = x1 * (y2 - y3) + x2 * (y3LessY1) + x3 * (y1 - y2); // see
// http://mathworld.wolfram.com/Collinear.html
// see http://mathworld.wolfram.com/Collinear.html
double collinearityTestForP3 = x1 * (y2 - y3) + x2 * (y3LessY1) + x3 * (y1 - y2);
// If p3 is collinear with p1 and p2 then p4 will also be collinear,
// since p1-p2 is parallel with p3-p4
if (collinearityTestForP3 == 0) {

View 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;
}

View File

@@ -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;

View File

@@ -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;