Refactor comments

This commit is contained in:
2022-03-28 10:31:17 +02:00
parent dca869995a
commit fcacaa8657
9 changed files with 152 additions and 88 deletions

View File

@ -1,10 +1,10 @@
// A queue of bodies. A collection designed for holding bodies prior to processing.
// The bodies of the queue can be accessed in a FIFO (first-in-first-out) manner,
// i.e., the body that was first inserted by 'add' is retrieved first by 'poll'.
// The number of elements of the queue is not limited.
//
/**
* A queue of bodies. A collection designed for holding bodies prior to processing.
* The bodies of the queue can be accessed in a FIFO (first-in-first-out) manner,
* i.e., the body that was first inserted by 'add' is retrieved first by 'poll'.
* The number of elements of the queue is not limited.
*/
public class BodyQueue {
private int capacity;
private int head = 0;
private int tail = 0;
@ -14,17 +14,21 @@ public class BodyQueue {
this(4);
}
// Initializes this queue with an initial capacity.
// Precondition: initialCapacity > 0.
/**
* Initializes this queue with an initial capacity.
* Precondition: initialCapacity > 0.
*/
public BodyQueue(int initialCapacity) {
this.capacity = initialCapacity;
this.queue = new Body[this.capacity];
}
// Initializes this queue as an independent copy of the specified queue.
// Calling methods of this queue will not affect the specified queue
// and vice versa.
// Precondition: q != null.
/**
* Initializes this queue as an independent copy of the specified queue.
* Calling methods of this queue will not affect the specified queue
* and vice versa.
* Precondition: q != null.
*/
public BodyQueue(BodyQueue q) {
this.capacity = q.capacity;
this.head = q.size();
@ -35,7 +39,9 @@ public class BodyQueue {
}
}
// Adds the specified body 'b' to this queue.
/**
* Adds the specified body 'b' to this queue.
*/
public void add(Body b) {
queue[head] = b;
head = (head + 1) % capacity;
@ -45,8 +51,10 @@ public class BodyQueue {
}
}
// Retrieves and removes the head of this queue, or returns 'null'
// if this queue is empty.
/**
* Retrieves and removes the head of this queue, or returns 'null'
* if this queue is empty.
*/
public Body poll() {
if (tail == head) {
return null;
@ -57,11 +65,16 @@ public class BodyQueue {
return b;
}
// Returns the number of bodies in this queue.
/**
* Returns the number of bodies in this queue.
*/
public int size() {
return (head - tail + capacity) % capacity;
}
/**
* Double the capacity of the queue.
*/
private void doubleCapacity() {
Body[] tmp = new Body[capacity * 2];
for (int i = head, j = 0; i < tail + capacity; i++, j++) {