From a075544fe29d1ad184a4c90134787b92e037ca60 Mon Sep 17 00:00:00 2001
From: Lorenz Stechauner <lorenz.stechauner@necronda.net>
Date: Sat, 7 May 2022 12:01:43 +0200
Subject: [PATCH] Refactor BodyLinkedList

---
 src/BodyLinkedList.java | 120 ++++++++++++++++++----------------------
 1 file changed, 54 insertions(+), 66 deletions(-)

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