Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / trace / TmfTraceStub.java
index 4108e39039edf2886c440905efb1a189b8540b10..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;
@@ -26,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> {
 
     // ------------------------------------------------------------------------
@@ -38,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
     // ------------------------------------------------------------------------
@@ -47,9 +52,7 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @throws FileNotFoundException
      */
     public TmfTraceStub(String filename) throws FileNotFoundException {
-        super("TmfTraceStub", TmfEvent.class, filename);
-        fTrace  = new RandomAccessFile(filename, "r");
-        fParser = new TmfEventParserStub();
+        this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
     }
 
     /**
@@ -67,7 +70,7 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @throws FileNotFoundException
      */
     public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
-        this(filename, DEFAULT_CACHE_SIZE, waitForCompletion);
+        this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
     }
     
     /**
@@ -77,17 +80,24 @@ 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 other
-//     */
-//    public TmfTraceStub(TmfTraceStub other) {
-//        this(filename, DEFAULT_CACHE_SIZE, 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;
+    }
     
     /**
      */
@@ -96,7 +106,7 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
        TmfTraceStub clone = null;
                try {
                        clone = (TmfTraceStub) super.clone();
-               clone.fTrace  = new RandomAccessFile(getName(), "r");
+               clone.fTrace  = new RandomAccessFile(getPath(), "r");
                clone.fParser = new TmfEventParserStub();
                } catch (CloneNotSupportedException e) {
                } catch (FileNotFoundException e) {
@@ -104,14 +114,10 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
        return clone;
     }
  
-    public ITmfTrace createTraceCopy() {
+    @Override
+       public ITmfTrace copy() {
                ITmfTrace returnedValue = null;
-               try {
-                       returnedValue = new TmfTraceStub(this.getName());
-               }
-               catch (FileNotFoundException e) {
-                       e.printStackTrace();
-               }
+               returnedValue = clone();
                return returnedValue;
        }
     
@@ -129,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 {
-                       // 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;
        }
@@ -189,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.029298 seconds and 5 git commands to generate.