TimeCache.java
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2011, Sjoerd van den Dries
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Technische Universiteit Eindhoven nor the
00014  *       names of its contributors may be used to endorse or promote products
00015  *       derived from this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 
00031 package tfjava;
00032 
00033 import java.util.TreeMap;
00034 
00041 public class TimeCache {
00042     
00044     protected TreeMap<Long, TransformStorage> storage;
00046     protected long maxStorageTime;
00048     protected Frame parentFrame;
00050     protected Frame childFrame;
00051     
00055     public TimeCache(long maxStorageTime, Frame parentFrame, Frame childFrame) {
00056         this.maxStorageTime = maxStorageTime;
00057         this.storage = new TreeMap<Long, TransformStorage>();
00058         this.parentFrame = parentFrame;
00059         this.childFrame = childFrame;
00060     }
00061     
00065     public boolean insertData(TransformStorage newData) {  
00066         // check if data is older than first frame in STORAGE - maxStorageTime
00067         if (!storage.isEmpty() && storage.firstKey() - maxStorageTime > newData.getTimeStamp()) {
00068             return false;                              
00069         }
00070         
00071         storage.put(newData.getTimeStamp(), newData);       
00072         
00073         removeOldData(); // same as pruneList in time_cache.h        
00074         return true;
00075     }    
00076 
00085     public TransformStorage getData(long time) {       
00086         
00087         if (storage.isEmpty()) {
00088             // TODO: throw error: "Cache for frame " + parentFrame.getFrameID() + " to " + childFrame.getFrameID() + " is empty";
00089             return null;
00090         } else if (storage.size() == 1) {
00091             // only one transform in cache, so return that one
00092             return storage.firstEntry().getValue();
00093         }        
00094         
00095         TransformStorage low, high;        
00096         
00097         if (time < storage.firstKey()) {
00098             // extrapolate back: low = oldest transform
00099             //                   high = oldest but one 
00100             low = storage.firstEntry().getValue();                     // O(1) (I guess?)
00101             high = storage.higherEntry(storage.firstKey()).getValue(); // O(log n) --> can be done in O(1), but how?
00102         } else if (time > storage.lastKey()) {
00103             // extrapolate forward: low = newest but one
00104             //                      high = newest transform        
00105             low = storage.lowerEntry(storage.lastKey()).getValue();    // O(log n) --> can be done in O(1), but how?
00106             high = storage.lastEntry().getValue();                     // O(1) (I guess?)
00107         } else {
00108             // interpolate: low = newest transform older than time,
00109             //              high = oldest transform newer than time
00110             low = storage.ceilingEntry(time).getValue();
00111             high = storage.floorEntry(time).getValue();
00112         } 
00113 
00114         return TransformStorage.interpolate(low, high, time);
00115     
00116     } 
00117     
00122     public long timeToNearestTransform(long time) {    
00123         Long floor = storage.floorKey(time);
00124         Long ceiling = storage.ceilingKey(time);
00125         
00126         if (floor == null) return (ceiling - time);
00127         if (ceiling == null) return (time - floor);
00128         return Math.min(ceiling - time, time - floor);
00129     }
00130     
00134     protected void removeOldData() {
00135         if (!storage.isEmpty()) {
00136             long timeLowerbound = storage.lastKey() - maxStorageTime;
00137             while (!storage.isEmpty() && storage.firstKey() < timeLowerbound) {
00138                 storage.pollFirstEntry();
00139             }       
00140         }
00141     }
00142     
00149     public boolean timeInBufferRange(long time) {
00150         if (!storage.isEmpty()) {
00151             if(time >  storage.firstKey()  && time < storage.lastKey())
00152                 return true;
00153         }
00154         return false;
00155     }
00156     
00157 }


tfjava
Author(s): Sjoerd van den Dries
autogenerated on Sun Oct 5 2014 22:54:19