Remove the generic location (replace by Comparable)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / tests / stubs / trace / TmfTraceStub.java
index 2ac183c73818a0838cd0520da3c07e62a537e9c6..fed0590eb3105e8f4fe7d50cbf20aa23c49fb5a9 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,16 +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;
 
 /**
@@ -34,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
@@ -44,22 +48,27 @@ 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 ReentrantLock fLock = new ReentrantLock();
-    
+    private final ReentrantLock fLock = new ReentrantLock();
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
 
+    public TmfTraceStub() {
+        super();
+        setParser(new TmfEventParserStub(this));
+    }
+
     /**
      * @param path
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(String path) throws FileNotFoundException {
-        this(path, DEFAULT_INDEX_PAGE_SIZE, false);
+    public TmfTraceStub(final String path) throws TmfTraceException {
+        this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false);
     }
 
     /**
@@ -67,32 +76,78 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @param cacheSize
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(String path, int cacheSize) throws FileNotFoundException {
+    public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException {
         this(path, cacheSize, false);
     }
 
     /**
      * @param path
+     * @param cacheSize
+     * @throws FileNotFoundException
+     */
+    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));
+    }
+
+    /**
+     * @param path
+     * @param cacheSize
+     * @throws FileNotFoundException
+     */
+    public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer indexer) throws TmfTraceException {
+        this(path, cacheSize, false, null, indexer);
+    }
+
+    /**
+     * @param path
+     * @param waitForCompletion
+     * @throws FileNotFoundException
+     */
+    public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException {
+        this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
+    }
+
+    /**
+     * @param path
+     * @param cacheSize
      * @param waitForCompletion
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(String path, boolean waitForCompletion) throws FileNotFoundException {
-        this(path, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
+    public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
+        super(null, TmfEvent.class, path, cacheSize);
+        try {
+            fTrace = new RandomAccessFile(path, "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser(new TmfEventParserStub(this));
+        if (waitForCompletion) {
+            indexTrace();
+        }
     }
-    
+
     /**
      * @param path
      * @param cacheSize
      * @param waitForCompletion
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(String path, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
-        super(null, TmfEvent.class, path, cacheSize, false);
-        fTrace = new RandomAccessFile(path, "r");
-        fParser = new TmfEventParserStub();
+    public TmfTraceStub(final IResource resource,  final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
+        super(resource, TmfEvent.class, path, cacheSize);
+        try {
+            fTrace = new RandomAccessFile(path, "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser(new TmfEventParserStub(this));
     }
 
-    
     /**
      * @param path
      * @param cacheSize
@@ -100,34 +155,50 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
      * @param parser
      * @throws FileNotFoundException
      */
-    public TmfTraceStub(String path, int cacheSize, boolean waitForCompletion, ITmfEventParser<TmfEvent> parser) throws FileNotFoundException {
-        super(path, TmfEvent.class, path, cacheSize, false);
-        fTrace = new RandomAccessFile(path, "r");
-        fParser = parser;
+    public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion,
+            final ITmfEventParser parser, final ITmfTraceIndexer indexer) throws TmfTraceException {
+        super(null, TmfEvent.class, path, cacheSize, 0, indexer);
+        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 TmfTraceException {
+        super(trace);
+        try {
+            fTrace = new RandomAccessFile(getPath(), "r");
+        } catch (FileNotFoundException e) {
+            throw new TmfTraceException(e.getMessage());
+        }
+        setParser(new TmfEventParserStub(this));
+    }
+
+    public void indexTrace() {
+        indexTrace(true);
+    }
+
     @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;
+    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 ITmfTrace<TmfEvent> copy() {
-               ITmfTrace<TmfEvent> returnedValue = null;
-               returnedValue = clone();
-               return returnedValue;
-       }
-    
+    public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
+        super.initialize(resource, path, type);
+    }
+
     // ------------------------------------------------------------------------
     // Accessors
     // ------------------------------------------------------------------------
@@ -140,66 +211,70 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
     // Operators
     // ------------------------------------------------------------------------
 
