ss: Bug 486689: Add methods for getting an optional attribute quark
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / StateSystem.java
index 09ce7cbef3cdc3b0a3372e306df3fed70516d5f4..e11bccc437f0f657d36b2073ebaf7b316abca698 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2015 Ericsson
+ * Copyright (c) 2012, 2016 Ericsson
  * Copyright (c) 2010, 2011 École Polytechnique de Montréal
  * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
  *
@@ -19,12 +19,14 @@ import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
 import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
@@ -234,17 +236,35 @@ public class StateSystem implements ITmfStateSystemBuilder {
     @Override
     public int getQuarkAbsolute(String... attribute)
             throws AttributeNotFoundException {
-        return getAttributeTree().getQuarkDontAdd(-1, attribute);
+        int quark = getAttributeTree().getQuarkDontAdd(ROOT_ATTRIBUTE, attribute);
+        if (quark == INVALID_ATTRIBUTE) {
+            throw new AttributeNotFoundException(getSSID() + " Path:" + Arrays.toString(attribute)); //$NON-NLS-1$
+        }
+        return quark;
+    }
+
+    @Override
+    public int optQuarkAbsolute(String... attribute) {
+        return getAttributeTree().getQuarkDontAdd(ROOT_ATTRIBUTE, attribute);
     }
 
     @Override
     public int getQuarkAbsoluteAndAdd(String... attribute) {
-        return getAttributeTree().getQuarkAndAdd(-1, attribute);
+        return getAttributeTree().getQuarkAndAdd(ROOT_ATTRIBUTE, attribute);
     }
 
     @Override
     public int getQuarkRelative(int startingNodeQuark, String... subPath)
             throws AttributeNotFoundException {
+        int quark = getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath);
+        if (quark == INVALID_ATTRIBUTE) {
+            throw new AttributeNotFoundException(getSSID() + " Quark:" + startingNodeQuark + ", SubPath:" + Arrays.toString(subPath)); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+        return quark;
+    }
+
+    @Override
+    public int optQuarkRelative(int startingNodeQuark, String... subPath) {
         return getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath);
     }
 
@@ -279,8 +299,8 @@ public class StateSystem implements ITmfStateSystemBuilder {
     }
 
     @Override
-    public List<Integer> getQuarks(String... pattern) {
-        List<Integer> quarks = new LinkedList<>();
+    public List<@NonNull Integer> getQuarks(String... pattern) {
+        List<@NonNull Integer> quarks = new LinkedList<>();
         List<String> prefix = new LinkedList<>();
         List<String> suffix = new LinkedList<>();
         boolean split = false;
@@ -329,20 +349,24 @@ public class StateSystem implements ITmfStateSystemBuilder {
             return quarks;
         }
 
-        try {
-            if (prefix.size() == 0) {
-                /*
-                 * If 'prefix' is empty, this means the wildcard was the first
-                 * element. Look for the root node's sub-attributes.
-                 */
-                startingAttribute = -1;
-            } else {
-                startingAttribute = getQuarkAbsolute(prefixStr);
+        if (prefix.isEmpty()) {
+            /*
+             * If 'prefix' is empty, this means the wildcard was the first
+             * element. Look for the root node's sub-attributes.
+             */
+            startingAttribute = ROOT_ATTRIBUTE;
+        } else {
+            startingAttribute = optQuarkAbsolute(prefixStr);
+            if (startingAttribute == INVALID_ATTRIBUTE) {
+                /* That attribute path did not exist, return the empty array */
+                return quarks;
             }
+        }
+        try {
             directChildren = getSubAttributes(startingAttribute, false);
         } catch (AttributeNotFoundException e) {
-            /* That attribute path did not exist, return the empty array */
-            return quarks;
+            /* Should not happen, starting attribute is a valid quark */
+            throw new IllegalStateException();
         }
 
         /*
@@ -350,13 +374,10 @@ public class StateSystem implements ITmfStateSystemBuilder {
          * 'suffix' part of the initial pattern.
          */
         for (int childQuark : directChildren) {
-            int matchingQuark;
-            try {
-                matchingQuark = getQuarkRelative(childQuark, suffixStr);
-            } catch (AttributeNotFoundException e) {
-                continue;
+            int matchingQuark = optQuarkRelative(childQuark, suffixStr);
+            if (matchingQuark != INVALID_ATTRIBUTE) {
+                quarks.add(matchingQuark);
             }
-            quarks.add(matchingQuark);
         }
 
         return quarks;
@@ -544,7 +565,7 @@ public class StateSystem implements ITmfStateSystemBuilder {
      * @param newStateIntervals
      *            The new List of state values to use as ongoing state info
      */
-    protected void replaceOngoingState(@NonNull List<ITmfStateInterval> newStateIntervals) {
+    protected void replaceOngoingState(@NonNull List<@NonNull ITmfStateInterval> newStateIntervals) {
         transState.replaceOngoingState(newStateIntervals);
     }
 
@@ -560,7 +581,7 @@ public class StateSystem implements ITmfStateSystemBuilder {
         }
 
         final int nbAttr = getNbAttributes();
-        List<ITmfStateInterval> stateInfo = new ArrayList<>(nbAttr);
+        List<@Nullable ITmfStateInterval> stateInfo = new ArrayList<>(nbAttr);
 
         /* Bring the size of the array to the current number of attributes */
         for (int i = 0; i < nbAttr; i++) {
This page took 0.02651 seconds and 5 git commands to generate.