Add option to retrieve sub-attributes recursively
authorAlexandre Montplaisir <alexandre.montplaisir@polymtl.ca>
Tue, 20 Mar 2012 18:30:06 +0000 (14:30 -0400)
committerFrancois Chouinard <fchouinard@gmail.com>
Tue, 27 Mar 2012 17:52:21 +0000 (13:52 -0400)
Signed-off-by: Alexandre Montplaisir <alexandre.montplaisir@polymtl.ca>
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/AttributeTree.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/StateSystem.java

index baa45280b545e3c95dfa03ddf4315b95ae8f79df..9c5d8fcb63b1810e37ea616013cd3e1a1e220874 100644 (file)
@@ -285,34 +285,43 @@ final class AttributeTree {
      * Returns the sub-attributes of the quark passed in parameter
      * 
      * @param attributeQuark
+     * @param recursive
      * @return
      * @throws AttributeNotFoundException
      */
-    List<Integer> getSubAttributes(int attributeQuark)
+    List<Integer> getSubAttributes(int attributeQuark, boolean recursive)
             throws AttributeNotFoundException {
         List<Integer> listOfChildren = new ArrayList<Integer>();
         Attribute startingAttribute;
-        
+
         /* Check if the quark is valid */
-        if ( attributeQuark < 0 || attributeQuark >= attributeList.size()) {
+        if (attributeQuark < 0 || attributeQuark >= attributeList.size()) {
             throw new AttributeNotFoundException();
         }
-        
+
         /* Set up the node from which we'll start the search */
-        if ( attributeQuark == -1 ) {
+        if (attributeQuark == -1) {
             startingAttribute = attributeTreeRoot;
         } else {
             startingAttribute = attributeList.get(attributeQuark);
         }
-        
+
         /* Iterate through the sub-attributes and add them to the list */
-        for (Attribute childNode : startingAttribute.getSubAttributesList()) {
-            listOfChildren.add(childNode.getQuark());
-        }
+        addSubAttributes(listOfChildren, startingAttribute, recursive);
 
         return listOfChildren;
     }
 
+    private void addSubAttributes(List<Integer> list, Attribute curAttribute,
+            boolean recursive) {
+        for (Attribute childNode : curAttribute.getSubAttributesList()) {
+            list.add(childNode.getQuark());
+            if (recursive) {
+                addSubAttributes(list, childNode, true);
+            }
+        }
+    }
+
     String getFullAttributeName(int quark) {
         if (quark >= attributeList.size() || quark < 0) {
             return null;
index 5608e34437c4afe4074e4755414d717c41b697a0..30edbb34128b43b5318100b877df6a4b0f133fcb 100644 (file)
@@ -141,13 +141,16 @@ public class StateSystem {
      * @param quark
      *            The attribute of which you want to sub-attributes. You can use
      *            "-1" here to specify the root node.
+     * @param recursive
+     *            True if you want all recursive sub-attributes, false if you
+     *            only want the first level.
      * @return A List of integers, matching the quarks of the sub-attributes.
      * @throws AttributeNotFoundException
      *             If the quark was not existing or invalid.
      */
-    public List<Integer> getSubAttributes(int quark)
+    public List<Integer> getSubAttributes(int quark, boolean recursive)
             throws AttributeNotFoundException {
-        return attributeTree.getSubAttributes(quark);
+        return attributeTree.getSubAttributes(quark, recursive);
     }
 
     /**
@@ -337,8 +340,13 @@ public class StateSystem {
     public void removeAttribute(long t, int attributeQuark)
             throws TimeRangeException, AttributeNotFoundException {
         assert (attributeQuark >= 0);
-        /* "Nullify our children first, recursively */
-        List<Integer> childAttributes = attributeTree.getSubAttributes(attributeQuark);
+        List<Integer> childAttributes;
+
+        /*
+         * "Nullify our children first, recursively. We pass 'false' because we
+         * handle the recursion ourselves.
+         */
+        childAttributes = attributeTree.getSubAttributes(attributeQuark, false);
         for (Integer childNodeQuark : childAttributes) {
             assert (attributeQuark != childNodeQuark);
             removeAttribute(t, childNodeQuark);
This page took 0.030988 seconds and 5 git commands to generate.