Refactor BodyLinkedList

This commit is contained in:
2022-05-07 12:01:43 +02:00
parent 391b389063
commit a075544fe2

View File

@ -6,8 +6,8 @@ import java.util.Iterator;
*/ */
public class BodyLinkedList implements Iterable<Body> { public class BodyLinkedList implements Iterable<Body> {
private int size = 0; private int size = 0;
private BodyLinkedListItem first; private Item first;
private BodyLinkedListItem last; private Item last;
/** /**
* Initializes 'this' as an empty list. * Initializes 'this' as an empty list.
@ -35,11 +35,11 @@ public class BodyLinkedList implements Iterable<Body> {
*/ */
public void addFirst(Body body) { public void addFirst(Body body) {
if (first == null) { if (first == null) {
first = new BodyLinkedListItem(body); first = new Item(body);
last = first; last = first;
} else { } else {
first.setPrev(new BodyLinkedListItem(body)); first.setPrev(new Item(body));
first = first.prev(); first = first.prev;
} }
size++; size++;
} }
@ -49,11 +49,11 @@ public class BodyLinkedList implements Iterable<Body> {
*/ */
public void addLast(Body body) { public void addLast(Body body) {
if (last == null) { if (last == null) {
last = new BodyLinkedListItem(body); last = new Item(body);
first = last; first = last;
} else { } else {
last.setNext(new BodyLinkedListItem(body)); last.setNext(new Item(body));
last = last.next(); last = last.next;
} }
size++; size++;
} }
@ -63,7 +63,7 @@ public class BodyLinkedList implements Iterable<Body> {
* Returns 'null' if the list is empty. * Returns 'null' if the list is empty.
*/ */
public Body getLast() { public Body getLast() {
return (last != null) ? last.body() : null; return (last != null) ? last.body : null;
} }
/** /**
@ -71,7 +71,7 @@ public class BodyLinkedList implements Iterable<Body> {
* Returns 'null' if the list is empty. * Returns 'null' if the list is empty.
*/ */
public Body getFirst() { public Body getFirst() {
return (first != null) ? first.body() : null; return (first != null) ? first.body : null;
} }
/** /**
@ -82,8 +82,8 @@ public class BodyLinkedList implements Iterable<Body> {
if (first == null) { if (first == null) {
return null; return null;
} }
Body b = first.body(); Body b = first.body;
first = first.next(); first = first.next;
if (first != null) first.setPrev(null); if (first != null) first.setPrev(null);
size--; size--;
return b; return b;
@ -97,8 +97,8 @@ public class BodyLinkedList implements Iterable<Body> {
if (last == null) { if (last == null) {
return null; return null;
} }
Body b = last.body(); Body b = last.body;
last = last.prev(); last = last.prev;
if (last != null) last.setNext(null); if (last != null) last.setNext(null);
size--; size--;
return b; return b;
@ -117,28 +117,28 @@ public class BodyLinkedList implements Iterable<Body> {
return; return;
} }
BodyLinkedListItem item = first; Item item = first;
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
item = item.next(); item = item.next;
} }
item.prev().setNext(new BodyLinkedListItem(body)); item.prev.setNext(new Item(body));
item.setPrev(item.prev().next()); item.setPrev(item.prev.next);
size++; size++;
} }
private Body removeItem(BodyLinkedListItem item) { private Body removeItem(Item item) {
if (item == first) { if (item == first) {
first = item.next(); first = item.next;
if (first != null) first.setPrev(null); if (first != null) first.setPrev(null);
} else if (item == last) { } else if (item == last) {
last = item.prev(); last = item.prev;
if (last != null) last.setNext(null); if (last != null) last.setNext(null);
} else { } else {
item.next().setPrev(item.prev()); item.next.setPrev(item.prev);
} }
size--; size--;
return item.body(); return item.body;
} }
/** /**
@ -146,19 +146,19 @@ public class BodyLinkedList implements Iterable<Body> {
* Precondition: i >= 0 && i < size(). * Precondition: i >= 0 && i < size().
*/ */
public Body get(int i) { public Body get(int i) {
BodyLinkedListItem item; Item item;
if (i < size / 2) { if (i < size / 2) {
item = first; item = first;
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
item = item.next(); item = item.next;
} }
} else { } else {
item = last; item = last;
for (int j = size - 1; j > i; j--) { for (int j = size - 1; j > i; j--) {
item = item.prev(); item = item.prev;
} }
} }
return item.body(); return item.body;
} }
/** /**
@ -170,12 +170,12 @@ public class BodyLinkedList implements Iterable<Body> {
return -1; return -1;
} }
BodyLinkedListItem item = first; Item item = first;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (item.body() == body) { if (item.body == body) {
return i; return i;
} }
item = item.next(); item = item.next;
} }
return -1; return -1;
@ -187,8 +187,8 @@ public class BodyLinkedList implements Iterable<Body> {
*/ */
public BodyLinkedList removeCollidingWith(Body body) { public BodyLinkedList removeCollidingWith(Body body) {
BodyLinkedList removed = new BodyLinkedList(); BodyLinkedList removed = new BodyLinkedList();
for (BodyLinkedListItem item = first; item != null; item = item.next()) { for (Item item = first; item != null; item = item.next) {
if (body != item.body() && body.collidesWith(item.body())) { if (body != item.body && body.collidesWith(item.body)) {
removed.addLast(this.removeItem(item)); removed.addLast(this.removeItem(item));
} }
} }
@ -205,57 +205,45 @@ public class BodyLinkedList implements Iterable<Body> {
@Override @Override
public Iterator<Body> iterator() { public Iterator<Body> iterator() {
return new Iterator<>() { return new Iterator<>() {
BodyLinkedListItem ptr = first; Item ptr = first;
boolean yieldedFirst = false; boolean yieldedFirst = false;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return ptr != null && (!yieldedFirst || ptr.next() != null); return ptr != null && (!yieldedFirst || ptr.next != null);
} }
@Override @Override
public Body next() { public Body next() {
if (!yieldedFirst) { if (!yieldedFirst) {
yieldedFirst = true; yieldedFirst = true;
return ptr.body(); return ptr.body;
} }
ptr = ptr.next(); ptr = ptr.next;
return ptr.body(); return ptr.body;
} }
}; };
} }
}
class BodyLinkedListItem { private class Item {
private final Body body; private final Body body;
private BodyLinkedListItem prev; private Item prev;
private BodyLinkedListItem next; private Item next;
public BodyLinkedListItem(Body body) { public Item(Body body) {
this.body = body; this.body = body;
this.prev = null; this.prev = null;
this.next = null; this.next = null;
} }
public Body body() { public void setPrev(Item prev) {
return body;
}
public BodyLinkedListItem prev() {
return prev;
}
public void setPrev(BodyLinkedListItem prev) {
this.prev = prev; this.prev = prev;
if (prev != null) prev.next = this; if (prev != null) prev.next = this;
} }
public BodyLinkedListItem next() { public void setNext(Item next) {
return next;
}
public void setNext(BodyLinkedListItem next) {
this.next = next; this.next = next;
if (next != null) next.prev = this; if (next != null) next.prev = this;
} }
}
} }