analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / filter / model / TmfFilterCompareNode.java
index 4c9b4aed69b28760278855bcbb12b948a7989e66..8b6ae8b51dec93345f34a17c6b78afeb518936f5 100644 (file)
@@ -22,7 +22,6 @@ import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
 
-
 /**
  * Filter node for the comparison operation
  *
@@ -95,7 +94,7 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
      * @param result the compare result (-1, 0 or 1)
      */
     public void setResult(int result) {
-        this.fResult = result;
+        this.fResult = (int) Math.signum(result);
     }
 
     /**
@@ -131,15 +130,9 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
             return;
         }
         if (fType == Type.NUM) {
-            try {
-                fValueNumber = NumberFormat.getInstance().parse(value).doubleValue();
-            } catch (ParseException e) {
-            }
+            fValueNumber = toNumber(value);
         } else if (fType == Type.TIMESTAMP) {
-            try {
-                fValueTimestamp = new TmfNanoTimestamp(fTimestampFormat.parseValue(value.toString()));
-            } catch (ParseException e) {
-            }
+            fValueTimestamp = toTimestamp(value);
         }
     }
 
@@ -163,22 +156,24 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
     @Override
     public boolean matches(ITmfEvent event) {
         if (event == null || fEventAspect == null) {
-            return false ^ fNot;
+            return false;
         }
         Object value = fEventAspect.resolve(event);
         if (value == null) {
-            return false ^ fNot;
+            return false;
         }
         if (fType == Type.NUM) {
-            if (fValueNumber != null) {
-                if (value instanceof Number) {
-                    double valueDouble = ((Number) value).doubleValue();
-                    return (Double.compare(valueDouble, fValueNumber.doubleValue()) == fResult) ^ fNot;
+            if (fValueNumber instanceof Double) {
+                Number valueNumber = toNumber(value);
+                if (valueNumber != null) {
+                    return (Double.compare(valueNumber.doubleValue(), fValueNumber.doubleValue()) == fResult) ^ fNot;
                 }
-                try {
-                    double valueDouble = NumberFormat.getInstance().parse(value.toString()).doubleValue();
-                    return (Double.compare(valueDouble, fValueNumber.doubleValue()) == fResult) ^ fNot;
-                } catch (ParseException e) {
+            } else if (fValueNumber != null) {
+                Number valueNumber = toNumber(value);
+                if (valueNumber instanceof Double || valueNumber instanceof Float) {
+                    return (Double.compare(valueNumber.doubleValue(), fValueNumber.doubleValue()) == fResult) ^ fNot;
+                } else if (valueNumber != null) {
+                    return (Long.compare(valueNumber.longValue(), fValueNumber.longValue()) == fResult) ^ fNot;
                 }
             }
         } else if (fType == Type.ALPHA) {
@@ -187,18 +182,40 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
             return (comp == fResult) ^ fNot;
         } else if (fType == Type.TIMESTAMP) {
             if (fValueTimestamp != null) {
-                if (value instanceof ITmfTimestamp) {
-                    ITmfTimestamp valueTimestamp = (ITmfTimestamp) value;
-                    return (valueTimestamp.compareTo(fValueTimestamp) == fResult) ^ fNot;
-                }
-                try {
-                    ITmfTimestamp valueTimestamp = new TmfNanoTimestamp(fTimestampFormat.parseValue(value.toString()));
-                    return (valueTimestamp.compareTo(fValueTimestamp) == fResult) ^ fNot;
-                } catch (ParseException e) {
+                ITmfTimestamp valueTimestamp = toTimestamp(value);
+                if (valueTimestamp != null) {
+                    int comp = (int) Math.signum(valueTimestamp.compareTo(fValueTimestamp));
+                    return (comp == fResult) ^ fNot;
                 }
             }
         }
-        return false ^ fNot;
+        return false;
+    }
+
+    private static Number toNumber(Object value) {
+        if (value instanceof Number) {
+            return (Number) value;
+        }
+        try {
+            return Long.decode(value.toString());
+        } catch (NumberFormatException e) {
+        }
+        try {
+            return NumberFormat.getInstance().parse(value.toString());
+        } catch (ParseException e) {
+        }
+        return null;
+    }
+
+    private ITmfTimestamp toTimestamp(Object value) {
+        if (value instanceof ITmfTimestamp) {
+            return (ITmfTimestamp) value;
+        }
+        try {
+            return new TmfNanoTimestamp(fTimestampFormat.parseValue(value.toString()));
+        } catch (ParseException e) {
+        }
+        return null;
     }
 
     @Override
@@ -207,12 +224,11 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
     }
 
     @Override
-    public String toString() {
+    public String toString(boolean explicit) {
         String result = (fResult == 0 ? "= " : fResult < 0 ? "< " : "> "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
         String open = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "["); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
         String close = (fType == Type.NUM ? "" : fType == Type.ALPHA ? "\"" : "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-        String aspectName = fEventAspect != null ? fEventAspect.getName() : ""; //$NON-NLS-1$
-        return aspectName + (fNot ? " not " : " ") + result + open + fValue + close; //$NON-NLS-1$ //$NON-NLS-2$
+        return getAspectLabel(explicit) + (fNot ? " not " : " ") + result + open + fValue + close; //$NON-NLS-1$ //$NON-NLS-2$
     }
 
     @Override
@@ -221,46 +237,4 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
         clone.setValue(fValue);
         return clone;
     }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = super.hashCode();
-        result = prime * result + (fNot ? 1231 : 1237);
-        result = prime * result + fResult;
-        result = prime * result + ((fType == null) ? 0 : fType.hashCode());
-        result = prime * result + ((fValue == null) ? 0 : fValue.hashCode());
-        return result;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (!super.equals(obj)) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        TmfFilterCompareNode other = (TmfFilterCompareNode) obj;
-        if (fNot != other.fNot) {
-            return false;
-        }
-        if (fResult != other.fResult) {
-            return false;
-        }
-        if (fType != other.fType) {
-            return false;
-        }
-        if (fValue == null) {
-            if (other.fValue != null) {
-                return false;
-            }
-        } else if (!fValue.equals(other.fValue)) {
-            return false;
-        }
-        return true;
-    }
 }
This page took 0.027489 seconds and 5 git commands to generate.