|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.util.WrapArray
public class WrapArray
Implementation of a wrap-around buffer. This class implements a queue in a array type form. Eventually it will probably implement the Java 1.2 Collection interface.
Basically, this object allows one to create an fixed size array capable of holding a maximum number of objects. As objects are added to the array, newer objects will replace older objects. When you fetch elements from the buffer, you specify a index value with must be from 0 to the current number of objects remaining in the queue.
This type of object is extremely useful when you want to keep a buffer of the last N objects around for a period of time.
At the moment, this object is NOT thread safe. I am currently assuming that this object should remain light weight and those which require synchronization will do it themselves.
There are numerous "protected" methods which are provided to allow extended classes the ability to optimize some operations (like binary searches).
NOTE: The name of this class can only be properly expressed while folding one's arms across the chest, wearing a baseball cap backwards, and tipping one's head to the right and slightly back (also use a deep voice).
| Constructor Summary | |
|---|---|
WrapArray()
Initializes array with a capacity of 1024 objects. |
|
WrapArray(int size)
Initializes the array with a configurable buffer capacity. |
|
| Method Summary | |
|---|---|
boolean |
add(Object o)
Add a new object to the buffer. |
protected int |
decrementIndex(int i)
Get next index position of element following a known index position. |
protected Object |
elementAt(int i)
Retrieve a specific object out of the buffer. |
Object |
get(int i)
Retrieve the Nth object out of the buffer. |
protected int |
getEndIndex()
Get index position of the last element in the buffer. |
int |
getMaxSize()
Get the maximum size the buffer can hold. |
protected int |
getMiddleIndex(int s,
int e)
Routine to find the "middle" index value between a start/end pair. |
protected int |
getStartIndex()
Get index position of the first element in the buffer. |
protected int |
incrementIndex(int i)
Get next index position of element following a known index position. |
boolean |
isEmpty()
Determine if the wrap around buffer is empty. |
Object |
remove()
Removes the oldest object from the buffer. |
void |
remove(int n)
Removes the oldest N objects from the buffer. |
int |
size()
Get the number of objects which are still in the buffer. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public WrapArray()
WrapArray(int)public WrapArray(int size)
size - Initial size for array.add(java.lang.Object),
get(int),
remove()| Method Detail |
|---|
public final boolean isEmpty()
get(int)public final int size()
add(java.lang.Object),
get(int)public int getMaxSize()
WrapArray
object can hold. This should be the same as the size which the
WrapArray was initially constructed with.
WrapArray can hold.WrapArray(int),
size()public boolean add(Object o)
o - Object to add to the collection.
remove(),
get(int)public Object remove()
get(0) instead.
add(java.lang.Object),
get(int)public void remove(int n)
num - Number of object to remove (if 0 or less, then nothing is
removed, if greater than or equal to size(), then ALL
are removed).remove(),
get(int)
public Object get(int i)
throws ArrayIndexOutOfBoundsException
void foo(WrapArray wa) {
for (int i = 0; i != wa.size(); i++) {
System.out.println("Object["+i+"] = "+wa.get(i).toString());
}
}
index - Index which should be in the range of [0,size()-1];
ArrayOutOfBoundsException - If specified index is invalid.
ArrayIndexOutOfBoundsExceptionsize()
protected final Object elementAt(int i)
throws ArrayIndexOutOfBoundsException
index - Index which should be in the range of
[getStartIndex(),getEndIndex()].
ArrayOutOfBoundsException - If specified index is invalid.
ArrayIndexOutOfBoundsExceptionsize(),
getStartIndex(),
getEndIndex()protected final int getStartIndex()
void foo(WrapArray wa) {
if (wa.size() == 0) return;
for (int i = wa.getStartIndex(); i != wa.getEndIndex();) {
System.out.println("Object["+i+"] = "+wa.elementAt(i).toString());
i = wa.incrementIndex(i);
}
}
IMPORTANT: If there are not any objects left in the queue,
this routine returns meaningless info.
getEndIndex(),
isEmpty()protected final int getEndIndex()
void foo(WrapArray wa) {
if (wa.size() == 0) return;
for (int i = wa.getStartIndex(); i != wa.getEndIndex();) {
System.out.println("Object["+i+"] = "+wa.elementAt(i).toString());
i = wa.incrementIndex(i);
}
}
IMPORTANT: If there are not any objects left in the queue,
this routine returns meaningless info.
getStartIndex(),
isEmpty()protected final int incrementIndex(int i)
void foo(WrapArray wa) {
for (int i = wa.getStartIndex(); i != wa.getEndIndex();) {
System.out.println("Object["+i+"] = "+wa.elementAt(i).toString());
i = wa.incrementIndex(i);
}
}
index - Current index position.
getStartIndex()protected final int decrementIndex(int i)
void foo(WrapArray wa) {
for (int i = wa.getStartIndex(); i != wa.getEndIndex();) {
System.out.println("Object["+i+"] = "+wa.elementAt(i).toString());
i = wa.incrementIndex(i);
}
}
index - Current index position.
incrementIndex(int)
protected final int getMiddleIndex(int s,
int e)
int findClosestIndex(WrapArray wa, long tMillis) {
int s = wa.getStartIndex();
int e = wa.getEndIndex();
while (s != e) {
// get time of object in middle
int i = wa.getMiddleIndex(s,e);
long dMillis = getTimeMillis(wa.getObject(i));
// if match found, then exit
if (dMillis == tMillis) return i;
if (dMillis > tMillis) {
// check to see if end reached
if (i == s) e = s;
else e = wa.decrementIndex(i);
}
else {
if (i == e) s = e;
else s = wa.incrementIndex(i);
}
}
return s; // s == e == i at closest match
}
s - Starting index postion into the wrap around array. Must be in
the range of [getStartIndex(),getEndIndex()].e - Ending index position into the wrap around array. Must be in
the range of [getStartIndex(),getEndIndex()].
getStartIndex(),
getEndIndex()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||