Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfIteratorManager.java
index b6c2977ade9bb0b7ca69d9c584268d4ea41921c0..65fac21e59080277a60dded2c8dec51d633085ac 100644 (file)
@@ -1,12 +1,14 @@
 /*******************************************************************************
- * Copyright (c) 2012 Ericsson
+ * Copyright (c) 2012, 2013 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: Matthew Khouzam - Initial API and implementation
+ * Contributors:
+ *   Matthew Khouzam - Initial API and implementation
+ *   Simon Delisle - Added a method to remove the iterator
  *******************************************************************************/
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
 
@@ -51,7 +53,10 @@ public abstract class CtfIteratorManager {
      *            the trace to register.
      */
     public static synchronized void removeTrace(final CtfTmfTrace trace) {
-        map.remove(trace);
+        CtfTraceManager mgr = map.remove(trace);
+        if (mgr != null) {
+            mgr.clear();
+        }
     }
 
     /**
@@ -62,11 +67,28 @@ public abstract class CtfIteratorManager {
      * @param ctx
      *            the context
      * @return the iterator
+     * @since 2.0
      */
     public static synchronized CtfIterator getIterator(final CtfTmfTrace trace,
-            final CtfTmfLightweightContext ctx) {
+            final CtfTmfContext ctx) {
         return map.get(trace).getIterator(ctx);
     }
+
+    /**
+     * Remove an iterator for a given trace and context
+     *
+     * @param trace
+     *            the trace
+     * @param ctx
+     *            the context
+     * @since 2.1
+     */
+    public static synchronized void removeIterator(final CtfTmfTrace trace, final CtfTmfContext ctx) {
+        CtfTraceManager traceManager = map.get(trace);
+        if (traceManager != null) {
+            traceManager.removeIterator(ctx);
+        }
+    }
 }
 
 /**
@@ -83,11 +105,11 @@ class CtfTraceManager {
     /*
      * The map of the cache.
      */
-    private final HashMap<CtfTmfLightweightContext, CtfIterator> fMap;
+    private final HashMap<CtfTmfContext, CtfIterator> fMap;
     /*
      * An array pointing to the same cache. this allows fast "random" accesses.
      */
-    private final ArrayList<CtfTmfLightweightContext> fRandomAccess;
+    private final ArrayList<CtfTmfContext> fRandomAccess;
     /*
      * The parent trace
      */
@@ -98,8 +120,8 @@ class CtfTraceManager {
     private final Random fRnd;
 
     public CtfTraceManager(CtfTmfTrace trace) {
-        fMap = new HashMap<CtfTmfLightweightContext, CtfIterator>();
-        fRandomAccess = new ArrayList<CtfTmfLightweightContext>();
+        fMap = new HashMap<CtfTmfContext, CtfIterator>();
+        fRandomAccess = new ArrayList<CtfTmfContext>();
         fRnd = new Random(System.nanoTime());
         fTrace = trace;
     }
@@ -119,7 +141,7 @@ class CtfTraceManager {
      *            the context to look up
      * @return the iterator refering to the context
      */
-    public CtfIterator getIterator(final CtfTmfLightweightContext context) {
+    public CtfIterator getIterator(final CtfTmfContext context) {
         /*
          * if the element is in the map, we don't need to do anything else.
          */
@@ -133,7 +155,7 @@ class CtfTraceManager {
                 /*
                  * if we're not full yet, just add an element.
                  */
-                retVal = new CtfIterator(fTrace);
+                retVal = fTrace.createIterator();
                 addElement(context, retVal);
 
             } else {
@@ -142,12 +164,19 @@ class CtfTraceManager {
                  */
                 retVal = replaceRandomElement(context);
             }
-            final CtfLocationData location = (CtfLocationData) context.getLocation().getLocationInfo();
-            retVal.seek( location);
+            if (context.getLocation() != null) {
+                final CtfLocationInfo location = (CtfLocationInfo) context.getLocation().getLocationInfo();
+                retVal.seek(location);
+            }
         }
         return retVal;
     }
 
+    public void removeIterator(CtfTmfContext context) {
+        fMap.remove(context);
+        fRandomAccess.remove(context);
+    }
+
     /**
      * Add a pair of context and element to the hashmap and the arraylist.
      *
@@ -156,7 +185,7 @@ class CtfTraceManager {
      * @param elem
      *            the iterator
      */
-    private void addElement(final CtfTmfLightweightContext context,
+    private void addElement(final CtfTmfContext context,
             final CtfIterator elem) {
         fMap.put(context, elem);
         fRandomAccess.add(context);
@@ -170,7 +199,7 @@ class CtfTraceManager {
      * @return the iterator of the removed elements.
      */
     private CtfIterator replaceRandomElement(
-            final CtfTmfLightweightContext context) {
+            final CtfTmfContext context) {
         /*
          * This needs some explanation too: We need to select a random victim
          * and remove it. The order of the elements is not important, so instead
@@ -180,11 +209,18 @@ class CtfTraceManager {
          */
         final int size = fRandomAccess.size();
         final int pos = fRnd.nextInt(size);
-        final CtfTmfLightweightContext victim = fRandomAccess.get(pos);
+        final CtfTmfContext victim = fRandomAccess.get(pos);
         fRandomAccess.set(pos, context);
         final CtfIterator elem = fMap.remove(victim);
         fMap.put(context, elem);
         return elem;
     }
 
+    void clear() {
+        for (CtfIterator iterator : fMap.values()) {
+            iterator.dispose();
+        }
+        fMap.clear();
+        fRandomAccess.clear();
+    }
 }
This page took 0.027725 seconds and 5 git commands to generate.