Inlist:
- add Inlist.List size() - make Inlist work items indirectly extending Inlist - check Inlist.push() argument to not push lists
This commit is contained in:
parent
a9d3b0a302
commit
fb628f4e25
@ -1,5 +1,6 @@
|
|||||||
package org.oscim.utils.pool;
|
package org.oscim.utils.pool;
|
||||||
|
|
||||||
|
import static java.lang.System.out;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
@ -75,6 +76,73 @@ public class InlistTest {
|
|||||||
assertEquals(i, 7);
|
assertEquals(i, 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRemoveFirstInIterator() {
|
||||||
|
List<Thing> list = new List<Thing>();
|
||||||
|
list.push(new Thing(1));
|
||||||
|
list.push(new Thing(2));
|
||||||
|
list.push(new Thing(3));
|
||||||
|
list.push(new Thing(4));
|
||||||
|
list.push(new Thing(5));
|
||||||
|
|
||||||
|
out.println("\n shouldRemoveFirstInIterator");
|
||||||
|
|
||||||
|
int cnt = 5;
|
||||||
|
for (Thing t : list) {
|
||||||
|
list.remove();
|
||||||
|
cnt--;
|
||||||
|
assertEquals(cnt, list.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRemoveSomeInIterator() {
|
||||||
|
List<Thing> list = new List<Thing>();
|
||||||
|
list.push(new Thing(1));
|
||||||
|
list.push(new Thing(2));
|
||||||
|
list.push(new Thing(3));
|
||||||
|
list.push(new Thing(4));
|
||||||
|
list.push(new Thing(5));
|
||||||
|
list.push(new Thing(6));
|
||||||
|
out.println("\n shouldRemoveSomeInIterator");
|
||||||
|
|
||||||
|
int pos = 0;
|
||||||
|
for (Thing t : list) {
|
||||||
|
if (pos++ % 2 == 0) {
|
||||||
|
out.println(pos + " val:" + t.value);
|
||||||
|
list.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(3, list.size());
|
||||||
|
|
||||||
|
for (Thing t : list) {
|
||||||
|
out.println(t.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRemoveLastInIterator() {
|
||||||
|
List<Thing> list = new List<Thing>();
|
||||||
|
list.append(new Thing(1));
|
||||||
|
list.append(new Thing(2));
|
||||||
|
out.println("\n shouldRemoveLastInIterator");
|
||||||
|
|
||||||
|
int pos = 0;
|
||||||
|
for (Thing t : list) {
|
||||||
|
if (pos++ == 1) {
|
||||||
|
out.println(pos + " val:" + t.value);
|
||||||
|
list.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(1, list.size());
|
||||||
|
|
||||||
|
for (Thing t : list) {
|
||||||
|
out.println(t.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class Thing extends Inlist<Thing> {
|
static class Thing extends Inlist<Thing> {
|
||||||
final int value;
|
final int value;
|
||||||
|
|
||||||
|
@ -140,9 +140,17 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
/** Iterator: Remove current item */
|
/** Iterator: Remove current item */
|
||||||
@Override
|
@Override
|
||||||
public void remove() {
|
public void remove() {
|
||||||
T tmp = (T) cur.next;
|
/* iterator is at first position */
|
||||||
head = Inlist.remove(head, cur);
|
if (head.next == cur) {
|
||||||
cur = tmp;
|
head = head.next;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Inlist prev = head;
|
||||||
|
while (prev.next.next != cur)
|
||||||
|
prev = prev.next;
|
||||||
|
|
||||||
|
prev.next = cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** NB: Only one iterator at a time possible! */
|
/** NB: Only one iterator at a time possible! */
|
||||||
@ -151,10 +159,18 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
cur = head;
|
cur = head;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return Inlist.size(head);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public T next;
|
public T next;
|
||||||
|
|
||||||
|
public T next() {
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Push 'item' onto 'list'.
|
* Push 'item' onto 'list'.
|
||||||
*
|
*
|
||||||
@ -165,6 +181,9 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public static <T extends Inlist<?>> T push(T list, T item) {
|
public static <T extends Inlist<?>> T push(T list, T item) {
|
||||||
|
if (item.next != null)
|
||||||
|
throw new IllegalArgumentException("'item' is a list");
|
||||||
|
|
||||||
((Inlist) (item)).next = list;
|
((Inlist) (item)).next = list;
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
@ -175,9 +194,9 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
* @param list the list
|
* @param list the list
|
||||||
* @return the number of items in 'list'
|
* @return the number of items in 'list'
|
||||||
*/
|
*/
|
||||||
static <T extends Inlist<T>> int size(T list) {
|
public static <T extends Inlist<?>> int size(T list) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (T l = list; l != null; l = l.next)
|
for (Inlist<?> l = list; l != null; l = l.next)
|
||||||
count++;
|
count++;
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
@ -189,15 +208,16 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
* @param item the item
|
* @param item the item
|
||||||
* @return the new head of 'list'
|
* @return the new head of 'list'
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public static <T extends Inlist<T>> T remove(T list, T item) {
|
public static <T extends Inlist<?>> T remove(T list, T item) {
|
||||||
if (item == list) {
|
if (item == list) {
|
||||||
T head = item.next;
|
Inlist head = item.next;
|
||||||
item.next = null;
|
item.next = null;
|
||||||
return head;
|
return (T) head;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (T 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) {
|
if (it == item) {
|
||||||
prev.next = item.next;
|
prev.next = item.next;
|
||||||
item.next = null;
|
item.next = null;
|
||||||
@ -216,13 +236,14 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
* @param i the index
|
* @param i the index
|
||||||
* @return the item or null
|
* @return the item or null
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({ "unchecked" })
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public static <T extends Inlist<T>> T get(T list, int i) {
|
public static <T extends Inlist<?>> T get(T list, int i) {
|
||||||
if (i < 0)
|
if (i < 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
while (--i > 0 && list != null)
|
while (--i > 0 && list != null)
|
||||||
list = list.next;
|
list = (T) list.next;
|
||||||
|
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
return list;
|
return list;
|
||||||
@ -238,8 +259,9 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
* @param item the item
|
* @param item the item
|
||||||
* @return the new head of 'list'
|
* @return the new head of 'list'
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public static <T extends Inlist<T>> T appendItem(T list, T item) {
|
public static <T extends Inlist<?>> T appendItem(T list, T item) {
|
||||||
|
|
||||||
if (item.next != null)
|
if (item.next != null)
|
||||||
throw new IllegalArgumentException("'item' is list");
|
throw new IllegalArgumentException("'item' is list");
|
||||||
@ -247,11 +269,11 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
if (list == null)
|
if (list == null)
|
||||||
return item;
|
return item;
|
||||||
|
|
||||||
T it = list;
|
Inlist<?> it = list;
|
||||||
while (it.next != null)
|
while (it.next != null)
|
||||||
it = it.next;
|
it = it.next;
|
||||||
|
|
||||||
it.next = item;
|
((Inlist) it).next = item;
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@ -263,22 +285,24 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
* @param other the other
|
* @param other the other
|
||||||
* @return the head of 'list'
|
* @return the head of 'list'
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public static <T extends Inlist<T>> T appendList(T list, T other) {
|
public static <T extends Inlist> T appendList(T list, T other) {
|
||||||
|
|
||||||
if (list == null)
|
if (list == null)
|
||||||
return other;
|
return other;
|
||||||
|
|
||||||
if (list == other)
|
if (other == null)
|
||||||
return list;
|
return list;
|
||||||
|
|
||||||
for (T it = list;; it = it.next) {
|
for (Inlist it = list;; it = it.next) {
|
||||||
if (it.next == null) {
|
if (it.next == null) {
|
||||||
it.next = other;
|
((Inlist) it).next = other;
|
||||||
break;
|
break;
|
||||||
} else if (it.next == other) {
|
|
||||||
throw new IllegalArgumentException("'other' already in 'list'");
|
|
||||||
}
|
}
|
||||||
|
//else if (it.next == other) {
|
||||||
|
// throw new IllegalArgumentException("'other' already in 'list'");
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
@ -290,12 +314,13 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
* @param list the list
|
* @param list the list
|
||||||
* @return the last item
|
* @return the last item
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public static <T extends Inlist<T>> T last(T list) {
|
public static <T extends Inlist<?>> T last(T list) {
|
||||||
while (list != null) {
|
while (list != null) {
|
||||||
if (list.next == null)
|
if (list.next == null)
|
||||||
return list;
|
return list;
|
||||||
list = list.next;
|
list = (T) list.next;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -308,8 +333,9 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
* @param other the other list
|
* @param other the other list
|
||||||
* @return the new head of list
|
* @return the new head of list
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
public static <T extends Inlist<T>> T prependRelative(T list, T item, T other) {
|
public static <T extends Inlist<?>> T prependRelative(T list, T item, T other) {
|
||||||
|
|
||||||
if (item.next != null)
|
if (item.next != null)
|
||||||
throw new IllegalArgumentException("'item' is list");
|
throw new IllegalArgumentException("'item' is list");
|
||||||
@ -318,25 +344,21 @@ public class Inlist<T extends Inlist<T>> {
|
|||||||
throw new IllegalArgumentException("'list' is null");
|
throw new IllegalArgumentException("'list' is null");
|
||||||
|
|
||||||
if (list == other) {
|
if (list == other) {
|
||||||
item.next = list;
|
((Inlist) item).next = list;
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
T it = list;
|
T it = list;
|
||||||
|
|
||||||
while (it != null && it.next != other)
|
while (it != null && it.next != other)
|
||||||
it = it.next;
|
it = (T) it.next;
|
||||||
|
|
||||||
if (it == null)
|
if (it == null)
|
||||||
throw new IllegalArgumentException("'other' not in 'list'");
|
throw new IllegalArgumentException("'other' not in 'list'");
|
||||||
|
|
||||||
item.next = it.next;
|
((Inlist) item).next = it.next;
|
||||||
it.next = item;
|
((Inlist) it).next = item;
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T next() {
|
|
||||||
return next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user