|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.AbstractCollection
com.ccg.util.TimeList
public final class TimeList
Collection of time ordered Objects.
This class is designed to serve as a buffer of time ordered objects. It is intended to be used in the following manner:
add(Object,long) (we use the
current system time if you don't provide a time stamp).remove()) of objects
produces a time ordered sequence as long as the collection
exists. This means that we may refuse to accept the addition of
some data - if the time stamp associated with the new data is older
than data which has already been removed from the queue.This class was originally designed for the purpose of sorting partially time ordered data in a real time context. The original problem, involved lightning solutions which had associated time stamps, however, due to the formulas involved, the resulting solutions would sometimes be slightly out of time order. This was undesirable as we wanted to distribute the solutions to other applications in time order. This class provides the ability to serve as a time ordering buffer in this situation.
Be aware that this implementation provides no internal synchronization. If you intend this collection with multiple threads, you will need to provide your own synchronization mechanism.
HANDY TIP: Though designed to work with the Java millisecond time stamp philosophy, this class could be used for ANY sequential integer sorting mechanism (since 'long' is used as the time stamp). This means you could pass nanoval, second values, day counts, etc - as long as you are consistent.
Here is how you might imbed a sample usage within the comment block:
import com.ccg.util.TimeList;
import java.util.Date;
import java.text.SimpleDateFormat;
public class BirthDay {
public BirthDay(String name, long bdate) {
_Born = bdate;
_Name = name;
}
public long getBirthDate() {
return _Born;
}
public String toString() {
return _Name+' '+(new Date(_Born));
}
private long _Born;
private String _Name;
public static void main(String[] args) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
TimeList tl = new TimeList();
String[] names = { "Erik", "Scott", "Paul", "Megan" };
String[] bdays = { "1992-05-17", "1993-10-04",
"1963-12-25", "1964-01-11" };
for (int i = 0; i < names.length; i++) {
BirthDay bd = new BirthDay(names[i],df.parse(bdays[i]).getTime());
tl.add(bd,bd.getBirthDate());
}
System.out.println(tl);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
| Constructor Summary | |
|---|---|
TimeList()
Default constructor for the TimeList class. |
|
| Method Summary | |
|---|---|
boolean |
add(Object o)
Add a element to the collection. |
boolean |
add(Object o,
long time_stamp)
Add a element to the collection with an associated time stamp. |
void |
clear()
Remove all elements from the collection. |
Object |
clone()
Make a new clone of the TimeList object. |
long |
getDuration()
Get the duration between the oldest and newest piece of data in the queue. |
long |
getTimeLastRemoved()
Get the time stamp associated with the last object removed. |
long |
getTimeNewest()
Get the time stamp of the newest object in the collection. |
long |
getTimeOldest()
Get the time stamp of the oldest object in the collection. |
boolean |
isEmpty()
Determine if the collection is empty. |
Iterator |
iterator()
Get a iterator to go through the elements contained within the collection. |
Object |
peek()
Peek at the oldest entry within the collection. |
Object |
remove()
Remove the oldest entry which is in the collection. |
boolean |
remove(Object obj)
Remove a specific entry from the collection. |
int |
size()
Get the number of elements contained within the collection. |
| Methods inherited from class java.util.AbstractCollection |
|---|
addAll, contains, containsAll, removeAll, retainAll, toArray, toArray, toString |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.Collection |
|---|
equals, hashCode |
| Constructor Detail |
|---|
public TimeList()
TimeList class.
The TimeList will be fully constructed and ready for the addition of objects after being constructed in this fashion.
| Method Detail |
|---|
public Object clone()
throws CloneNotSupportedException
clone in class ObjectCloneNotSupportedExceptionpublic Iterator iterator()
Please note that we support the use of Iterator.remove()
in this implementation. However, since this allows the removal of
objects in an arbitrary order, we do not update the time of the
last removed object if you remove anything in this fashion.
iterator in interface Iterableiterator in interface Collectioniterator in class AbstractCollectionpublic int size()
size in interface Collectionsize in class AbstractCollectionpublic boolean isEmpty()
isEmpty in interface CollectionisEmpty in class AbstractCollectionpublic boolean add(Object o)
This method attempts to add a new element to the collection. It associates the current system time as the time stamp with the object as it attempts to insert it into the collection.
add in interface Collectionadd in class AbstractCollectiono - Object to add to collection.
IllegalArgumentException - If insertion could yield an out of time order problem.
public boolean add(Object o,
long time_stamp)
This method attempts to add a new element to the collection with a specific time stamp associated with the object.
o - Object to add to collection.time_stamp - The particular point in time to associate with
the object. This is used in deciding how to insert the entry into
the collection.
IllegalArgumentException - If time_stamp contains a time prior to any element which has
already passed into AND out of the time list (if insertion
could yield an out of time order problem).public Object peek()
This method allows one to look at the "hot element" within the
collection. The "hot element" is the next element within the
Collection which the remove() operation will
remove (the oldest piece of data).
Using this method does NOT remove the element "peeked" at from the collection.
IllegalStateException - - if the collection is empty.public boolean remove(Object obj)
remove in interface Collectionremove in class AbstractCollectiono - Object to remove (null is OK).
public Object remove()
This is the counter part to the add
method. This method is similar to the peek() method except
that it removes one item from the collection. The removed object
(if any are available) will be the one with the oldest associated
time stamp.
removed - Handle to store reference to object removed (if
anything removed).
IllegalStateException - - if the collection is empty.public void clear()
clear in interface Collectionclear in class AbstractCollectionpublic long getTimeLastRemoved()
This method allows one to get the time associated with the
last object which was removed from the TimeList. If an object has
NEVER been removed, then this method returns Long.MIN_VALUE. The time returned represents the oldest time
which the collection will allow for new additions of data. In
other words, if you attempt to add data to the collection with a
time stamp earlier than this value, your request will be
refused.
Millisecond - time stamp of oldest object which has ever
been removed, OR Long.MIN_VALUE if a object has never
been removed.public long getTimeOldest()
This method allows one to get the time associated with the oldest object which is still contained by the TimeList. If the TimeList is empty, then this method returns false. If this method returns true, it will update the ts variable you pass. The time returned represents the time associated with the oldest data currently in the collection.
ts - Where to store the time stamp of the oldest object in
the collection.
IllegalStateException - - if the collection is empty.public long getTimeNewest()
This method allows one to get the time associated with the newest object which is still contained by the TimeList. If the TimeList is empty, then this method returns 0.
public long getDuration()
This method computes the age difference between the oldest and newest piece of data in the queue. It only does this if its possible (there are at least two items in the queue).
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||