Apply collection coding standards

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131493 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-01-01 19:24:46 +00:00
parent d83c7839e0
commit 6f705a723c
2 changed files with 92 additions and 86 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java,v 1.3 2003/12/28 16:36:48 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/buffer/BoundedFifoBuffer.java,v 1.4 2004/01/01 19:24:46 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -89,7 +89,7 @@ import org.apache.commons.collections.BufferUnderflowException;
* This buffer prevents null objects from being added. * This buffer prevents null objects from being added.
* *
* @since Commons Collections 3.0 (previously in main package v2.1) * @since Commons Collections 3.0 (previously in main package v2.1)
* @version $Revision: 1.3 $ $Date: 2003/12/28 16:36:48 $ * @version $Revision: 1.4 $ $Date: 2004/01/01 19:24:46 $
* *
* @author Avalon * @author Avalon
* @author Berin Loritsch * @author Berin Loritsch
@ -100,10 +100,10 @@ import org.apache.commons.collections.BufferUnderflowException;
public class BoundedFifoBuffer extends AbstractCollection public class BoundedFifoBuffer extends AbstractCollection
implements Buffer, BoundedCollection { implements Buffer, BoundedCollection {
private final Object[] m_elements; private final Object[] elements;
private int m_start = 0; private int start = 0;
private int m_end = 0; private int end = 0;
private boolean m_full = false; private boolean full = false;
private final int maxElements; private final int maxElements;
/** /**
@ -125,8 +125,8 @@ public class BoundedFifoBuffer extends AbstractCollection
if (size <= 0) { if (size <= 0) {
throw new IllegalArgumentException("The size must be greater than 0"); throw new IllegalArgumentException("The size must be greater than 0");
} }
m_elements = new Object[size]; elements = new Object[size];
maxElements = m_elements.length; maxElements = elements.length;
} }
/** /**
@ -150,12 +150,12 @@ public class BoundedFifoBuffer extends AbstractCollection
public int size() { public int size() {
int size = 0; int size = 0;
if (m_end < m_start) { if (end < start) {
size = maxElements - m_start + m_end; size = maxElements - start + end;
} else if (m_end == m_start) { } else if (end == start) {
size = (m_full ? maxElements : 0); size = (full ? maxElements : 0);
} else { } else {
size = m_end - m_start; size = end - start;
} }
return size; return size;
@ -192,10 +192,10 @@ public class BoundedFifoBuffer extends AbstractCollection
* Clears this buffer. * Clears this buffer.
*/ */
public void clear() { public void clear() {
m_full = false; full = false;
m_start = 0; start = 0;
m_end = 0; end = 0;
Arrays.fill(m_elements, null); Arrays.fill(elements, null);
} }
/** /**
@ -211,18 +211,18 @@ public class BoundedFifoBuffer extends AbstractCollection
throw new NullPointerException("Attempted to add null object to buffer"); throw new NullPointerException("Attempted to add null object to buffer");
} }
if (m_full) { if (full) {
throw new BufferOverflowException("The buffer cannot hold more than " + maxElements + " objects."); throw new BufferOverflowException("The buffer cannot hold more than " + maxElements + " objects.");
} }
m_elements[m_end++] = element; elements[end++] = element;
if (m_end >= maxElements) { if (end >= maxElements) {
m_end = 0; end = 0;
} }
if (m_end == m_start) { if (end == start) {
m_full = true; full = true;
} }
return true; return true;
@ -239,7 +239,7 @@ public class BoundedFifoBuffer extends AbstractCollection
throw new BufferUnderflowException("The buffer is already empty"); throw new BufferUnderflowException("The buffer is already empty");
} }
return m_elements[m_start]; return elements[start];
} }
/** /**
@ -253,16 +253,16 @@ public class BoundedFifoBuffer extends AbstractCollection
throw new BufferUnderflowException("The buffer is already empty"); throw new BufferUnderflowException("The buffer is already empty");
} }
Object element = m_elements[m_start]; Object element = elements[start];
if (null != element) { if (null != element) {
m_elements[m_start++] = null; elements[start++] = null;
if (m_start >= maxElements) { if (start >= maxElements) {
m_start = 0; start = 0;
} }
m_full = false; full = false;
} }
return element; return element;
@ -304,28 +304,32 @@ public class BoundedFifoBuffer extends AbstractCollection
public Iterator iterator() { public Iterator iterator() {
return new Iterator() { return new Iterator() {
private int index = m_start; private int index = start;
private int lastReturnedIndex = -1; private int lastReturnedIndex = -1;
private boolean isFirst = m_full; private boolean isFirst = full;
public boolean hasNext() { public boolean hasNext() {
return isFirst || (index != m_end); return isFirst || (index != end);
} }
public Object next() { public Object next() {
if (!hasNext()) throw new NoSuchElementException(); if (!hasNext()) {
throw new NoSuchElementException();
}
isFirst = false; isFirst = false;
lastReturnedIndex = index; lastReturnedIndex = index;
index = increment(index); index = increment(index);
return m_elements[lastReturnedIndex]; return elements[lastReturnedIndex];
} }
public void remove() { public void remove() {
if (lastReturnedIndex == -1) throw new IllegalStateException(); if (lastReturnedIndex == -1) {
throw new IllegalStateException();
}
// First element can be removed quickly // First element can be removed quickly
if (lastReturnedIndex == m_start) { if (lastReturnedIndex == start) {
BoundedFifoBuffer.this.remove(); BoundedFifoBuffer.this.remove();
lastReturnedIndex = -1; lastReturnedIndex = -1;
return; return;
@ -333,20 +337,20 @@ public class BoundedFifoBuffer extends AbstractCollection
// Other elements require us to shift the subsequent elements // Other elements require us to shift the subsequent elements
int i = lastReturnedIndex + 1; int i = lastReturnedIndex + 1;
while (i != m_end) { while (i != end) {
if (i >= maxElements) { if (i >= maxElements) {
m_elements[i - 1] = m_elements[0]; elements[i - 1] = elements[0];
i = 0; i = 0;
} else { } else {
m_elements[i - 1] = m_elements[i]; elements[i - 1] = elements[i];
i++; i++;
} }
} }
lastReturnedIndex = -1; lastReturnedIndex = -1;
m_end = decrement(m_end); end = decrement(end);
m_elements[m_end] = null; elements[end] = null;
m_full = false; full = false;
index = decrement(index); index = decrement(index);
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java,v 1.2 2003/12/28 16:36:48 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/buffer/UnboundedFifoBuffer.java,v 1.3 2004/01/01 19:24:46 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -86,7 +86,7 @@ import org.apache.commons.collections.BufferUnderflowException;
* This buffer prevents null objects from being added. * This buffer prevents null objects from being added.
* *
* @since Commons Collections 3.0 (previously in main package v2.1) * @since Commons Collections 3.0 (previously in main package v2.1)
* @version $Revision: 1.2 $ $Date: 2003/12/28 16:36:48 $ * @version $Revision: 1.3 $ $Date: 2004/01/01 19:24:46 $
* *
* @author Avalon * @author Avalon
* @author Federico Barbieri * @author Federico Barbieri
@ -96,9 +96,9 @@ import org.apache.commons.collections.BufferUnderflowException;
*/ */
public class UnboundedFifoBuffer extends AbstractCollection implements Buffer { public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
protected Object[] m_buffer; protected Object[] buffer;
protected int m_head; protected int head;
protected int m_tail; protected int tail;
/** /**
* Constructs an UnboundedFifoBuffer with the default number of elements. * Constructs an UnboundedFifoBuffer with the default number of elements.
@ -123,9 +123,9 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
if (initialSize <= 0) { if (initialSize <= 0) {
throw new IllegalArgumentException("The size must be greater than 0"); throw new IllegalArgumentException("The size must be greater than 0");
} }
m_buffer = new Object[initialSize + 1]; buffer = new Object[initialSize + 1];
m_head = 0; head = 0;
m_tail = 0; tail = 0;
} }
/** /**
@ -136,10 +136,10 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
public int size() { public int size() {
int size = 0; int size = 0;
if (m_tail < m_head) { if (tail < head) {
size = m_buffer.length - m_head + m_tail; size = buffer.length - head + tail;
} else { } else {
size = m_tail - m_head; size = tail - head;
} }
return size; return size;
@ -167,30 +167,30 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
throw new NullPointerException("Attempted to add null object to buffer"); throw new NullPointerException("Attempted to add null object to buffer");
} }
if (size() + 1 >= m_buffer.length) { if (size() + 1 >= buffer.length) {
Object[] tmp = new Object[((m_buffer.length - 1) * 2) + 1]; Object[] tmp = new Object[((buffer.length - 1) * 2) + 1];
int j = 0; int j = 0;
for (int i = m_head; i != m_tail;) { for (int i = head; i != tail;) {
tmp[j] = m_buffer[i]; tmp[j] = buffer[i];
m_buffer[i] = null; buffer[i] = null;
j++; j++;
i++; i++;
if (i == m_buffer.length) { if (i == buffer.length) {
i = 0; i = 0;
} }
} }
m_buffer = tmp; buffer = tmp;
m_head = 0; head = 0;
m_tail = j; tail = j;
} }
m_buffer[m_tail] = obj; buffer[tail] = obj;
m_tail++; tail++;
if (m_tail >= m_buffer.length) { if (tail >= buffer.length) {
m_tail = 0; tail = 0;
} }
return true; return true;
} }
@ -206,7 +206,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
throw new BufferUnderflowException("The buffer is already empty"); throw new BufferUnderflowException("The buffer is already empty");
} }
return m_buffer[m_head]; return buffer[head];
} }
/** /**
@ -220,14 +220,14 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
throw new BufferUnderflowException("The buffer is already empty"); throw new BufferUnderflowException("The buffer is already empty");
} }
Object element = m_buffer[m_head]; Object element = buffer[head];
if (null != element) { if (null != element) {
m_buffer[m_head] = null; buffer[head] = null;
m_head++; head++;
if (m_head >= m_buffer.length) { if (head >= buffer.length) {
m_head = 0; head = 0;
} }
} }
@ -242,7 +242,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
*/ */
private int increment(int index) { private int increment(int index) {
index++; index++;
if (index >= m_buffer.length) { if (index >= buffer.length) {
index = 0; index = 0;
} }
return index; return index;
@ -257,7 +257,7 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
private int decrement(int index) { private int decrement(int index) {
index--; index--;
if (index < 0) { if (index < 0) {
index = m_buffer.length - 1; index = buffer.length - 1;
} }
return index; return index;
} }
@ -270,28 +270,30 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
public Iterator iterator() { public Iterator iterator() {
return new Iterator() { return new Iterator() {
private int index = m_head; private int index = head;
private int lastReturnedIndex = -1; private int lastReturnedIndex = -1;
public boolean hasNext() { public boolean hasNext() {
return index != m_tail; return index != tail;
} }
public Object next() { public Object next() {
if (!hasNext()) if (!hasNext()) {
throw new NoSuchElementException(); throw new NoSuchElementException();
}
lastReturnedIndex = index; lastReturnedIndex = index;
index = increment(index); index = increment(index);
return m_buffer[lastReturnedIndex]; return buffer[lastReturnedIndex];
} }
public void remove() { public void remove() {
if (lastReturnedIndex == -1) if (lastReturnedIndex == -1) {
throw new IllegalStateException(); throw new IllegalStateException();
}
// First element can be removed quickly // First element can be removed quickly
if (lastReturnedIndex == m_head) { if (lastReturnedIndex == head) {
UnboundedFifoBuffer.this.remove(); UnboundedFifoBuffer.this.remove();
lastReturnedIndex = -1; lastReturnedIndex = -1;
return; return;
@ -299,19 +301,19 @@ public class UnboundedFifoBuffer extends AbstractCollection implements Buffer {
// Other elements require us to shift the subsequent elements // Other elements require us to shift the subsequent elements
int i = lastReturnedIndex + 1; int i = lastReturnedIndex + 1;
while (i != m_tail) { while (i != tail) {
if (i >= m_buffer.length) { if (i >= buffer.length) {
m_buffer[i - 1] = m_buffer[0]; buffer[i - 1] = buffer[0];
i = 0; i = 0;
} else { } else {
m_buffer[i - 1] = m_buffer[i]; buffer[i - 1] = buffer[i];
i++; i++;
} }
} }
lastReturnedIndex = -1; lastReturnedIndex = -1;
m_tail = decrement(m_tail); tail = decrement(tail);
m_buffer[m_tail] = null; buffer[tail] = null;
index = decrement(index); index = decrement(index);
} }