From 32d17cca18273d732eb00a9b00460247f540d958 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Sat, 28 Sep 2013 20:58:34 +0200 Subject: [PATCH] utils.pool: remove debug + docs --- vtm/src/org/oscim/utils/pool/Inlist.java | 121 +++++++++++++-------- vtm/src/org/oscim/utils/pool/SyncPool.java | 17 +++ 2 files changed, 94 insertions(+), 44 deletions(-) diff --git a/vtm/src/org/oscim/utils/pool/Inlist.java b/vtm/src/org/oscim/utils/pool/Inlist.java index 5fb0e2e4..66ee31f6 100644 --- a/vtm/src/org/oscim/utils/pool/Inlist.java +++ b/vtm/src/org/oscim/utils/pool/Inlist.java @@ -19,15 +19,31 @@ package org.oscim.utils.pool; * Instead of using an additional list to hold pool items just extend this * class. * - * Also handy for objects that exist in only *one list* at a time. - * */ - + * Also handy for objects that exist in only *one list* at a time, if you + * are *REALLY* sure about it. Better do not use it! :) + */ public class Inlist> { - private final static boolean debug = false; - public T next; + /** + * Push 'item' onto 'list'. + * + * @param list the list + * @param item the item + * @return the new head of 'list' (item) + */ + public static > T push(T list, T item) { + item.next = list; + return item; + } + + /** + * Get size of 'list'. + * + * @param list the list + * @return the number of items in 'list' + */ static > int size(T list) { int count = 0; for (Inlist l = list; l != null; l = l.next) @@ -35,14 +51,21 @@ public class Inlist> { return count; } + /** + * Removes the 'item' from 'list'. + * + * @param list the list + * @param item the item + * @return the new head of 'list' + */ public static > T remove(T list, T item) { if (item == list) { T head = item.next; item.next = null; return head; } - for (Inlist prev = list, it = list.next; it != null; it = it.next) { + for (Inlist prev = list, it = list.next; it != null; it = it.next) { if (it == item) { prev.next = item.next; item.next = null; @@ -54,6 +77,13 @@ public class Inlist> { return list; } + /** + * Gets the 'item' with index 'i'. + * + * @param list the list + * @param i the index + * @return the item or null + */ public static > T get(T list, int i) { if (i < 0) return null; @@ -67,31 +97,18 @@ public class Inlist> { return null; } - // public static > T insertAt(T list, T item, int i){ - // if (i < 0) - // return null; - // - // while (--i > 0 && list != null) - // list = list.next; - // - // if (i == 0) - // return list; - // - // return null; - // } - - public static > T push(T list, T item) { - item.next = list; - return item; - } - + /** + * Append 'item' to 'list'. 'item' may not be in another list, + * i.e. item.next must be null + * + * @param list the list + * @param item the item + * @return the new head of 'list' + */ public static > T appendItem(T list, T item) { - if (debug) { - if (item.next != null) { - throw new IllegalArgumentException("item is list"); - } - } + if (item.next != null) + throw new IllegalArgumentException("'item' is list"); if (list == null) return item; @@ -106,28 +123,40 @@ public class Inlist> { return list; } - public static > T appendList(T list, T list2) { + /** + * Append list 'other' to 'list'. + * + * @param list the list + * @param other the other + * @return the head of 'list' + */ + public static > T appendList(T list, T other) { if (list == null) - return list2; + return other; - if (list == list2) + if (list == other) return list; Inlist it = list; while (it.next != null) { - if (it.next == list2) - // hmmmm, already in list - return list; + if (it.next == other) + throw new IllegalArgumentException("'other' alreay in 'list'"); it = it.next; } - it.next = list2; + it.next = other; return list; } + /** + * Get last item in from list. + * + * @param list the list + * @return the last item + */ public static > T last(T list) { while (list != null) { if (list.next == null) @@ -137,17 +166,21 @@ public class Inlist> { return null; } + /** + * Prepend 'item' relative to 'other'. + * + * @param list the list + * @param item the item + * @param other the other list + * @return the new head of list + */ public static > T prependRelative(T list, T item, T other) { - if (debug) { - if (item.next != null) { - // warn - item.next = null; - } - } + if (item.next != null) + throw new IllegalArgumentException("'item' is list"); if (list == null) - throw new IllegalArgumentException("Inlist.prependRelative 'list' is null"); + throw new IllegalArgumentException("'list' is null"); if (list == other) { item.next = list; @@ -160,7 +193,7 @@ public class Inlist> { it = it.next; if (it == null) - throw new IllegalArgumentException("Inlist.prependRelative 'other' not in 'list'"); + throw new IllegalArgumentException("'other' not in 'list'"); item.next = it.next; it.next = item; diff --git a/vtm/src/org/oscim/utils/pool/SyncPool.java b/vtm/src/org/oscim/utils/pool/SyncPool.java index d6b60ded..f3adb1c9 100644 --- a/vtm/src/org/oscim/utils/pool/SyncPool.java +++ b/vtm/src/org/oscim/utils/pool/SyncPool.java @@ -59,6 +59,11 @@ public abstract class SyncPool> { } + /** + * Creates the item. To be implemented by subclass. + * + * @return the item + */ protected abstract T createItem(); public void release(T item) { @@ -82,6 +87,12 @@ public abstract class SyncPool> { } } + /** + * Release all items from 'item'. Do not use the + * 'item' reference afterwards! + * + * @param item the item (or list or items) + */ public void releaseAll(T item) { if (item == null) return; @@ -116,6 +127,12 @@ public abstract class SyncPool> { } } + /** + * Gets an 'item' from pool, if pool is empty a new + * item will be created by createItem(). + * + * @return the item + */ public T get() { synchronized (this) {