-       @Override
-       @SuppressWarnings("unchecked")
-       public TmfContext seekLocation(ITmfLocation<?> location) {
-           fLock.lock();
+    @Override
+    public TmfContext seekEvent(final ITmfLocation location) {
         try {
-            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;
+            fLock.lock();
+            try {
+                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 = (Long) location.getLocationData();
+                        rank = ITmfContext.UNKNOWN_RANK;
+                    }
+                    if (loc != fTrace.getFilePointer()) {
+                        fTrace.seek(loc);
+                    }
+                    final TmfContext context = new TmfContext(getCurrentLocation(), rank);
+                    return context;
                 }
-                if (loc != fTrace.getFilePointer()) {
-                    fTrace.seek(loc);
-                }
-                TmfContext context = new TmfContext(getCurrentLocation(), rank);
-                return context;
+            } catch (final IOException e) {
+                e.printStackTrace();
+            } catch (final NullPointerException e) {
+                e.printStackTrace();
             }
-               } catch (IOException e) {
-                       e.printStackTrace();
-               }
-        finally{
-            fLock.unlock();
+            finally{
+                fLock.unlock();
+            }
+        } catch (final NullPointerException e) {
+            e.printStackTrace();
         }
-               return null;
+        return null;
     }
 
 
-       @Override
-    public TmfContext seekLocation(double ratio) {
-           fLock.lock();
+    @Override
+    public TmfContext seekEvent(final double ratio) {
+        fLock.lock();
         try {
             if (fTrace != null) {
-                ITmfLocation<?> location = new TmfLocation<Long>(Long.valueOf((long) (ratio * fTrace.length())));
-                TmfContext context = seekLocation(location);
+                final ITmfLocation location = new TmfLongLocation(Long.valueOf((long) (ratio * fTrace.length())));
+                final TmfContext context = seekEvent(location);
                 context.setRank(ITmfContext.UNKNOWN_RANK);
                 return context;
             }
-        } catch (IOException e) {
+        } catch (final 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();
+                if (location.getLocationData() instanceof Long) {
+                    return (double) ((Long) location.getLocationData()) / fTrace.length();
                 }
             }
-        } catch (IOException e) {
+        } catch (final IOException e) {
             e.printStackTrace();
         } finally {
             fLock.unlock();
@@ -208,13 +283,13 @@ 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());
+                return new TmfLongLocation(fTrace.getFilePointer());
             }
-        } catch (IOException e) {
+        } catch (final IOException e) {
             e.printStackTrace();
         } finally {
             fLock.unlock();
@@ -222,53 +297,68 @@ public class TmfTraceStub extends TmfTrace<TmfEvent> {
         return null;
     }
 
-       @Override
-       public ITmfEvent parseEvent(ITmfContext context) {
-           fLock.lock();
-               try {
-                       // parseNextEvent will update the context
-                   if (fTrace != null) {
-                       ITmfEvent 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 ITmfEvent parseEvent(final ITmfContext context) {
+        fLock.lock();
+        try {
+            // parseNextEvent will update the context
+            if (fTrace != null && getParser() != null && context != null) {
+                final ITmfEvent event = getParser().parseEvent(context.clone());
+                return event;
+            }
+        } 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);
+    }
+
+    @Override
+    public void setStartTime(final ITmfTimestamp startTime) {
+        super.setStartTime(startTime);
     }
 
-       @Override
-       public void setStartTime(ITmfTimestamp startTime) {
-       super.setStartTime(startTime);
+    @Override
+    public void setEndTime(final ITmfTimestamp endTime) {
+        super.setEndTime(endTime);
     }
 
-       @Override
-       public void setEndTime(ITmfTimestamp endTime) {
-       super.setEndTime(endTime);
+    @Override
+    public void setStreamingInterval(final long interval) {
+        super.setStreamingInterval(interval);
     }
-       
-       @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
+
+    @Override
+    public synchronized void dispose() {
+        fLock.lock();
+        try {
+            if (fTrace != null) {
+                fTrace.close();
+                fTrace = null;
+            }
+        } catch (final IOException e) {
+            // Ignore
+        } finally {
+            fLock.unlock();
+        }
+        super.dispose();
+    }
+
+    /* (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.029468 seconds and 5 git commands to generate.