Merge master in TmfTraceModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / events / TmfEventsCache.java
index 69b882a99ca2130c703475e8eb277a76d9e272ca..adefd6f8f64301b2a9dce9e2a1e6e7b41f7ab8fe 100644 (file)
@@ -1,11 +1,11 @@
 /*******************************************************************************\r
  * Copyright (c) 2011 Ericsson\r
- * \r
+ *\r
  * All rights reserved. This program and the accompanying materials are\r
  * made available under the terms of the Eclipse Public License v1.0 which\r
  * accompanies this distribution, and is available at\r
  * http://www.eclipse.org/legal/epl-v10.html\r
- * \r
+ *\r
  * Contributors:\r
  *   Patrick Tasse - Initial API and implementation\r
  ******************************************************************************/\r
@@ -19,43 +19,43 @@ import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;\r
 import org.eclipse.core.runtime.jobs.Job;\r
 import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider;\r
-import org.eclipse.linuxtools.tmf.core.event.TmfEvent;\r
+import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;\r
 import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter;\r
 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;\r
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;\r
 \r
 public class TmfEventsCache {\r
 \r
-       public class CachedEvent {\r
-               TmfEvent event;\r
+       public static class CachedEvent {\r
+               ITmfEvent event;\r
                long rank;\r
 \r
-               public CachedEvent (TmfEvent event, long rank) {\r
-                       this.event = event;\r
+               public CachedEvent (ITmfEvent iTmfEvent, long rank) {\r
+                       this.event = iTmfEvent;\r
                        this.rank = rank;\r
                }\r
        }\r
 \r
-    private CachedEvent[] fCache;\r
+    private final CachedEvent[] fCache;\r
     private int fCacheStartIndex = 0;\r
     private int fCacheEndIndex   = 0;\r
 \r
     private ITmfTrace<?> fTrace;\r
-    private TmfEventsTable fTable;\r
+    private final TmfEventsTable fTable;\r
     private ITmfFilter fFilter;\r
-    private ArrayList<Integer> fFilterIndex = new ArrayList<Integer>(); // contains the event rank at each 'cache size' filtered events\r
+    private final ArrayList<Integer> fFilterIndex = new ArrayList<Integer>(); // contains the event rank at each 'cache size' filtered events\r
 \r
     public TmfEventsCache(int cacheSize, TmfEventsTable table) {\r
        fCache = new CachedEvent[cacheSize];\r
        fTable = table;\r
     }\r
-    \r
+\r
     public void setTrace(ITmfTrace<?> trace) {\r
        fTrace = trace;\r
        clear();\r
     }\r
-    \r
-    public void clear() {\r
+\r
+    public synchronized void clear() {\r
        fCacheStartIndex = 0;\r
        fCacheEndIndex = 0;\r
        fFilterIndex.clear();\r
@@ -65,13 +65,13 @@ public class TmfEventsCache {
        fFilter = filter;\r
        clear();\r
     }\r
-    \r
+\r
     public void clearFilter() {\r
        fFilter = null;\r
        clear();\r
     }\r
-    \r
-    public CachedEvent getEvent(int index) {\r
+\r
+    public synchronized CachedEvent getEvent(int index) {\r
         if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {\r
             int i = index - fCacheStartIndex;\r
             return fCache[i];\r
@@ -80,15 +80,15 @@ public class TmfEventsCache {
        return null;\r
     }\r
 \r
-    public CachedEvent peekEvent(int index) {\r
+    public synchronized CachedEvent peekEvent(int index) {\r
         if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {\r
             int i = index - fCacheStartIndex;\r
             return fCache[i];\r
         }\r
        return null;\r
     }\r
-    \r
-    public synchronized void storeEvent(TmfEvent event, long rank, int index) {\r
+\r
+    public synchronized void storeEvent(ITmfEvent event, long rank, int index) {\r
        if (fCacheStartIndex == fCacheEndIndex) {\r
                fCacheStartIndex = index;\r
                fCacheEndIndex = index;\r
@@ -100,28 +100,28 @@ public class TmfEventsCache {
                        fCacheEndIndex++;\r
                }\r
        }\r
-       if (fFilter != null && index % fCache.length == 0) {\r
+       if ((fFilter != null) && ((index % fCache.length) == 0)) {\r
                int i = index / fCache.length;\r
-               fFilterIndex.add(i, new Integer((int) rank));\r
+               fFilterIndex.add(i, Integer.valueOf((int) rank));\r
        }\r
     }\r
-    \r
+\r
     @SuppressWarnings("unchecked")\r
-    public int getFilteredEventIndex(final long rank) {\r
+    public synchronized int getFilteredEventIndex(final long rank) {\r
        int current;\r
        int startRank;\r
-       TmfDataRequest<TmfEvent> request;\r
+       TmfDataRequest<ITmfEvent> request;\r
        synchronized (this) {\r
                int start = 0;\r
                int end = fFilterIndex.size();\r
-               \r
-               if (fCacheEndIndex - fCacheStartIndex > 1) {\r
+\r
+               if ((fCacheEndIndex - fCacheStartIndex) > 1) {\r
                        if (rank < fCache[0].rank) {\r
-                               end = fCacheStartIndex / fCache.length + 1;\r
+                               end = (fCacheStartIndex / fCache.length) + 1;\r
                        } else if (rank > fCache[fCacheEndIndex - fCacheStartIndex - 1].rank) {\r
                                start = fCacheEndIndex / fCache.length;\r
                        } else {\r
-                               for (int i = 0; i < fCacheEndIndex - fCacheStartIndex; i++) {\r
+                               for (int i = 0; i < (fCacheEndIndex - fCacheStartIndex); i++) {\r
                                        if (fCache[i].rank >= rank) {\r
                                                return fCacheStartIndex + i;\r
                                        }\r
@@ -129,7 +129,7 @@ public class TmfEventsCache {
                                return fCacheEndIndex;\r
                        }\r
                }\r
-               \r
+\r
                current = (start + end) / 2;\r
                while (current != start) {\r
                        if (rank < fFilterIndex.get(current)) {\r
@@ -142,23 +142,25 @@ public class TmfEventsCache {
                }\r
                startRank = fFilterIndex.get(current);\r
        }\r
-       \r
+\r
        final int index = current * fCache.length;\r
-       \r
-       class DataRequest<T extends TmfEvent> extends TmfDataRequest<T> {\r
+\r
+       class DataRequest<T extends ITmfEvent> extends TmfDataRequest<T> {\r
                int fRank;\r
                int fIndex;\r
-               \r
+\r
                DataRequest(Class<T> dataType, int start, int nbRequested) {\r
                        super(dataType, start, nbRequested);\r
                        fRank = start;\r
                        fIndex = index;\r
                }\r
-               \r
+\r
                        @Override\r
                        public void handleData(T event) {\r
                                super.handleData(event);\r
-                               if (isCancelled()) return;\r
+                               if (isCancelled()) {\r
+                    return;\r
+                }\r
                                if (fRank >= rank) {\r
                                        cancel();\r
                                        return;\r
@@ -173,21 +175,21 @@ public class TmfEventsCache {
                    return fIndex;\r
             }\r
        }\r
-       \r
-       request = new DataRequest<TmfEvent>(TmfEvent.class, startRank, TmfDataRequest.ALL_DATA);\r
-               ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(request);\r
+\r
+       request = new DataRequest<ITmfEvent>(ITmfEvent.class, startRank, TmfDataRequest.ALL_DATA);\r
+               ((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);\r
                try {\r
                        request.waitForCompletion();\r
-                       return ((DataRequest<TmfEvent>) request).getFilteredIndex();\r
+                       return ((DataRequest<ITmfEvent>) request).getFilteredIndex();\r
                } catch (InterruptedException e) {\r
                }\r
        return 0;\r
     }\r
-    \r
+\r
     // ------------------------------------------------------------------------\r
     // Event cache population\r
     // ------------------------------------------------------------------------\r
-    \r
+\r
     // The event fetching job\r
     private Job job;\r
     private synchronized void populateCache(final int index) {\r
@@ -196,14 +198,14 @@ public class TmfEventsCache {
          * 1. The job must exist\r
          * 2. It must be running (i.e. not completed)\r
          * 3. The requested index must be within the cache range\r
-         * \r
+         *\r
          * If the job meets these conditions, we simply exit.\r
          * Otherwise, we create a new job but we might have to cancel\r
          * an existing job for an obsolete range.\r
          */\r
         if (job != null) {\r
             if (job.getState() != Job.NONE) {\r
-                if (index >= fCacheStartIndex && index < (fCacheStartIndex + fCache.length)) {\r
+                if ((index >= fCacheStartIndex) && (index < (fCacheStartIndex + fCache.length))) {\r
                     return;\r
                 }\r
                 // The new index is out of the requested range\r
@@ -211,7 +213,7 @@ public class TmfEventsCache {
                 job.cancel();\r
             }\r
         }\r
-        \r
+\r
         fCacheStartIndex = index;\r
         fCacheEndIndex   = index;\r
 \r
@@ -233,12 +235,12 @@ public class TmfEventsCache {
                                skipCount = index - (i * fCache.length);\r
                        }\r
                }\r
-               \r
-                TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(TmfEvent.class, startIndex, nbRequested) {\r
+\r
+                TmfDataRequest<ITmfEvent> request = new TmfDataRequest<ITmfEvent>(ITmfEvent.class, startIndex, nbRequested) {\r
                     private int count = 0;\r
                     private long rank = startIndex;\r
                     @Override\r
-                    public void handleData(TmfEvent event) {\r
+                    public void handleData(ITmfEvent event) {\r
                         // If the job is canceled, cancel the request so waitForCompletion() will unlock\r
                         if (monitor.isCanceled()) {\r
                             cancel();\r
@@ -246,7 +248,7 @@ public class TmfEventsCache {
                         }\r
                         super.handleData(event);\r
                         if (event != null) {\r
-                               if ((fFilter == null || fFilter.matches(event)) && skipCount-- <= 0) {\r
+                               if (((fFilter == null) || fFilter.matches(event)) && (skipCount-- <= 0)) {\r
                                        synchronized (TmfEventsCache.this) {\r
                                                fCache[count] = new CachedEvent(event.clone(), rank);\r
                                                count++;\r
@@ -259,14 +261,14 @@ public class TmfEventsCache {
                         }\r
                         if (count >= fCache.length) {\r
                                cancel();\r
-                        } else if (fFilter != null && count >= fTable.getTable().getItemCount() - 3) { // -1 for header row, -2 for top and bottom filter status rows\r
+                        } else if ((fFilter != null) && (count >= (fTable.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows\r
                                cancel();\r
                         }\r
                         rank++;\r
                     }\r
                 };\r
 \r
-                ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(request);\r
+                ((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);\r
                 try {\r
                     request.waitForCompletion();\r
                 } catch (InterruptedException e) {\r
@@ -274,7 +276,7 @@ public class TmfEventsCache {
                 }\r
 \r
                 fTable.cacheUpdated(true);\r
-                \r
+\r
                 // Flag the UI thread that the cache is ready\r
                 if (monitor.isCanceled()) {\r
                        return Status.CANCEL_STATUS;\r
This page took 0.029237 seconds and 5 git commands to generate.