analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / model / readwrite / TmfXmlReadWriteStateValue.java
index 4d7994ad495f2fceb2fcbcd2fddb204fb86a44b8..a9264234a6f0cce75b943cc842c84fed7403fb49 100644 (file)
@@ -15,6 +15,7 @@ package org.eclipse.tracecompass.tmf.analysis.xml.core.model.readwrite;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
@@ -75,12 +76,12 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
         this(modelFactory, node, container, new ArrayList<ITmfXmlStateAttribute>(), eventField);
     }
 
-    private TmfXmlReadWriteStateValue(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container, List<ITmfXmlStateAttribute> attributes, String eventField) {
+    private TmfXmlReadWriteStateValue(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container, List<ITmfXmlStateAttribute> attributes, @Nullable String eventField) {
         super(modelFactory, node, container, attributes, eventField);
     }
 
     @Override
-    protected ITmfStateSystemBuilder getStateSystem() {
+    protected @Nullable ITmfStateSystemBuilder getStateSystem() {
         return (ITmfStateSystemBuilder) super.getStateSystem();
     }
 
@@ -90,6 +91,9 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
         /* Process the XML Element state value */
         String type = node.getAttribute(TmfXmlStrings.TYPE);
         String value = getSsContainer().getAttributeValue(node.getAttribute(TmfXmlStrings.VALUE));
+        if (value == null) {
+            throw new IllegalStateException();
+        }
 
         switch (type) {
         case TmfXmlStrings.TYPE_INT: {
@@ -133,6 +137,9 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
             List<Element> children = XmlUtils.getChildElements(node);
             List<ITmfXmlStateAttribute> childAttributes = new ArrayList<>();
             for (Element child : children) {
+                if (child == null) {
+                    continue;
+                }
                 ITmfXmlStateAttribute queryAttribute = modelFactory.createStateAttribute(child, getSsContainer());
                 childAttributes.add(queryAttribute);
             }
@@ -166,25 +173,58 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
 
         @Override
         protected void processValue(int quark, long timestamp, ITmfStateValue value) throws AttributeNotFoundException, TimeRangeException, StateValueTypeException {
+            ITmfStateSystemBuilder ss = getStateSystem();
+            if (ss == null) {
+                throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
+            }
             switch (getStackType()) {
             case POP:
-                getStateSystem().popAttribute(timestamp, quark);
+                ss.popAttribute(timestamp, quark);
                 break;
             case PUSH:
-                getStateSystem().pushAttribute(timestamp, value, quark);
+                ss.pushAttribute(timestamp, value, quark);
                 break;
             case NULL:
             case PEEK:
             default:
-                getStateSystem().modifyAttribute(timestamp, value, quark);
+                ss.modifyAttribute(timestamp, value, quark);
                 break;
             }
         }
 
         @Override
         protected void incrementValue(ITmfEvent event, int quark, long timestamp) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
-            getStateSystem().incrementAttribute(timestamp, quark);
+            ITmfStateSystemBuilder ss = getStateSystem();
+            if (ss == null) {
+                throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
+            }
+            ss.incrementAttribute(timestamp, quark);
+        }
+    }
+
+    private static @Nullable ITmfStateValue incrementByType(int quark, ITmfStateSystem ss, ITmfStateValue stateValue) throws AttributeNotFoundException {
+        ITmfStateValue value = null;
+        switch (stateValue.getType()) {
+        case LONG: {
+            long incrementLong = stateValue.unboxLong();
+            ITmfStateValue currentState = ss.queryOngoingState(quark);
+            long currentValue = (currentState.isNull() ? 0 : currentState.unboxLong());
+            value = TmfStateValue.newValueLong(incrementLong + currentValue);
+            return value;
+        }
+        case INTEGER: {
+            int increment = stateValue.unboxInt();
+            ITmfStateValue currentState = ss.queryOngoingState(quark);
+            int currentValue = (currentState.isNull() ? 0 : currentState.unboxInt());
+            value = TmfStateValue.newValueInt(increment + currentValue);
+            return value;
+        }
+        case DOUBLE:
+        case NULL:
+        case STRING:
+        default:
         }
+        return value;
     }
 
     /* This state value uses a constant value, defined in the XML */
@@ -197,36 +237,24 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
         }
 
         @Override
-        public ITmfStateValue getValue(ITmfEvent event) {
+        public ITmfStateValue getValue(@Nullable ITmfEvent event) {
             return fValue;
         }
 
         @Override
         public void incrementValue(ITmfEvent event, int quark, long timestamp) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
             ITmfStateSystem ss = getStateSystem();
-            switch (fValue.getType()) {
-            case LONG: {
-                long incrementLong = fValue.unboxLong();
-                long currentValue = ss.queryOngoingState(quark).unboxLong();
-                ITmfStateValue value = TmfStateValue.newValueLong(incrementLong + currentValue);
-                processValue(quark, timestamp, value);
-                return;
+            if (ss == null) {
+                throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
             }
-            case INTEGER: {
-                int increment = fValue.unboxInt();
-                int currentValue = ss.queryOngoingState(quark).unboxInt();
-                ITmfStateValue value = TmfStateValue.newValueInt(increment + currentValue);
+            ITmfStateValue value = incrementByType(quark, ss, fValue);
+            if (value != null) {
                 processValue(quark, timestamp, value);
-                break;
-            }
-            case DOUBLE:
-            case NULL:
-            case STRING:
-            default:
+            } else {
                 Activator.logWarning("TmfXmlStateValue: The increment value is not a number type"); //$NON-NLS-1$
-                break;
             }
         }
+
     }
 
     /* The state value uses the value of an event field */
@@ -239,7 +267,7 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
         }
 
         @Override
-        public ITmfStateValue getValue(ITmfEvent event) {
+        public ITmfStateValue getValue(@Nullable ITmfEvent event) {
             if (event == null) {
                 Activator.logWarning("XML State value: requested an event field, but event is null"); //$NON-NLS-1$
                 return TmfStateValue.nullValue();
@@ -250,28 +278,15 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
         @Override
         public void incrementValue(ITmfEvent event, int quark, long timestamp) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
             ITmfStateSystem ss = getSsContainer().getStateSystem();
-            ITmfStateValue incrementValue = getValue(event);
-            switch (incrementValue.getType()) {
-            case INTEGER: {
-                int increment = incrementValue.unboxInt();
-                int currentValue = ss.queryOngoingState(quark).unboxInt();
-                ITmfStateValue value = TmfStateValue.newValueInt(increment + currentValue);
-                processValue(quark, timestamp, value);
-                break;
+            if (ss == null) {
+                throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
             }
-            case LONG: {
-                long incrementLong = incrementValue.unboxLong();
-                long currentValue = ss.queryOngoingState(quark).unboxLong();
-                ITmfStateValue value = TmfStateValue.newValueLong(incrementLong + currentValue);
+            ITmfStateValue incrementValue = getValue(event);
+            ITmfStateValue value = incrementByType(quark, ss, incrementValue);
+            if (value != null) {
                 processValue(quark, timestamp, value);
-                break;
-            }
-            case DOUBLE:
-            case NULL:
-            case STRING:
-            default:
+            } else {
                 Activator.logWarning(String.format("TmfXmlStateValue: The event field increment %s is not a number type but a %s", fFieldName, incrementValue.getType())); //$NON-NLS-1$
-                break;
             }
         }
     }
@@ -280,12 +295,12 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
     private class TmfXmlStateValueEventName extends TmfXmlStateValueTypeReadWrite {
 
         @Override
-        public ITmfStateValue getValue(ITmfEvent event) {
+        public ITmfStateValue getValue(@Nullable ITmfEvent event) {
             if (event == null) {
                 Activator.logWarning("XML State value: request event name, but event is null"); //$NON-NLS-1$
                 return TmfStateValue.nullValue();
             }
-            return TmfStateValue.newValueString(event.getType().getName());
+            return TmfStateValue.newValueString(event.getName());
         }
 
     }
@@ -294,7 +309,7 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
     private class TmfXmlStateValueDelete extends TmfXmlStateValueTypeReadWrite {
 
         @Override
-        public ITmfStateValue getValue(ITmfEvent event) throws AttributeNotFoundException {
+        public ITmfStateValue getValue(@Nullable ITmfEvent event) throws AttributeNotFoundException {
             return TmfStateValue.nullValue();
         }
 
@@ -320,11 +335,14 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
         }
 
         @Override
-        public ITmfStateValue getValue(ITmfEvent event) throws AttributeNotFoundException {
+        public ITmfStateValue getValue(@Nullable ITmfEvent event) throws AttributeNotFoundException {
             /* Query the state system for the value */
             ITmfStateValue value = TmfStateValue.nullValue();
             int quarkQuery = IXmlStateSystemContainer.ROOT_QUARK;
             ITmfStateSystem ss = getStateSystem();
+            if (ss == null) {
+                throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
+            }
 
             for (ITmfXmlStateAttribute attribute : fQueryValue) {
                 quarkQuery = attribute.getAttributeQuark(event, quarkQuery);
@@ -339,6 +357,9 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
              */
             if (quarkQuery != IXmlStateSystemContainer.ERROR_QUARK) {
                 value = ss.queryOngoingState(quarkQuery);
+                if (value == null) {
+                    throw new IllegalStateException();
+                }
             }
             return value;
         }
@@ -346,28 +367,16 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
         @Override
         public void incrementValue(ITmfEvent event, int quark, long timestamp) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
             ITmfStateSystem ss = getStateSystem();
-            ITmfStateValue incrementValue = getValue(event);
-            switch (incrementValue.getType()) {
-            case INTEGER: {
-                int increment = incrementValue.unboxInt();
-                int currentValue = ss.queryOngoingState(quark).unboxInt();
-                ITmfStateValue value = TmfStateValue.newValueInt(increment + currentValue);
-                processValue(quark, timestamp, value);
-                break;
+            if (ss == null) {
+                throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
             }
-            case LONG: {
-                long incrementLong = incrementValue.unboxLong();
-                long currentValue = ss.queryOngoingState(quark).unboxLong();
-                ITmfStateValue value = TmfStateValue.newValueLong(incrementLong + currentValue);
+
+            ITmfStateValue incrementValue = getValue(event);
+            ITmfStateValue value = incrementByType(quark, ss, incrementValue);
+            if (value != null) {
                 processValue(quark, timestamp, value);
-                break;
-            }
-            case DOUBLE:
-            case NULL:
-            case STRING:
-            default:
+            } else {
                 Activator.logWarning("TmfXmlStateValue: The query result increment is not a number type"); //$NON-NLS-1$
-                break;
             }
         }
     }
This page took 0.035482 seconds and 5 git commands to generate.