sk.robotics.rlcs.data
Class PositionQueue

java.lang.Object
  extended by java.util.AbstractCollection<TimedPosition>
      extended by sk.robotics.rlcs.data.PositionQueue
All Implemented Interfaces:
com.microstepmis.xplatform.CustomXmlMappable, java.lang.Iterable<TimedPosition>, java.util.Collection<TimedPosition>

public class PositionQueue
extends java.util.AbstractCollection<TimedPosition>
implements com.microstepmis.xplatform.CustomXmlMappable

Collection class holding queue of positions. Allows constant-time push and pop, as well as constant-time random access. Positions in the queue are always sorted by time (but this is application's responsibility), so the queue also provides logarithmic-time searching by position age.

This class also allows a shift operation, which shifts all current stored positions by given delta x, y, and h. But this shift operation does not actually change the stored positions, it only remembers that all positions older than the shift are to be treated as shifted. Thus, it is a constant time operation.

This class' implementation is strongly inspired by java.util.ArrayDeque.


Constructor Summary
PositionQueue()
          Constructs an empty queue with an initial capacity for 16 elements.
PositionQueue(int numElements)
          Constructs an empty queue with specified initial capacity.
 
Method Summary
 boolean add(TimedPosition e)
          Adds the specified element to (the tail of) this queue.
 TimedPosition applyShifts(TimedPosition pos)
          Apply all applicable shifts to a position.
 void clear()
          Removes all of the elements from this queue.
 TimedPosition getNewest()
          Retrieves the tail element of this queue, without removing it.
 TimedPosition getOldest()
          Retrieves the head element of this queue, without removing it.
 Position getPositionByTime(long timestamp)
          Get position estimation for given time.
 java.util.Collection<TimedPosition.XmlMappable> getXmlMappable()
           
 boolean isEmpty()
          Checks for queue emptyness.
 java.util.Iterator<TimedPosition> iterator()
          Returns an iterator over the elements in this queue.
 boolean removeOldest()
          Removes the head element of this queue.
 void shiftBy(double dx, double dy, double dh)
           
 void shiftBy(double dx, double dy, double dh, long timestamp)
          Shift all positions in the queue.
 int size()
          Returns the number of elements in this queue.
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Constructor Detail

PositionQueue

public PositionQueue()
Constructs an empty queue with an initial capacity for 16 elements.


PositionQueue

public PositionQueue(int numElements)
Constructs an empty queue with specified initial capacity.

Method Detail

add

public boolean add(TimedPosition e)
Adds the specified element to (the tail of) this queue.

Specified by:
add in interface java.util.Collection<TimedPosition>
Overrides:
add in class java.util.AbstractCollection<TimedPosition>

removeOldest

public boolean removeOldest()
Removes the head element of this queue.

Returns:
True if an item was removed, false if queue was empty

getOldest

public TimedPosition getOldest()
Retrieves the head element of this queue, without removing it.

Returns:
the oldest item, or null if queue is empty

getNewest

public TimedPosition getNewest()
Retrieves the tail element of this queue, without removing it.

Returns:
the newest item, or null if queue is empty

size

public int size()
Returns the number of elements in this queue.

Specified by:
size in interface java.util.Collection<TimedPosition>
Specified by:
size in class java.util.AbstractCollection<TimedPosition>

isEmpty

public boolean isEmpty()
Checks for queue emptyness.

Specified by:
isEmpty in interface java.util.Collection<TimedPosition>
Overrides:
isEmpty in class java.util.AbstractCollection<TimedPosition>

iterator

public java.util.Iterator<TimedPosition> iterator()
Returns an iterator over the elements in this queue. The elements will be ordered from head to tail (from oldest to newest).

Specified by:
iterator in interface java.lang.Iterable<TimedPosition>
Specified by:
iterator in interface java.util.Collection<TimedPosition>
Specified by:
iterator in class java.util.AbstractCollection<TimedPosition>

clear

public void clear()
Removes all of the elements from this queue.

Specified by:
clear in interface java.util.Collection<TimedPosition>
Overrides:
clear in class java.util.AbstractCollection<TimedPosition>

getPositionByTime

public Position getPositionByTime(long timestamp)
Get position estimation for given time. If the queue is empty, null is returned. For times older than oldest position in the queue, the oldest position is returned. For times newer than newest position in the queue, the newest position is returned. If there is a position from exactly the time given, it is returned. Otherwise, a position is interpolated from the closest older and newer positions.


shiftBy

public void shiftBy(double dx,
                    double dy,
                    double dh,
                    long timestamp)
Shift all positions in the queue.


shiftBy

public void shiftBy(double dx,
                    double dy,
                    double dh)

applyShifts

public TimedPosition applyShifts(TimedPosition pos)
Apply all applicable shifts to a position.


getXmlMappable

public java.util.Collection<TimedPosition.XmlMappable> getXmlMappable()
Specified by:
getXmlMappable in interface com.microstepmis.xplatform.CustomXmlMappable