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 502484718753eb08e92d47ba7a11343a0121d511..8b6ae8b51dec93345f34a17c6b78afeb518936f5 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2014 Ericsson
+ * Copyright (c) 2010, 2015 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -18,8 +18,9 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
-import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
-
+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
@@ -52,12 +53,14 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
         TIMESTAMP
     }
 
+
     private boolean fNot = false;
     private int fResult;
     private Type fType = Type.NUM;
     private String fValue;
     private transient Number fValueNumber;
-    private transient TmfTimestamp fValueTimestamp;
+    private transient ITmfTimestamp fValueTimestamp;
+    private transient TmfTimestampFormat fTimestampFormat = new TmfTimestampFormat("T.SSSSSSSSS"); //$NON-NLS-1$
 
     /**
      * @param parent the parent node
@@ -81,17 +84,17 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
     }
 
     /**
-     * @return the compare result
+     * @return the compare result (-1, 0 or 1)
      */
     public int getResult() {
         return fResult;
     }
 
     /**
-     * @param result the compare result
+     * @param result the compare result (-1, 0 or 1)
      */
     public void setResult(int result) {
-        this.fResult = result;
+        this.fResult = (int) Math.signum(result);
     }
 
     /**
@@ -110,14 +113,14 @@ public class TmfFilterCompareNode extends TmfFilterAspectNode {
     }
 
     /**
-     * @return the comparison value
+     * @return the comparison value (in seconds for the TIMESTAMP type)
      */
     public String getValue() {
         return fValue;
     }
 
     /**
-     * @param value the comparison value
+     * @param value the comparison value (in seconds for the TIMESTAMP type)
      */
     public void setValue(String value) {
         this.fValue = value;
@@ -127,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 TmfTimestamp((long) (1E9 * NumberFormat.getInstance().parse(value.toString()).doubleValue()));
-            } catch (ParseException e) {
-            }
+            fValueTimestamp = toTimestamp(value);
         }
     }
 
@@ -159,48 +156,66 @@ 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) {
             String valueString = value.toString();
-            int comp = valueString.compareTo(fValue.toString());
-            if (comp < -1) {
-                comp = -1;
-            } else if (comp > 1) {
-                comp = 1;
-            }
+            int comp = (int) Math.signum(valueString.compareTo(fValue.toString()));
             return (comp == fResult) ^ fNot;
         } else if (fType == Type.TIMESTAMP) {
             if (fValueTimestamp != null) {
-                if (value instanceof TmfTimestamp) {
-                    TmfTimestamp valueTimestamp = (TmfTimestamp) value;
-                    return (valueTimestamp.compareTo(fValueTimestamp) == fResult) ^ fNot;
-                }
-                try {
-                    TmfTimestamp valueTimestamp = new TmfTimestamp((long) (1E9 * NumberFormat
-                                    .getInstance().parse(value.toString()).doubleValue()));
-                    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
@@ -209,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
@@ -223,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.027314 seconds and 5 git commands to generate.