Add handling of initial range offset in experiments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / tests / stubs / trace / TmfTraceStub.java
index b2bced5b67617062307e344f62b494467d97f70b..2a16a2e7ba47ac2932bc41858534373b11ecf061 100644 (file)
@@ -1,11 +1,11 @@
 /*******************************************************************************
  * Copyright (c) 2009, 2010 Ericsson
- * 
+ *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
  *******************************************************************************/
@@ -17,17 +17,20 @@ import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.util.concurrent.locks.ReentrantLock;
 
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
-import org.eclipse.linuxtools.tmf.core.parser.ITmfEventParser;
+import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer;
 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
-import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
+import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation;
 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
 
 /**
@@ -35,8 +38,8 @@ import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
  * <p>
  * Dummy test trace. Use in conjunction with TmfEventParserStub.
  */
-@SuppressWarnings("nls")
-public class TmfTraceStub extends TmfTrace<TmfEvent> {
+@SuppressWarnings({"nls","javadoc"})
+public class TmfTraceStub extends TmfTrace implements ITmfEventParser {
 
     // ------------------------------------------------------------------------
     // Attributes
@@ -45,31 +48,38 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     // The actual stream
     private RandomAccessFile fTrace;
 
-    // The associated event parser
-    private ITmfEventParser<TmfEvent> fParser;
+//    // The associated event parser
+//    private ITmfEventParser<TmfEvent> fParser;
 
     // The synchronization lock
     private final ReentrantLock fLock = new ReentrantLock();
 
+    private ITmfTimestamp fInitialRangeOffset = null;
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
 
+    public TmfTraceStub() {
+        super();
+        setParser(new TmfEventParserStub(this));
+    }
+
     /**
      * @param path
      * @throws FileNotFoundException
      */
-    public TmfTraceStub() {
-        super();
-        fParser = new TmfEventParserStub();
+    public TmfTraceStub(final String path) throws TmfTraceException {
+        this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false);
     }
 
     /**
      * @param path
+     * @param cacheSize
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(final String path) throws FileNotFoundException {
-        this(path, DEFAULT_TRACE_CACHE_SIZE, false);
+    public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException {
+        this(path, cacheSize, false);
     }
 
     /**
@@ -77,8 +87,14 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @param cacheSize
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(final String path, final int cacheSize) throws FileNotFoundException {
-        this(path, cacheSize, false);
+    public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException {
+        super(null, TmfEvent.class, path, cacheSize, interval);
+        try {
+            fTrace = new RandomAccessFile(path, "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser(new TmfEventParserStub(this));
     }
 
     /**
@@ -86,7 +102,7 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @param cacheSize
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer<?> indexer) throws FileNotFoundException {
+    public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer indexer) throws TmfTraceException {
         this(path, cacheSize, false, null, indexer);
     }
 
@@ -95,8 +111,8 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @param waitForCompletion
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(final String path, final boolean waitForCompletion) throws FileNotFoundException {
-        this(path, DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
+    public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException {
+        this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
     }
 
     /**
@@ -105,11 +121,17 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @param waitForCompletion
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws FileNotFoundException {
-        //        this(path, cacheSize, waitForCompletion, null);
+    public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
         super(null, TmfEvent.class, path, cacheSize);
-        fTrace = new RandomAccessFile(path, "r");
-        fParser = new TmfEventParserStub();
+        try {
+            fTrace = new RandomAccessFile(path, "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser(new TmfEventParserStub(this));
+        if (waitForCompletion) {
+            indexTrace();
+        }
     }
 
     /**
@@ -118,11 +140,14 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @param waitForCompletion
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(final IResource resource,  final String path, final int cacheSize, final boolean waitForCompletion) throws FileNotFoundException {
-        //        this(path, cacheSize, waitForCompletion, null);
+    public TmfTraceStub(final IResource resource,  final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
         super(resource, TmfEvent.class, path, cacheSize);
-        fTrace = new RandomAccessFile(path, "r");
-        fParser = new TmfEventParserStub();
+        try {
+            fTrace = new RandomAccessFile(path, "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser(new TmfEventParserStub(this));
     }
 
     /**
@@ -133,32 +158,49 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @throws FileNotFoundException
      */
     public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion,
-            final ITmfEventParser<TmfEvent> parser, final ITmfTraceIndexer<?> indexer) throws FileNotFoundException {
+            final ITmfEventParser parser, final ITmfTraceIndexer indexer) throws TmfTraceException {
         super(null, TmfEvent.class, path, cacheSize, 0, indexer);
-        fTrace = new RandomAccessFile(path, "r");
-        fParser = (parser != null) ? parser : new TmfEventParserStub();
+        try {
+            fTrace = new RandomAccessFile(path, "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser((parser != null) ? parser : new TmfEventParserStub(this));
     }
 
     /**
      * Copy constructor
      */
-    public TmfTraceStub(final TmfTraceStub trace) throws FileNotFoundException {
+    public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException {
         super(trace);
-        fTrace = new RandomAccessFile(getPath(), "r");
-        fParser = new TmfEventParserStub();
+        try {
+            fTrace = new RandomAccessFile(getPath(), "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser(new TmfEventParserStub(this));
     }
 
     public void indexTrace() {
-        fIndexer.buildIndex(true);
+        indexTrace(true);
     }
 
     @Override
-    public void initTrace(final IResource resource, final String path, final Class<TmfEvent> type) throws FileNotFoundException {
-        fTrace = new RandomAccessFile(path, "r");
-        fParser = new TmfEventParserStub();
+    public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
+        try {
+            fTrace = new RandomAccessFile(path, "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser(new TmfEventParserStub(this));
         super.initTrace(resource, path, type);
     }
 
+    @Override
+    public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
+        super.initialize(resource, path, type);
+    }
+
     // ------------------------------------------------------------------------
     // Accessors
     // ------------------------------------------------------------------------
@@ -167,13 +209,24 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
         return fTrace;
     }
 
+    public void setInitialRangeOffset(ITmfTimestamp initOffset) {
+        fInitialRangeOffset = initOffset;
+    }
+
+    @Override
+    public ITmfTimestamp getInitialRangeOffset() {
+        if (fInitialRangeOffset != null) {
+            return fInitialRangeOffset;
+        }
+        return super.getInitialRangeOffset();
+    }
+
     // ------------------------------------------------------------------------
     // Operators
     // ------------------------------------------------------------------------
 
     @Override
-    @SuppressWarnings("unchecked")
-    public TmfContext seekLocation(final ITmfLocation<?> location) {
+    public TmfContext seekEvent(final ITmfLocation location) {
         try {
             fLock.lock();
             try {
@@ -183,7 +236,7 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
                     long loc  = 0;
                     long rank = 0;
                     if (location != null) {
-                        loc = ((TmfLocation<Long>) location).getLocation();
+                        loc = (Long) location.getLocationInfo();
                         rank = ITmfContext.UNKNOWN_RANK;
                     }
                     if (loc != fTrace.getFilePointer()) {
@@ -197,23 +250,23 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
             } catch (final NullPointerException e) {
                 e.printStackTrace();
             }
+            finally{
+                fLock.unlock();
+            }
         } catch (final NullPointerException e) {
             e.printStackTrace();
         }
-        finally{
-            fLock.unlock();
-        }
         return null;
     }
 
 
     @Override
-    public TmfContext seekLocation(final double ratio) {
+    public TmfContext seekEvent(final double ratio) {
         fLock.lock();
         try {
             if (fTrace != null) {
-                final ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length())));
-                final TmfContext context = seekLocation(location);
+                final ITmfLocation location = new TmfLongLocation(Long.valueOf(Math.round(ratio * fTrace.length())));
+                final TmfContext context = seekEvent(location);
                 context.setRank(ITmfContext.UNKNOWN_RANK);
                 return context;
             }
@@ -227,13 +280,14 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     }
 
     @Override
-    @SuppressWarnings("rawtypes")
-    public double getLocationRatio(final ITmfLocation location) {
+    public double getLocationRatio(ITmfLocation location) {
         fLock.lock();
         try {
-            if (fTrace != null)
-                if (location.getLocation() instanceof Long)
-                    return (double) ((Long) location.getLocation()) / fTrace.length();
+            if (fTrace != null) {
+                if (location.getLocationInfo() instanceof Long) {
+                    return (double) ((Long) location.getLocationInfo()) / fTrace.length();
+                }
+            }
         } catch (final IOException e) {
             e.printStackTrace();
         } finally {
@@ -243,11 +297,12 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     }
 
     @Override
-    public TmfLocation<Long> getCurrentLocation() {
+    public ITmfLocation getCurrentLocation() {
         fLock.lock();
         try {
-            if (fTrace != null)
-                return new TmfLocation<Long>(fTrace.getFilePointer());
+            if (fTrace != null) {
+                return new TmfLongLocation(fTrace.getFilePointer());
+            }
         } catch (final IOException e) {
             e.printStackTrace();
         } finally {
@@ -261,19 +316,21 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
         fLock.lock();
         try {
             // parseNextEvent will update the context
-            if (fTrace != null) {
-                final ITmfEvent event = fParser.parseNextEvent(this, context.clone());
+            if (fTrace != null && getParser() != null && context != null) {
+                final ITmfEvent event = getParser().parseEvent(context.clone());
                 return event;
             }
-        }
-        catch (final IOException e) {
-            e.printStackTrace();
         } finally {
             fLock.unlock();
         }
         return null;
     }
 
+    @Override
+    public synchronized void setNbEvents(final long nbEvents) {
+        super.setNbEvents(nbEvents);
+    }
+
     @Override
     public void setTimeRange(final TmfTimeRange range) {
         super.setTimeRange(range);
@@ -290,7 +347,12 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     }
 
     @Override
-    public void dispose() {
+    public void setStreamingInterval(final long interval) {
+        super.setStreamingInterval(interval);
+    }
+
+    @Override
+    public synchronized void dispose() {
         fLock.lock();
         try {
             if (fTrace != null) {
@@ -305,4 +367,12 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
         super.dispose();
     }
 
-}
\ No newline at end of file
+    /* (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
+     */
+    @Override
+    public boolean validate(IProject project, String path) {
+        return fileExists(path);
+    }
+
+}
This page took 0.030892 seconds and 5 git commands to generate.