Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / trace / TmfTraceStub.java
index 8aa0d3afeb3808510d82f5c277b91ed4e9c339d3..e2cdcbc04450727edb03018a23efef22734d4bc1 100644 (file)
@@ -15,8 +15,11 @@ package org.eclipse.linuxtools.tmf.trace;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.eclipse.linuxtools.tmf.event.TmfEvent;
+import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
+import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
 import org.eclipse.linuxtools.tmf.parser.ITmfEventParser;
 
 /**
@@ -24,6 +27,7 @@ import org.eclipse.linuxtools.tmf.parser.ITmfEventParser;
  * <p>
  * Dummy test trace. Use in conjunction with TmfEventParserStub.
  */
+@SuppressWarnings("nls")
 public class TmfTraceStub extends TmfTrace<TmfEvent> {
 
     // ------------------------------------------------------------------------
@@ -31,11 +35,14 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     // ------------------------------------------------------------------------
 
     // The actual stream
-    private final RandomAccessFile fTrace;
+    private RandomAccessFile fTrace;
 
     // The associated event parser
-    private final ITmfEventParser fParser;
+    private ITmfEventParser fParser;
 
+    // The synchronization lock
+    private ReentrantLock fLock = new ReentrantLock();
+    
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -45,7 +52,7 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @throws FileNotFoundException
      */
     public TmfTraceStub(String filename) throws FileNotFoundException {
-        this(filename, DEFAULT_CACHE_SIZE, false);
+        this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
     }
 
     /**
@@ -59,24 +66,61 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
 
     /**
      * @param filename
+     * @param waitForCompletion
      * @throws FileNotFoundException
      */
     public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
-        this(filename, DEFAULT_CACHE_SIZE, waitForCompletion);
+        this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
     }
-
+    
     /**
      * @param filename
      * @param cacheSize
+     * @param waitForCompletion
      * @throws FileNotFoundException
      */
     public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
-        super(TmfEvent.class, filename, cacheSize);
+        super(null, TmfEvent.class, filename, cacheSize, false);
         fTrace = new RandomAccessFile(filename, "r");
         fParser = new TmfEventParserStub();
-        indexTrace(waitForCompletion);
     }
 
+    
+    /**
+     * @param filename
+     * @param cacheSize
+     * @param waitForCompletion
+     * @param parser
+     * @throws FileNotFoundException
+     */
+    public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion, ITmfEventParser parser) throws FileNotFoundException {
+        super(filename, TmfEvent.class, filename, cacheSize, false);
+        fTrace = new RandomAccessFile(filename, "r");
+        fParser = parser;
+    }
+    
+    /**
+     */
+    @Override
+       public TmfTraceStub clone() {
+       TmfTraceStub clone = null;
+               try {
+                       clone = (TmfTraceStub) super.clone();
+               clone.fTrace  = new RandomAccessFile(getPath(), "r");
+               clone.fParser = new TmfEventParserStub();
+               } catch (CloneNotSupportedException e) {
+               } catch (FileNotFoundException e) {
+               }
+       return clone;
+    }
+    @Override
+       public ITmfTrace copy() {
+               ITmfTrace returnedValue = null;
+               returnedValue = clone();
+               return returnedValue;
+       }
+    
     // ------------------------------------------------------------------------
     // Accessors
     // ------------------------------------------------------------------------
@@ -89,46 +133,135 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     // Operators
     // ------------------------------------------------------------------------
 
+       @Override
        @SuppressWarnings("unchecked")
        public TmfContext seekLocation(ITmfLocation location) {
+           fLock.lock();
         try {
-               synchronized(fTrace) {
-                       // Position the trace at the requested location and
-                       // returns the corresponding context
-                       long loc = (location != null) ? ((TmfLocation<Long>) location).getValue() : 0;
-                       if (loc != fTrace.getFilePointer()) {
-                               fTrace.seek(loc);
-                       }
-                       TmfContext context = new TmfContext(getCurrentLocation(), 0);
-                       return context;
-               }
+            if (fTrace != null) {
+                // Position the trace at the requested location and
+                // returns the corresponding context
+                long loc  = 0;
+                long rank = 0;
+                if (location != null) {
+                    loc = ((TmfLocation<Long>) location).getLocation();
+                    rank = ITmfContext.UNKNOWN_RANK;
+                }
+                if (loc != fTrace.getFilePointer()) {
+                    fTrace.seek(loc);
+                }
+                TmfContext context = new TmfContext(getCurrentLocation(), rank);
+                return context;
+            }
                } catch (IOException e) {
                        e.printStackTrace();
                }
+        finally{
+            fLock.unlock();
+        }
                return null;
     }
 
+
+       @Override
+    public TmfContext seekLocation(double ratio) {
+           fLock.lock();
+        try {
+            if (fTrace != null) {
+                ITmfLocation<?> location = new TmfLocation<Long>(new Long((long) (ratio * fTrace.length())));
+                TmfContext context = seekLocation(location);
+                context.setRank(ITmfContext.UNKNOWN_RANK);
+                return context;
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            fLock.unlock();
+        }
+        
+        return null;
+    }
+
+    @Override
+    @SuppressWarnings("rawtypes")
+    public double getLocationRatio(ITmfLocation location) {
+        fLock.lock();
+        try {
+            if (fTrace != null) {
+                if (location.getLocation() instanceof Long) {
+                    return (double) ((Long) location.getLocation()) / fTrace.length();
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            fLock.unlock();
+        }
+        return 0;
+    }
+
     @Override
        public TmfLocation<Long> getCurrentLocation() {
+        fLock.lock();
         try {
-            return new TmfLocation<Long>(fTrace.getFilePointer());
+            if (fTrace != null) {
+                return new TmfLocation<Long>(fTrace.getFilePointer());
+            }
         } catch (IOException e) {
             e.printStackTrace();
+        } finally {
+            fLock.unlock();
         }
         return null;
     }
 
        @Override
        public TmfEvent parseEvent(TmfContext context) {
+           fLock.lock();
                try {
-                       // paserNextEvent updates the context
-                       TmfEvent event = fParser.parseNextEvent(this, context);
-                       return event;
+                       // parseNextEvent will update the context
+                   if (fTrace != null) {
+                       TmfEvent event = fParser.parseNextEvent(this, context.clone());
+                       return event;
+                   }
                }
                catch (IOException e) {
                        e.printStackTrace();
+               } finally {
+                   fLock.unlock();
                }
                return null;
        }
 
+       @Override
+       public void setTimeRange(TmfTimeRange range) {
+       super.setTimeRange(range);
+    }
+
+       @Override
+       public void setStartTime(TmfTimestamp startTime) {
+       super.setStartTime(startTime);
+    }
+
+       @Override
+       public void setEndTime(TmfTimestamp endTime) {
+       super.setEndTime(endTime);
+    }
+       
+       @Override
+       public void dispose() {
+           fLock.lock();
+           try {
+               if (fTrace != null) {
+                   fTrace.close();
+                   fTrace = null;
+               }
+           } catch (IOException e) {
+               // Ignore
+           } finally {
+               fLock.unlock();  
+           }
+           super.dispose();
+       }
+
 }
\ No newline at end of file
This page took 0.025788 seconds and 5 git commands to generate.