Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / trace / TmfTraceStub.java
index 32b0e0595e867c269e049edfbd2091259f7a7efe..e2cdcbc04450727edb03018a23efef22734d4bc1 100644 (file)
@@ -15,6 +15,7 @@ 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;
@@ -39,6 +40,9 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     // The associated event parser
     private ITmfEventParser fParser;
 
+    // The synchronization lock
+    private ReentrantLock fLock = new ReentrantLock();
+    
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -76,11 +80,25 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @throws FileNotFoundException
      */
     public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
-        super(filename, TmfEvent.class, filename, cacheSize);
+        super(null, TmfEvent.class, filename, cacheSize, false);
         fTrace = new RandomAccessFile(filename, "r");
         fParser = new TmfEventParserStub();
     }
 
+    
+    /**
+     * @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
@@ -97,7 +115,7 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     }
  
     @Override
-       public ITmfTrace createTraceCopy() {
+       public ITmfTrace copy() {
                ITmfTrace returnedValue = null;
                returnedValue = clone();
                return returnedValue;
@@ -117,48 +135,100 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
 
        @Override
        @SuppressWarnings("unchecked")
-       public TmfContext seekLocation(ITmfLocation<?> location) {
+       public TmfContext seekLocation(ITmfLocation location) {
+           fLock.lock();
         try {
-               synchronized(fTrace) {
-                       // 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;
-               }
+            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 {
                        // parseNextEvent will update the context
-                       TmfEvent event = fParser.parseNextEvent(this, context.clone());
-                       return event;
+                   if (fTrace != null) {
+                       TmfEvent event = fParser.parseNextEvent(this, context.clone());
+                       return event;
+                   }
                }
                catch (IOException e) {
                        e.printStackTrace();
+               } finally {
+                   fLock.unlock();
                }
                return null;
        }
@@ -177,5 +247,21 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
        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.026357 seconds and 5 git commands to generate.